compile error about void*

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • George2

    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\t est_void1\test_ void1\main.cpp( 5) : error C2143: syntax error : missing ';' before '['
    1>d:\visual studio 2008\projects\t est_void1\test_ void1\main.cpp( 5) :
    error C3409: empty attribute block is not allowed
    1>d:\visual studio 2008\projects\t est_void1\test_ void1\main.cpp( 5) :
    error C2143: syntax error : missing ']' before 'constant'
    1>d:\visual studio 2008\projects\t est_void1\test_ void1\main.cpp( 5) :
    error C2143: syntax error : missing ';' before 'constant'
    1>d:\visual studio 2008\projects\t est_void1\test_ void1\main.cpp( 5) :
    error C2143: syntax error : missing ';' before ']'
    1>d:\visual studio 2008\projects\t est_void1\test_ void1\main.cpp( 5) :
    error C2143: syntax error : missing ';' before ']'


    thanks in advance,
    George
  • t.lehmann@rtsgroup.net

    #2
    Re: compile error about void*

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

    You syntax: using a special new operator.

    Comment

    • Michael DOUBEZ

      #3
      Re: compile error about void*

      George2 a écrit :
      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];
      Code:
      p = new void*[100];
      [QUOTE]
      >
      	return 0;
      }
      [/QUOTE]
      >
      >d:\visual studio 2008\projects\t est_void1\test_ void1\main.cpp( 5) : error C2143: syntax error : missing ';' before '['
      1>d:\visual studio 2008\projects\t est_void1\test_ void1\main.cpp( 5) :
      error C3409: empty attribute block is not allowed
      1>d:\visual studio 2008\projects\t est_void1\test_ void1\main.cpp( 5) :
      error C2143: syntax error : missing ']' before 'constant'
      1>d:\visual studio 2008\projects\t est_void1\test_ void1\main.cpp( 5) :
      error C2143: syntax error : missing ';' before 'constant'
      1>d:\visual studio 2008\projects\t est_void1\test_ void1\main.cpp( 5) :
      error C2143: syntax error : missing ';' before ']'
      1>d:\visual studio 2008\projects\t est_void1\test_ void1\main.cpp( 5) :
      error C2143: syntax error : missing ';' before ']'
      >
      >
      thanks in advance,
      George

      Comment

      • anon

        #4
        Re: compile error about void*

        George2 wrote:
        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\t est_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;
        }

        Comment

        • jalina

          #5
          Re: compile error about void*

          George2 a écrit :
          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\t est_void1\test_ void1\main.cpp( 5) : error C2143: syntax error : missing ';' before '['
          1>d:\visual studio 2008\projects\t est_void1\test_ void1\main.cpp( 5) :
          error C3409: empty attribute block is not allowed
          1>d:\visual studio 2008\projects\t est_void1\test_ void1\main.cpp( 5) :
          error C2143: syntax error : missing ']' before 'constant'
          1>d:\visual studio 2008\projects\t est_void1\test_ void1\main.cpp( 5) :
          error C2143: syntax error : missing ';' before 'constant'
          1>d:\visual studio 2008\projects\t est_void1\test_ void1\main.cpp( 5) :
          error C2143: syntax error : missing ';' before ']'
          1>d:\visual studio 2008\projects\t est_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;
          }

          Comment

          • =?iso-8859-1?q?Tom=E1s_=D3_h=C9ilidhe?=

            #6
            Re: compile error about void*

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

            Comment

            • James Kanze

              #7
              Re: compile error about void*

              On Jan 17, 9:28 am, Michael DOUBEZ <michael.dou... @free.frwrote:
              George2 a écrit :
              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];
              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.kan ze@gmail.com
              Conseils en informatique orientée objet/
              Beratung in objektorientier ter Datenverarbeitu ng
              9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

              Comment

              • James Kanze

                #8
                Re: compile error about void*

                On Jan 17, 2:58 pm, "Tomás Ó hÉilidhe" <t...@lavabit.c omwrote:
                George2:
                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]
                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.kan ze@gmail.com
                Conseils en informatique orientée objet/
                Beratung in objektorientier ter Datenverarbeitu ng
                9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                Comment

                • Jerry Coffin

                  #9
                  Re: compile error about void*

                  In article <852ad1fa-f151-4bb5-b2c6-802042120aa8
                  @m34g2000hsf.go oglegroups.com> , george4academic @yahoo.com says...
                  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;
                  }
                  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<vo id *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.

                  Comment

                  • Ioannis Vranos

                    #10
                    Re: compile error about void*

                    George2 wrote:
                    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;
                    }

                    Wrong syntax:


                    The correct way to do what you want is:

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

                    Comment

                    Working...