Surprised with dynamic memory allocation on stack

Programming, for all ages and all languages.
Post Reply
User avatar
d4n1l0d
Member
Member
Posts: 42
Joined: Sat Dec 02, 2006 4:12 pm

Surprised with dynamic memory allocation on stack

Post 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?
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Surprised with dynamic memory allocation on stack

Post 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.
Every good solution is obvious once you've found it.
User avatar
d4n1l0d
Member
Member
Posts: 42
Joined: Sat Dec 02, 2006 4:12 pm

Re: Surprised with dynamic memory allocation on stack

Post by d4n1l0d »

Thanks a lot!
I'll take a look on that article!
Post Reply