I got this piece of code:


Code:
#include <stdio.h>
main()
{
   int i=5;
   int v[i];
   
   for (i=0; i<5;i++)
{   v[i] = 0;
   printf("%d array= %d\n", i, v[i]);}


}


There's supposed to be something dubious about it and I'm supposed to find out what, but the compiler doesn't give me any error and the program works just fine.
Yet I looked up the solution and it says that the dimension of an array must be a constant or a constant expression.

My doubt is, why's that when using a variable works just fine?
Hmm, my guess is the compiler is optimizing away the variable in that case. Try it where the user enters in the size and declare it the same way.
Can you be more specific?
Depending on your compiler and the standard it purports to accept, this may be allowed. C99 permits variable-length arrays, and I'd imagine newer revisions of C++ also permit it.

What compiler are you using, and what options are you passing to it? GCC's default gnu89 dialect might allow this; ANSI C definitely doesn't.
My compiler is C-Free Standard
Looks more like that's your IDE, which appears to ship with MinGW GCC. Still can't tell what options you're using though.
...

Look I don't know that much about programming... I have no idea what an IDE or an MinGW GCC might be.

So, my guess is, it works though some compilers don't support it, it that it?
More or less, yeah. The more canonical way to do it, which I'm sure you'll learn later in your book, is with malloc.
Alright, thanks for the help guys Smile
AliceIsDead wrote:
So, my guess is, it works though some compilers don't support it, it that it?


merthsoft wrote:
More or less, yeah. The more canonical way to do it, which I'm sure you'll learn later in your book, is with malloc.


That's not true at all. Most compilers do support it. The array is simply allocated on the stack. Like:


X86

Code:

mov eax, 5         ; load 5 into eax for int i;
add eax, eax      ; multiply by 4 to get size of array
add eax, eax
sub esp, eax      ; allocate space on the stack

; do loop code here

add esp, eax      ; free space on the stack
Wouldn't most compilers supporting it mean that some don't support it? Meaning it is true at all?
Very few compilers don't support. Unless you're using some compiler you found off of a shady website instead of something like GCC, MSVC, TCC, LCC, etc, you have nothing to worry about.

Variable length arrays were added in C99. That's 1999, 15 years ago.
Quote:
Very few compilers don't support.
And therefore some number of compilers don't support it. So what I said isn't wrong.
When I said "very few" I meant private compilers. Things like what university students work on as a project might not support it.

Any publicly used compiler does.
Counterpoint: MSVC doesn't support C99.
Tari wrote:
Counterpoint: MSVC doesn't support C99.


That's bull. MSVC was actually the first to implement several of C99's features, and was the first to write a safe standard C library that prevents things like buffer overflows.

When it comes to standard C library implementation, MSVC is way ahead of anything else.
Transcribed from IRC/SAX.

Code:
[03:09:52] <@Merth> http://stackoverflow.com/questions/5246900/enabling-vlasvariable-length-arrays-in-ms-visual-c
[03:09:55] <@Merth> http://msdn.microsoft.com/en-us/library/02y9a5ye.aspx
[03:10:42] <@saxjax> (C) [DaemonR] Therein lies the problem altogether
[03:10:42] <@saxjax> (C) [DaemonR] MSVC is not a C compiler
[03:10:42] <@saxjax> (C) [DaemonR] it's a C++ compiler
[03:10:55] <+Tari> and yet it conforms to C90..
[03:11:02] <@saxjax> (C) [DaemonR] C11 and C++11 are actually incompatible
[03:11:05] <+Tari> therefore it's a C compiler
[03:11:27] <@Merth> The only answer is deathmatch
[03:12:02] <@saxjax> (C) [DaemonR] For the record, that post was made 3 years ago
[03:12:02] <@saxjax> (C) [DaemonR] There's already new standards release, and like 4 new versions of MSVC
[03:12:17] <+Tari> http://msdn.microsoft.com/en-us/library/02y9a5ye.aspx
[03:12:21] <+Tari> > Visual Studio 2013
[03:12:34] <+Tari> you're still wrong
[03:13:03] <@saxjax> (C) [DaemonR] I guess I am
I was wrong. Found this

https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html#Variable-Length

While VLA's are allowed in the C99 standard, they are not enforced by it. According to this page, Microsoft added support for the C99 runtime library in 2013. However, according to the roadmap, Microsoft is not actually going to implement the full C99 standard. Like...ever.

I'm guessing this stems somewhat from the fact that there are incompatibilities between C and C++ standards. MSVC was designed specifically for C++. The fact that it has C89/C90 compliance is more or less a perk.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement