compile error about void*

This is a discussion on compile error about void* within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, What is wrong with the code, I just want to allocate an array of 100 void* pointers. :-) ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    compile error about void*

    Hello everyone,


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

    Code:
    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

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    Perhaps you could try:
    Code:
    int main()
    {
    	void** p;
    
    	p = new void*[100];
    
    	return 0;
    }
    Incidentally, remember to match new[] with delete[].
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks laserlight,


    It works. I do not know why adding () will make trouble. Any ideas?

    Quote Originally Posted by laserlight View Post
    Perhaps you could try:
    Code:
    int main()
    {
    	void** p;
    
    	p = new void*[100];
    
    	return 0;
    }
    Incidentally, remember to match new[] with delete[].

    regards,
    George

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    From what I understand,
    Code:
    p = new void*[100];
    is equivalent to:
    Code:
    p = new (void*[100]);
    Placing the parentheses as (void*) followed by the [100] causes the compiler to parse it differently such that the syntax is simply incorrect. My guess is that it sees that the non-array form of new is used, yet there is this strange attempt to use the syntax of the array form of new.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks laserlight,


    Quote Originally Posted by laserlight View Post
    yet there is this strange attempt to use the syntax of the array form of new.
    You mean using new to allcoate an array of something is strange? We used it in everyday. :-)


    regards,
    George

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    You mean using new to allcoate an array of something is strange?
    Yes, it is strange. We use new[] (the array form of new), not new (as in the non-array form of new), to dynamically allocate arrays.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Sorry, laserlight!


    I am confused. Do you mean using the statement like this is strange?

    Code:
    int *p = new int [256];
    What do you mean strange? Above code is not strange in my mind. :-)

    Quote Originally Posted by laserlight View Post
    Yes, it is strange. We use new[] (the array form of new), not new (as in the non-array form of new), to dynamically allocate arrays.

    regards,
    George

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    But you wouldn't write [code] int *p = new (int)[256];[/clog], would you?

    The compiler will then interpret the (int) part [or in the original post (void *)] as a C-style cast. It then doesn't understand what the number in brackets does at all, so gives you a bunch of error messages, because it's "got lost and doesn't know where it's going", just like the example I gave some time ago where you tell someone to turn right after the big oak, then left at the second turning - except, the big oak has been cut down, so the person you instructed goes way past the right-turn, ends up on a road with no left-turn, and obviously can't find the place you gave directions to. Unfortunately, a confuzzled compiler will be pretty useless at giving good error messages...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    Do you mean using the statement like this is strange?
    No, that is normal. What you gave in your example is strange, and to adapt it to your new example:
    Code:
    int *p = new (int) [256];
    What I gather is that according to the rules for parsing this as given in the C++ standard, the compiler interprets it as:
    Code:
    int *p = new int;
    So, this is the non-array form of new. Suddenly, it spots the "[256]", hence you get the "missing ';' before '['" error. This is as if you are trying to use the array form of new (which indeed is what you are trying to do), yet have used the non-array form of new due to the placement of the parentheses.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks laserlight and Mats,


    My question is answered.

    Quote Originally Posted by laserlight View Post
    No, that is normal. What you gave in your example is strange, and to adapt it to your new example:
    Code:
    int *p = new (int) [256];
    What I gather is that according to the rules for parsing this as given in the C++ standard, the compiler interprets it as:
    Code:
    int *p = new int;
    So, this is the non-array form of new. Suddenly, it spots the "[256]", hence you get the "missing ';' before '['" error. This is as if you are trying to use the array form of new (which indeed is what you are trying to do), yet have used the non-array form of new due to the placement of the parentheses.

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C and C++ compile speed
    By swgh in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-02-2007, 01:37 PM
  2. Compile as you type
    By Rocketmagnet in forum A Brief History of Cprogramming.com
    Replies: 33
    Last Post: 12-07-2006, 12:36 PM
  3. How to compile mfc libs from platform sdk
    By tjcbs in forum Windows Programming
    Replies: 6
    Last Post: 11-19-2006, 07:20 AM
  4. Compile crashes certain windows
    By Loduwijk in forum C++ Programming
    Replies: 5
    Last Post: 03-26-2006, 08:05 PM
  5. How can I compile C or C++ with Visual Studio .NET?
    By Dakkon in forum C Programming
    Replies: 8
    Last Post: 02-11-2003, 01:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21