compile error about void* | Familiar Sight | | Join Date: Dec 2007
Posts: 200
| |
Hello everyone,
What is wrong with the code, I just want to allocate an array of 100 void* pointers. :-) -
int main()
-
{
-
void** p;
-
-
p = new (void*) [100];
-
-
return 0;
-
}
-
>d:\visual studio 2008\projects\test_void1\test_void1\main.cpp(5) : error C2143: syntax error : missing ';' before '['
1>d:\visual studio 2008\projects\test_void1\test_void1\main.cpp(5) : error C3409: empty attribute block is not allowed
1>d:\visual studio 2008\projects\test_void1\test_void1\main.cpp(5) : error C2143: syntax error : missing ']' before 'constant'
1>d:\visual studio 2008\projects\test_void1\test_void1\main.cpp(5) : error C2143: syntax error : missing ';' before 'constant'
1>d:\visual studio 2008\projects\test_void1\test_void1\main.cpp(5) : error C2143: syntax error : missing ';' before ']'
1>d:\visual studio 2008\projects\test_void1\test_void1\main.cpp(5) : error C2143: syntax error : missing ';' before ']'
thanks in advance,
George
|  | Expert | | Join Date: Feb 2007
Posts: 1,737
| | | re: compile error about void*
You just remove those '()' around void*,and it should compile. | | Moderator | | Join Date: Mar 2007 Location: North Bend Washington USA
Posts: 5,375
| | | re: compile error about void*
Use of void* deprecated in C++.
| | Familiar Sight | | Join Date: Dec 2007
Posts: 200
| | | re: compile error about void*
Thanks Savage,
Why adding () does not work, which C++ rule is violated? Quote:
Originally Posted by Savage You just remove those '()' around void*,and it should compile.
regards,
George
| | Familiar Sight | | Join Date: Dec 2007
Posts: 200
| | | re: compile error about void*
Why weaknessforcats?
Any links for this topic and using what to replace void*? Quote:
Originally Posted by weaknessforcats Use of void* deprecated in C++.
regards,
George
|  | Expert | | Join Date: Feb 2007
Posts: 1,737
| | | re: compile error about void* Quote:
Originally Posted by George2 Thanks Savage,
Why adding () does not work, which C++ rule is violated?
regards,
George
this is interpreted by compiler as: - p=(new unknown_type) (void*)[100];
because of new operator precedence.
| | Moderator | | Join Date: Mar 2007 Location: North Bend Washington USA
Posts: 5,375
| | | re: compile error about void* Quote:
Originally Posted by george2 p = new (void*) [100]; C++ ain't C.
In C++ new(void*) is a function named new that has a void* arguiment. Quote:
Originally Posted by George2 Why weaknessforcats?
Any links for this topic and using what to replace void*?
Quote:
Originally Posted by weaknessforcats
Use of void* deprecated in C++. Function overloading.
|  | Expert | | Join Date: Feb 2007
Posts: 1,737
| | | re: compile error about void* Quote:
Originally Posted by weaknessforcats C++ ain't C.
In C++ new(void*) is a function named new that has a void* arguiment.
Function overloading. What about this then: - void foo();
-
void (**p)();
-
-
p = new (void (*[3])());
-
p[0] = f;
-
-
//etc...
Is this interpreted as a overloaded function called new that takes a pointer to the array of generic function pointers,or does it allocate memory for p?
| | Familiar Sight | | Join Date: Dec 2007
Posts: 200
| | | re: compile error about void*
Thanks Savage,
I have tried your code can compile. What does the following statement mean?
p = new (void (*[3])());
what is the type of p? Could you interpret the meaning of right side of assignment and left side of assignment please? Quote:
Originally Posted by Savage What about this then: - void foo();
-
void (**p)();
-
-
p = new (void (*[3])());
-
p[0] = f;
-
-
//etc...
Is this interpreted as a overloaded function called new that takes a pointer to the array of generic function pointers,or does it allocate memory for p?
regards,
George
| | Familiar Sight | | Join Date: Dec 2007
Posts: 200
| | | re: compile error about void*
Thanks Savage,
Could you explain why the code will be explained to - p=(new unknown_type) (void*)[100];
please?
What is unknown_type comes from? What is new operator precedence? Quote:
Originally Posted by Savage
this is interpreted by compiler as: - p=(new unknown_type) (void*)[100];
because of new operator precedence.
regards,
George
| | Familiar Sight | | Join Date: Dec 2007
Posts: 200
| | | re: compile error about void*
Yes, weaknessforcats.
I agree "In C++ new(void*) is a function named new that has a void* arguiment", and the global new operator function is invoked.
But, does it has anything to do with my question (compile error)? Could you provide more description please? Quote:
Originally Posted by weaknessforcats C++ ain't C.
In C++ new(void*) is a function named new that has a void* arguiment.
Function overloading.
regards,
George
| | Moderator | | Join Date: Mar 2007 Location: North Bend Washington USA
Posts: 5,375
| | | re: compile error about void* Quote:
Originally Posted by George2 I agree "In C++ new(void*) is a function named new that has a void* arguiment", and the global new operator function is invoked. If you agree that new(void*) is a function named new that has a void argument, then you have answered your own question. You are not allowed to have a function with the same name as a C++ keyword.
| | Familiar Sight | | Join Date: Dec 2007
Posts: 200
| | | re: compile error about void*
Thanks weaknessforcats,
In below statements, you mean system global new function? Quote:
Originally Posted by weaknessforcats If you agree that new(void*) is a function named new that has a void argument, then you have answered your own question.
regards,
George
| | Moderator | | Join Date: Mar 2007 Location: North Bend Washington USA
Posts: 5,375
| | | re: compile error about void*
No I do not. new is a keyword. You can't use new as a function name.
I think you may confusing the keyword new with the function
operator new() that is called to allocate memory. I'm not making this up. The new operator calls operator new.
| | Familiar Sight | | Join Date: Dec 2007
Posts: 200
| | | re: compile error about void*
Thanks weaknessforcats,
Now I understand you mean operator new. :-)
This is what you mentioned before,
--------------------
If you agree that new(void*) is a function named new that has a void argument, then you have answered your own question.
--------------------
Why if I agree I should answer it by myself? I am confused. So, you mean you think in my question the operator new which takes void* as input argument take effect? Quote:
Originally Posted by weaknessforcats No I do not. new is a keyword. You can't use new as a function name.
I think you may confusing the keyword new with the function
operator new() that is called to allocate memory. I'm not making this up. The new operator calls operator new.
regards,
George
| | Moderator | | Join Date: Mar 2007 Location: North Bend Washington USA
Posts: 5,375
| | | re: compile error about void*
What the compiler sees is new(void*).
That is a function named new that has a void* argument.
That is an error because new is a reserved C++ keyword.
End of discussion.
| | Familiar Sight | | Join Date: Dec 2007
Posts: 200
| | | re: compile error about void*
Thanks for your help, weaknessforcats!
I think when adding (), the placement form of new is invoked, right? Quote:
Originally Posted by weaknessforcats What the compiler sees is new(void*).
That is a function named new that has a void* argument.
That is an error because new is a reserved C++ keyword.
End of discussion.
regards,
George
| | Moderator | | Join Date: Mar 2007 Location: North Bend Washington USA
Posts: 5,375
| | | re: compile error about void*
That is correct. -
char array[1000];
-
int* arr = new (array) int[5];
-
The int array is allocated inside the char array.
| | Familiar Sight | | Join Date: Dec 2007
Posts: 200
| | | re: compile error about void*
Thanks weaknessforcats!
My question is answered. Quote:
Originally Posted by weaknessforcats That is correct. -
char array[1000];
-
int* arr = new (array) int[5];
-
The int array is allocated inside the char array.
regards,
George
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|