Page 1 of 1

Surprised with dynamic memory allocation on stack

Posted: Mon Jun 22, 2009 7:33 am
by d4n1l0d
Like the subject says, I surprised with dynamic memory allocation on stack!
I tried this code:

Code: Select all

int main(int argc, char *argv[])
{
   int n;
   scanf("%d",&n);

  int vector[n];
}
And it worked!!
I'm using dev-cpp on Windows Vista ( gcc (GCC) 3.4.2 (mingw-special) )
( looking the asm code I could see that it does it by calling alloca() )
What I want to know is if this kind of memory allocation is allowed by ANSI C?
And, is there any other kind of stack dynamic memory allocation?

Re: Surprised with dynamic memory allocation on stack

Posted: Mon Jun 22, 2009 7:39 am
by Solar
Variable Length Arrays (VLA's), introduced with C99.

See http://www.geocities.com/vijoeyz/articl ... /pna2.html.

*Nods*

Yes, C99 has much to offer beyond what K&R told you. It's worth checking it out.

Re: Surprised with dynamic memory allocation on stack

Posted: Mon Jun 22, 2009 7:43 am
by d4n1l0d
Thanks a lot!
I'll take a look on that article!