GCC & VS weirdness

Published March 12, 2008
Advertisement
Yesterday while coding the destructor for the TextureManager I noticed some rather odd difference in VS and GCC/G++.

First off the piece of code that VS was having trouble with that G++ didn't mind or complain at all about was this:

const unsigned int size = _vLoadedTextures.size();
GLuint textures[size];

Seems, simple enough right?

G++ doesn't throw any errors or warnings about this but VS gives me at least 3 errors.

Error 1 error C2057: expected constant expression
Error 2 error C2466: cannot allocate an array of constant size 0
Error 3 error C2133: 'textures' : unknown size

The first error makes me laugh.

Why is this an issue in VS? Does it not like sizing arrays at run time? I know I could just use malloc but seriously, wtf?
Previous Entry Easy to use
Next Entry Spring Break
0 likes 5 comments

Comments

extralongpants
I believe this is because array.size() could result in a value of 0, and according to MSDN, arrays of size 0 are typically not allowed:

Quote:
The number of elements in the array is given by the constant expression. The first element in the array is the 0th element, and the last element is the (n-1) element, where n is the number of elements the array can contain. The constant-expression must be of an integral type and must be greater than 0. A zero-sized array is legal only when the array is the last field in a struct or union and when the Microsoft extensions (/Ze) are enabled.


Although your integer is constant, it cannot be verified at compile time that your integer will be assigned a valid value ( > 0).
March 12, 2008 10:10 AM
_swx_
No, the problem is that size is not a constant expression.

From http://msdn2.microsoft.com/en-us/library/tby3xex3(VS.80).aspx

"A constant expression is evaluated at compile time, not run time, ..."

So the issue is that your code isn't valid C++.
March 12, 2008 11:10 AM
bladerunner627
Quote:Original post by _swx_
No, the problem is that size is not a constant expression.

From http://msdn2.microsoft.com/en-us/library/tby3xex3(VS.80).aspx

"A constant expression is evaluated at compile time, not run time, ..."

So the issue is that your code isn't valid C++.


"The operands of a constant expression can be .. sizeof expressions, and other constant expressions."

Still, I think it's more of a VS issue, VS has trouble conforming to C++ standards.

http://developer.apple.com/DOCUMENTATION/DeveloperTools/gcc-4.0.1/gccint/Constants.html
This site shows a few types of constant expressions, vector being one of them.

Meh, well, either way it's rather annoying.
March 12, 2008 11:59 AM
_swx_
sizeof is done at compile time, so it's a constant expression. The issue is not vc++, if anything it's why gcc would allow it...
March 12, 2008 12:11 PM
rip-off
Pass g++ the "pedantic" switch, and you get an error:
Quote:
help.cpp: In function 'void example(unsigned int)':
help.cpp:5: error: ISO C++ forbids variable-size array 'buffer'


Where buffer is a char array whose size is the value of the parameter passed to 'example'.
March 12, 2008 03:16 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement

Latest Entries

Boo!

1570 views

Group blog

1060 views

Easy Usage

1077 views

Angel Script

938 views

My game idea

955 views

Lightmaps

929 views

Yay BSPs!

928 views
Advertisement