2

I was amazed to see that this code is working. I couldn't figure out why

#include<stdio.h>
int main(){
  int row,col,i,j;
  scanf("%d %d",&row,&col);
  int a[row][col];
  for(i=0;i<row;i++)
      for(j=0;j<col;j++)
           scanf("%d",&a[i][j]);
   for(i=0;i<row;i++){
      for(j=0;j<col;j++)
           printf("%d ",a[i][j]);
       printf("\n");
   }
}

Since C is a compiled language then How it is allocating memory for the array a[row][col] ? Since at the time of compilation the value of row and column are not known, then how it is able to make machine code and set the address space for the program? Why this is working as an interpreter language would have worked, If this is a way to create a dynamic array then why are we taught to use malloc for creating dynamic array in C.

haccks
  • 104,019
  • 25
  • 176
  • 264
ASEN
  • 167
  • 1
  • 10

2 Answers2

3

Variable-length arrays have been a standard feature of C since C99.

How it is allocating memory for the array a[row][col]

Once the value of row and col is known, there is no problem for the compiled code to make this allocation in the automatic storage area (commonly referred to as "the stack").

how it is able to make machine code and set the address space for the program?

The compiler squirrels away the values of row and col for internal use by machine code that it generates. Instructions that reference a[i][j] look up the sizes, do the math, add the offset, and get the address, as if row and col were known at compile time.

The feature does change a few other things - for example, sizeof is no longer a purely compile-time operation, because the size of VLAs need to be computed at run-time.

why are we taught to use malloc for creating dynamic array in C.

malloc gives you more flexibility than a VLA. For example, you can return a malloc-created array from your function, while VLA gets automatically deallocated as soon as the function finishes. In addition, malloc is less likely to run your program out of memory, because dynamic storage area is more generously sized than the automatic one.

For example, if you try to enter 2000 2000 for your program, it is likely going to crash. Yet it would have no problem allocating the same amount of memory with malloc:

int (*a)[col] = malloc(row*col*sizeof(int));
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

That's feature of C99. The reason to use malloc anyway is that it allocates space not on the stack, but the heap - and the heap is where the 'heavy' data should be stored, as the stack space is severely limited. More than that; failure to do a stack allocation typically crashes the program - while allocating with malloc returns a NULL pointer, which allows you to proceed in elegant fashion.

TNW
  • 716
  • 6
  • 15
  • No this feature is with earlier version of C too. – ASEN Jan 24 '16 at 11:57
  • 1
    There is no mention in the C standard of heap or stack (at least, not in the context of being distinct areas of memory), let alone any requirement VLAs be allocated in the stack – Peter Jan 24 '16 at 11:57
  • 4
    @ASEN - the first C standard which supported VLAs was the 1999 C standard. Some implementations supported such things as an extension before then. – Peter Jan 24 '16 at 11:58
  • Thanks Peter I didn't knew about it I was using C89 and it worked so i said – ASEN Jan 24 '16 at 12:11