What am I missing here? array declaration confusion

Started by
10 comments, last by graveyard filla 19 years, 7 months ago
We started coding in my c++ class today, and the instructor has

	int var;
	cin>>var;
	int array[var];


which compiles on the compilers at school but not on MVSC++ which i use. Now I thought when you declared arrays like this, the array was created at run time and I thought the only way to do something to this effect was with int *array=new int[var]; but that doesnt explain why it compiles on the comps at school or why the instructor had no clue what I was talking about. Is this compiler specific? We're using some linux compiler at school.
Advertisement
well whats the error you are getting for why it won't compile for you?
: error C2057: expected constant expression
: error C2466: cannot allocate an array of constant size 0
: error C2133: 'array' : unknown size
The error there is that arrays allocated to the stack need a size that can be determined at compile time, and hence the integer needs to be constant. If you want to create an array that is sized dynamically at runtime you need to use the new operator.

int arraysize = 0;std::cin >> arraysize;int* parray = new int[arraysize];delete [] parray;


This is stuff that you cannot learn well over the internet forums, you need a good beginners book. Thinking in C++ is an ok book, and its free!

It's not legal C++. Static array sizes must be compile-time constants. C99 allows variable-length arrays, but C++, at present, does not. You must indeed allocate memory dynamically—or use a vector.
thats weird... i dont see how that could work....

EDIT: are you sure var wasnt a constant ??
FTA, my 2D futuristic action MMORPG
No no no , lol ,thank you guys for the replies but your making inferences! I'm not confused as to why it DOESNT work for ME, im confused as to why it DOES work for HIM and why this dude who took c++ in college has no idea what I'm talking about. I've been coding c++ for slightly over a year now and I didnt think it what he was doing was valid, but it does compile at school! I cant figure out why.

edit: yea graveyard im positive, this makes no sence.

edit2: what exaclty is C99? i guess that must be what we're doing...
C99 is the 1999 revision of the C language standard and has nothing directly to do with C++. As for why this works on the compilers where you have seen it in action—not all compilers are entirely standard compliant; presumably whatever compiler you use has an extension to allow this functionality. If it is g++ (or MinGW, or ...), you may want to play with the flags -ansi and -pedantic to enforce standard C++.

I personally compile pretty much every C++ program I write with -Wall -Werror -ansi -pedantic ...
Oh, whats g++ because thats the command we use to compile it. (He has us do virtually ALL file manipulation through the stupid linux console), I wonder why he didnt explain any of this from the start, let alone once i brought up my confusion.
g++ is the C++ compiler of the GNU Compiler Collection, GCC. What, exactly, is it that he should have mentioned? I'm not sure I understand your grievance—though I obviously think that using the -Werror, -Wall, -ansi, and -pedantic options is a good idea, unless you have specific reason not to.

As for "the stupid linux console", don't knock it. I'm a Gentoo Linux user myself, and mostly a CLI man (through graphical terminals like gnome-terminal)—I've yet to find a GUI that offers me the same power, flexibility, and yes, ease of use that the bash shell offers me (save for specific purposes, such as browsing image directories—then I use Nautilus). Once you learn about redirection and piping, you'll wonder how you could ever stand using a GUI ...

As for "why this dude who took c++ in college" doesn't know what's wrong, well, a CS degree is not about the intricacies of any one language, and many a good programmer may be unaware of some of the finer nuances. Additionally, there are a lot of people who graduate from college who couldn't program their way out of a paper bag if they had a chainsaw with a C++ interface ...

This topic is closed to new replies.

Advertisement