Archived

This topic is now archived and is closed to further replies.

GCC v. MSVC++ Question

This topic is 5654 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I encountered this problem a few months back, but I never asked anyone about it until now.
  
int main()
{
   int x = 40;
   float z[x];
   return 0;
}
  
I was under the impression that the above snippet of code is illegal under ANSI C++, because x is not a constant. Visual C++ 6.0 responded as I thought it would, with errors: c:\projects\test\test.cpp(4) : error C2057: expected constant expression c:\projects\test\test.cpp(4) : error C2466: cannot allocate an array of constant size 0 c:\projects\test\test.cpp(4) : error C2133: ''z'' : unknown size When I try this using GCC, though, it compiles without a problem. I tried this with DJGPP and the GCC compiler on the Unix machines at CSU Chico, and both compiled fine. I don''t have much information on the latter compiler, though. Here''s the source code for the lab where this came up (CSCI 15B class), in case the snippet above isn''t enough. That file will not compile in Visual C++ unless I comment out the #define GNU line. Anyone know the reason for this? Is this a fault for either compiler? -------------------------- "Facing terror isn''t half as fun as sharing it" -- Bun-bun

Share this post


Link to post
Share on other sites
You should instead dynamically allocate memory using "new", then delete it when you''re done using "delete".

Once you know arrays and pointers inside out you could then move on to using the stl vector, but that''s another story.


Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions

Share this post


Link to post
Share on other sites
Oluseyi: Therein lies the rub. Not only does it compile with GCC without any warnings or errors, the program works without a single problem. I got 100% on that lab, doing the exact same thing. I don't understand it at all.

siaspete: I know I should have allocated memory with new/delete. I knew that when I wrote the program. But my professor's assistant, who wrote all of the labs, said we could NOT use dynamic memory allocation. Given that I wouldn't know what size the array needed to be until the user told me, it was either float z[x] or make an extremely large static array.

--------------------------
"Facing terror isn't half as fun as sharing it" -- Bun-bun

[edited by - Creation on June 24, 2002 2:34:23 AM]

Share this post


Link to post
Share on other sites
Well, variable length arrays are allowed in C according to the C99 spec. Here''s a snippet from the spec itself


  
size_t fsize3 (int n)
{
char b[n+3]; // Variable length array.

return sizeof b; // Execution time sizeof.

}


and this wasn''t used to illustrate an error or undefined behavior condition. I haven''t found the exact place in the spec that delineates the introduction of variable length arrays - but no matter. I only brought this up as a possibility of why gcc didn''t cough when msvc did. Perhaps gcc has adopted some of the C99 spec?

Share this post


Link to post
Share on other sites
Magmai: I tried. No change. And my copy of DJGPP was/is version 3.03. I have no idea what the school uses and I haven''t a clue how to check.

LessBread: That doesn''t make a whole heap of sense... allowing variable length arrays. What''s the point? Dynamic allocation on the stack as opposed to the heap? I honestly don''t know o.O

--------------------------
"Facing terror isn''t half as fun as sharing it" -- Bun-bun

Share this post


Link to post
Share on other sites
quote:
Original post by Creation
LessBread: That doesn't make a whole heap of sense... allowing variable length arrays. What's the point? Dynamic allocation on the stack as opposed to the heap? I honestly don't know o.O



Magmai's answer is more likely to be the case. I offered the above as a possibility - as remote as it may be. However, since it's in the C99 spec - where the possibility for dynamic allocation on the stack presents itself as well - there might be something more going on. I'm reminded of the situation of declaring a zeroed array
eg int array[nelements] = {0}; (where nelements is a const) - some compilers put that array into the .text section as a block of zeroes. Other's take the liberty of inserting a call to memset (or equivalent) to zero the array. In this light, might not some compilers take the liberty of inserting a call to malloc (or equivalent) so that the dynamic allocation comes from the heap and not the stack? // edit - actually the call would be to calloc wouldn't it?

[edited by - lessbread on June 24, 2002 3:13:22 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
dynamic stack tables, posible, 0 cost, no functions need to be called, no warrying about freeing memory, alsow more chances that table is already in CPU cache, but you can blow the stack, you need to be carefull, gcc supports more language facilities than msvc, use a parameter switch to compile with strict ansi rules

Share this post


Link to post
Share on other sites