Connecting Tech Pros Worldwide Forums | Help | Site Map

compile error about void*

Familiar Sight
 
Join Date: Dec 2007
Posts: 200
#1: Jan 17 '08
Hello everyone,


What is wrong with the code, I just want to allocate an array of 100 void* pointers. :-)

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     void** p;
  4.  
  5.     p = new (void*) [100];
  6.  
  7.     return 0;
  8. }
  9.  
>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

Savage's Avatar
Expert
 
Join Date: Feb 2007
Posts: 1,737
#2: Jan 17 '08

re: compile error about void*


You just remove those '()' around void*,and it should compile.

Expand|Select|Wrap|Line Numbers
  1. p = new void* [100];
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,375
#3: Jan 17 '08

re: compile error about void*


Use of void* deprecated in C++.
Familiar Sight
 
Join Date: Dec 2007
Posts: 200
#4: Jan 18 '08

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.

Expand|Select|Wrap|Line Numbers
  1. p = new void* [100];


regards,
George
Familiar Sight
 
Join Date: Dec 2007
Posts: 200
#5: Jan 18 '08

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
Savage's Avatar
Expert
 
Join Date: Feb 2007
Posts: 1,737
#6: Jan 18 '08

re: compile error about void*


Quote:

Originally Posted by George2

Thanks Savage,


Why adding () does not work, which C++ rule is violated?




regards,
George

Expand|Select|Wrap|Line Numbers
  1. p = new (void*) [100];
this is interpreted by compiler as:

Expand|Select|Wrap|Line Numbers
  1. p=(new unknown_type) (void*)[100];
because of new operator precedence.
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,375
#7: Jan 18 '08

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.
Savage's Avatar
Expert
 
Join Date: Feb 2007
Posts: 1,737
#8: Jan 18 '08

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:

Expand|Select|Wrap|Line Numbers
  1. void foo();
  2. void (**p)();
  3.  
  4. p = new (void (*[3])());
  5. p[0] = f;
  6.  
  7. //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
#9: Jan 19 '08

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:

Expand|Select|Wrap|Line Numbers
  1. void foo();
  2. void (**p)();
  3.  
  4. p = new (void (*[3])());
  5. p[0] = f;
  6.  
  7. //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
#10: Jan 19 '08

re: compile error about void*


Thanks Savage,


Could you explain why the code will be explained to

Expand|Select|Wrap|Line Numbers
  1. p=(new unknown_type) (void*)[100];
please?

What is unknown_type comes from? What is new operator precedence?

Quote:

Originally Posted by Savage

Expand|Select|Wrap|Line Numbers
  1. p = new (void*) [100];
this is interpreted by compiler as:

Expand|Select|Wrap|Line Numbers
  1. p=(new unknown_type) (void*)[100];
because of new operator precedence.


regards,
George
Familiar Sight
 
Join Date: Dec 2007
Posts: 200
#11: Jan 19 '08

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
#12: Jan 20 '08

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
#13: Jan 22 '08

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
#14: Jan 22 '08

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
#15: Jan 23 '08

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
#16: Jan 23 '08

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
#17: Jan 25 '08

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
#18: Jan 25 '08

re: compile error about void*


That is correct.

Expand|Select|Wrap|Line Numbers
  1. char array[1000];
  2. int* arr = new (array) int[5];
  3.  
The int array is allocated inside the char array.
Familiar Sight
 
Join Date: Dec 2007
Posts: 200
#19: Jan 27 '08

re: compile error about void*


Thanks weaknessforcats!


My question is answered.

Quote:

Originally Posted by weaknessforcats

That is correct.

Expand|Select|Wrap|Line Numbers
  1. char array[1000];
  2. int* arr = new (array) int[5];
  3.  
The int array is allocated inside the char array.


regards,
George
Reply