Is this bad C++ code?

Joined
Mar 6, 2003
Messages
737
Is this bad code?

Code:
void foo(int n) {
  float bar[n];
  ...
}

That code compiles and works, but I thought you are suppose to do something like:
Code:
float * bar = new float[n];

Why does it let you compile and use those arrays if your not suppose to do that?
 
The first example should not compile, or at least should not work.
The second way is the only correct way to allocate dynamic memory for your given situation.
 
Well, that's werid.

Here I am using gcc 3.3.1 with -Wall...but it compiles and even works (must be overwriting random bits of memory heh)
 
Pyrolistical said:
Well, that's werid.

Here I am using gcc 3.3.1 with -Wall...but it compiles and even works (must be overwriting random bits of memory heh)

It will work, but you shouldn't use it. If you want strict checking, try adding the -ansi and -pedantic flags.
 
Yeh, you can do:

g++ -Wall -Wextra -pedantic -ansi file.cpp -o file

If the gcc version is a little old then, -W instead of -Wextra.
 
Back
Top