0

When we declare an array in C...array is basically a pointer stored with base address of that array..

For ex:

int *x=malloc(5);

Gives memory for that array 'x' in Heap

Now,my question is that..where does memory will be allocated for this type of array declaration

int x1[5];

Assume that both 'x' &'x1' are declared in a function.

Where does 'x1' go in memory. Is it in stack? Or in Heap?

Nitesh_Adapa
  • 133
  • 7
  • 4
    For `x1` that really depends on where the definition is made. And also note that the C specification doesn't really mention anything about "the stack" or "a stack". That *local* variables *usually* end up on a stack is just an implementation detail. – Some programmer dude Nov 11 '17 at 05:35
  • 1
    Also note that if `x` is defined as a local variable in a function you have *two* allocations: One for the variable `x` itself and one for the memory allocated by the call to `malloc`. And talking about `malloc`, the C specification doesn't really say where or what kind of memory the "heap" is. – Some programmer dude Nov 11 '17 at 05:36
  • 'When we declare an array in C...array is basically a pointer stored with base address of that array' no. – Martin James Nov 11 '17 at 12:52

2 Answers2

0

If int x1[5]; doesn't defined in any function, the array x1 is on your program's bss segment , variable x1 is a global array.

If int x1[5]; defines in any function, the array x1 is on your program's stack during executing this function.

Ayra Faceless
  • 229
  • 1
  • 7
0

I believe int x1[5]; goes on the stack.

Tanner Babcock
  • 3,232
  • 6
  • 21
  • 23
  • 3
    The C standard does not mention a stack. A common way of implementing the behaviors that the C standard requires of objects with automatic storage durations is to use a stack, and so many objects will, if inspected with a debugger or otherwise, be found on a stack. But a C implementation may use other methods. For such a small array, it might keep it entirely in registers. – Eric Postpischil Nov 11 '17 at 05:47