Connecting Tech Pros Worldwide Help | Site Map

compile error about void*

  #1  
Old January 17th, 2008, 08:15 AM
George2
Guest
 
Posts: n/a
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.  
Quote:
>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  
Old January 17th, 2008, 08:25 AM
t.lehmann@rtsgroup.net
Guest
 
Posts: n/a

re: compile error about void*


void** p;
Quote:
>
p = new (void*) [100];
Write this:
p = new void*[100];

You syntax: using a special new operator.
  •         
  •  
  •                 >
  •     return 0;
  • }
  •  
  •         
  •     
  •     
  •   #3  
    Old January 17th, 2008, 08:25 AM
    Michael DOUBEZ
    Guest
     
    Posts: n/a

    re: compile error about void*


    George2 a écrit :
    Quote:
    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];
    Expand|Select|Wrap|Line Numbers
    1.  
    2. p = new void*[100];
    3.     Quote:
  •     
  •     
  • Quote:
    >
    Quote:
    >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
      #4  
    Old January 17th, 2008, 08:25 AM
    anon
    Guest
     
    Posts: n/a

    re: compile error about void*


    George2 wrote:
    Quote:
    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.  
    >
    Quote:
    >d:\visual studio 2008\projects\test_void1\test_void1\main.cpp(5) : error C2143: syntax error : missing ';' before '['
    It tells you whats wrong.
    Try this instead:

    int main()
    {
    void** p;
    p = new void* [100];
    return 0;
    }
      #5  
    Old January 17th, 2008, 12:05 PM
    jalina
    Guest
     
    Posts: n/a

    re: compile error about void*


    George2 a écrit :
    Quote:
    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.  
    >
    Quote:
    >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
    typedef void* PVOID;

    int
    main()
    {
    PVOID *p;
    p = new PVOID[100];
    return 0;
    }

      #6  
    Old January 17th, 2008, 02:05 PM
    =?iso-8859-1?q?Tom=E1s_=D3_h=C9ilidhe?=
    Guest
     
    Posts: n/a

    re: compile error about void*


    George2:
    Quote:
    p = new (void*) [100];

    Where T is a type:

    If you want an array of pointers then:

    T*[num]

    If you want a pointer to an array, then:

    T(*)[num]


    --
    Tomás Ó hÉilidhe
      #7  
    Old January 18th, 2008, 10:25 AM
    James Kanze
    Guest
     
    Posts: n/a

    re: compile error about void*


    On Jan 17, 9:28 am, Michael DOUBEZ <michael.dou...@free.frwrote:
    Quote:
    George2 a écrit :
    Quote:
    Quote:
    What is wrong with the code, I just want to allocate an array of 100
    void* pointers. :-)
    Quote:
    Quote:
    [code]
    int main()
    {
    void** p;
    Quote:
    Quote:
    p = new (void*) [100];
    Quote:
    p = new void*[100];
    Or "p = new (void * [100]) ;".

    The type specifier in a new expression has a very restricted
    syntax. In particular, it cannot contain parentheses unless it
    is entirely surrounded by parentheses.

    Note that the syntax "new (int*)[100]" would be legal syntax,
    but doesn't do what you might expect, and will invoke undefined
    behavior at run-time---it allocates a single int*, then uses it
    as if it were a pointer to the first element of an array,
    accessing the 101st element. Of course, the resulting type of
    the expression is int, so you will almost certainly get a type
    error when you try to assign the results.

    --
    James Kanze (GABI Software) email:james.kanze@gmail.com
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
      #8  
    Old January 18th, 2008, 10:35 AM
    James Kanze
    Guest
     
    Posts: n/a

    re: compile error about void*


    On Jan 17, 2:58 pm, "Tomás Ó hÉilidhe" <t...@lavabit.comwrote:
    Quote:
    George2:
    Quote:
    Quote:
    p = new (void*) [100];
    Quote:
    Where T is a type:
    Quote:
    If you want an array of pointers then:
    Quote:
    T*[num]
    Quote:
    If you want a pointer to an array, then:
    Quote:
    T(*)[num]
    Not in a new expression. In a new expression, you'd have to put
    that in parentheses (i.e. "(T(*)[num])").

    More generally, I don't think there's any context in the
    language where "(void*)[100]" could be legal. If the [...] is
    the subscript operator, then what precedes must be an expression
    (and "(void*)" isn't a legal expression). And if the [...] is
    meant to be part of a declaration, the only context in a
    declaration where (void*) would be legal is as a list of
    function parameters, and you can't declare a function to return
    an array. (You can declare a function to return a pointer or a
    reference to an array, but in this case, it would be something
    like:
    int (&f(void*))[100] ;
    with an extra closing ) in the string.)

    --
    James Kanze (GABI Software) email:james.kanze@gmail.com
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
      #9  
    Old January 19th, 2008, 11:45 PM
    Jerry Coffin
    Guest
     
    Posts: n/a

    re: compile error about void*


    In article <852ad1fa-f151-4bb5-b2c6-802042120aa8
    @m34g2000hsf.googlegroups.com>, george4academic@yahoo.com says...
    Quote:
    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.  
    You've gotten a number of answers that show the problem with the syntax
    you're using. None, however, has mentioned that there are usually better
    ways of doing this in C++. If you really want to do this, something like
    "std::vector<void *p(100)" will do the job -- but an array (or vector)
    of pointers to void sounds somewhat questionable in itself. If you're
    doing something like writing your own memory allocator, this is likely
    to make sense -- but for most code, a pointer to void (not to mention a
    lot of pointers to void) tends to indicate a possible problem.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
      #10  
    Old January 20th, 2008, 12:25 AM
    Ioannis Vranos
    Guest
     
    Posts: n/a

    re: compile error about void*


    George2 wrote:
    Quote:
    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.  

    Wrong syntax:


    The correct way to do what you want is:

    void **p= new void *[100];
    Closed Thread


    Similar Threads
    Thread Thread Starter Forum Replies Last Post
    compile error about void* George2 answers 18 January 27th, 2008 08:33 AM
    gcc compile error about vector Huang.Antony@gmail.com answers 5 December 1st, 2005 11:35 AM
    gcc compile error about vector Huang.Antony@gmail.com answers 7 November 30th, 2005 01:45 PM
    help: class compile error xuatla answers 5 July 22nd, 2005 05:06 PM