473,241 Members | 1,487 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

compile error about void*

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
Jan 17 '08 #1
9 3608
void** p;
>
p = new (void*) [100];
Write this:
p = new void*[100];

You syntax: using a special new operator.
Jan 17 '08 #2
George2 a écrit :
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.     p = new (void*) [100];
Expand|Select|Wrap|Line Numbers
  1.  
  2. p = new void*[100];
  3.  
  4.         
  5.                 >
  6.     return 0;
  7. }
  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
Jan 17 '08 #3
George2 wrote:
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.     p = new (void*) [100];
  5.     return 0;
  6. }
  7.  
>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;
}
Jan 17 '08 #4
George2 a écrit :
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.     p = new (void*) [100];
  5.     return 0;
  6. }
  7.  
>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;
}

Jan 17 '08 #5
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
Jan 17 '08 #6
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:ja*********@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
Jan 18 '08 #7
On Jan 17, 2:58 pm, "Tomás Ó hÉilidhe" <t...@lavabit.comwrote:
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:ja*********@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
Jan 18 '08 #8
In article <852ad1fa-f151-4bb5-b2c6-802042120aa8
@m34g2000hsf.googlegroups.com>, ge*************@yahoo.com says...
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.     p = new (void*) [100];
  5.     return 0;
  6. }
  7.  
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.
Jan 19 '08 #9
George2 wrote:
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.     p = new (void*) [100];
  5.     return 0;
  6. }
  7.  

Wrong syntax:
The correct way to do what you want is:

void **p= new void *[100];
Jan 20 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: xuatla | last post by:
Hi, I encountered the following compile error of c++ and hope to get your help. test2.cpp: In member function `CTest CTest::operator+=(CTest&)': test2.cpp:79: error: no match for 'operator='...
8
by: pavel.orehov | last post by:
Hi, I am using flex and bizon to write HTTP parser. I am passing well flex and bison tools but can't compile their output. ================= Flex file (http_parser.lpp) ============== %{...
10
by: Chris LaJoie | last post by:
Our company has been developing a program in C# for some time now, and we haven't had any problems with it, but just last night something cropped up that has me, and everyone else, stumped. I...
1
by: ranges22 | last post by:
****************************************************************** I am compiling a librarry which has a .h file containing th following:...
1
by: electrixnow | last post by:
Help!, I need to compile this code with static libs so it run on another XP machine that does'nt have MS Studio installed. When I compile now I get an ERROR: 1>------ Rebuild All started:...
1
by: HugoScripts | last post by:
hi there, as i said i'm trying to compile a simple program that uses allegro, it's a small thing, indeed my goal was just to start using allegro, but until now i'm unable even to compile my simple...
1
by: Jonas Schneider | last post by:
Heya, don´t know if you know BOINC: it´s a system for distributed computing, therefore distributing work to many computers and creating a little super-computer, see also their homepage:...
12
by: Ioannis Vranos | last post by:
Perhaps a mechanism can be introduced in the C++0x/1x standard, something simple like defining a function as: void somefunc(void) throw() { // ... }
2
by: akhilesh.noida | last post by:
I am trying to compile glibc-2.5 for ARM based board. But I am getting errors while configuring it. Please check and give your inputs for resolving this. configure command : $...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.