Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic memory allocation on stack

I recently tried this experiment in which instead of going for dynamic memory allocation for memory requirements of unknown size, I did a static allocation. When an array a[i] was declared by me, I kept i (size of the array) variable and dependent on the input that the user gives.

 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <conio.h>
 void function(int );
 int main(void)
 {
     int i;
     printf("Enter:");
     scanf("%d",&i);
     function(i);
     printf("i = %d\n",i);
     getch();
     return 0;
 }
 void function(int i)
 {
      char a[i];
      char b[4];
      strncpy(a,"hello",i);
      strcpy(b,"world");
      int j = 0;
      char *c = a;
      for( j = 0; j< 20; j++ )
           printf("%c",*c++);
 }

My questions are:

  • Is such an operation legal?
  • If no, why does the compiler not issue any warning or error?
  • Where will this memory be allocated: Stack or heap?
  • Why does ANSI C/GCC allow this?
like image 656
Saurabh Gandhi Avatar asked Oct 31 '09 08:10

Saurabh Gandhi


People also ask

Why can't we just do dynamic allocation within the stack?

Why can't we allocate dynamic memory (i.e. memory of size that is only known at runtime) on the stack? It's more complicated to achieve this. The size of each stack frame is burned-in to your compiled program as a consequence of the sort of instructions the finished executable needs to contain in order to work.

How is memory allocated in stack?

Stack Allocation: The allocation happens on contiguous blocks of memory. We call it a stack memory allocation because the allocation happens in the function call stack. The size of memory to be allocated is known to the compiler and whenever a function is called, its variables get memory allocated on the stack.

Is stack dynamic or heap?

While a stack is used mainly for static memory allocation, a heap is used for dynamic memory allocation. One of the things stack and heap have in common is that they are both stored in a computer's RAM.

What is stack dynamic storage?

A STACK-DYNAMIC VARIABLE is bound when the declaration statement is executed, and it is deallocated when the procedure returns. Memory for a stack-dynamic variable is allocated from the run-time stack.


3 Answers

Is such an operation legal?

It's called a variable length array.

VLAs are legal in ANSI C99 and as an extension to some pre-C99 compilers. GCC supports it both as strict C99 and as an extension to non-C99 code. It's also legal in C++0x.

If no, why does the compiler not issue any warning or error?

With gcc:

$ gcc -std=c89 src/vla.c  -Wall -ansi -pedantic
src/vla.c: In function ‘function’:, not dynamic array.
src/vla.c:17: warning: ISO C90 forbids variable length array ‘a’
src/vla.c:21: warning: ISO C90 forbids mixed declarations and code

The presence of 'conio.h' from MSDOS indicates you're probably using a Microsoft Visual C++ compiler, so don't worry about it. MS has worked to make their compiler more conformant to the C++0x standard, but makes no claims about how standard its C compiler mode is. You're asking why Spanish dialect words aren't in the French dictionary.

Where will this memory be allocated: Stack or heap?

It is an automatic object, so most C implementations will put in on the stack for efficiency reasons.

Why does ANSI C/GCC allow this

It is useful for creating temporary arrays of variable size at runtime whose lifetime doesn't extend beyond the function call.

like image 150
Pete Kirkham Avatar answered Sep 20 '22 10:09

Pete Kirkham


This is valid C99.

Look here for a more detailed explanation in another StackOverflow question.

like image 39
lx. Avatar answered Sep 19 '22 10:09

lx.


This is legal, but not all compilers support it. At least Visual Studio <= 2003 afaik do not support it.

I would assume it is not Ansi C++, try gcc -ansi -pedantic.

like image 20
ypnos Avatar answered Sep 20 '22 10:09

ypnos