Sign in to follow this  
  • entries
    56
  • comments
    80
  • views
    41680

GCC & VS weirdness

Sign in to follow this  

218 views

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?
Sign in to follow this  


5 Comments


Recommended Comments

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).

Share this comment


Link to comment
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++.

Share this comment


Link to comment
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.

Share this comment


Link to comment
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...

Share this comment


Link to comment
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'.

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now