0

May be the question is very simple, but I would like to know what exactly happens when I do this:

int arr[10];

The more specific question: I know this is a static memory allocation, but where is it monitored which part of memory is occupied? thank you.

Igor
  • 1
  • That's for the compiler to know and you to not care about. A small array like that will likely be just added to the enclosing function's stack frame, but the compiler is free to put it anywhere it wants to. – Lee Daniel Crocker Mar 24 '15 at 21:24
  • It's in the same memory region as `int arr;` would be in the same context; but 10 times as much space. – M.M Mar 24 '15 at 21:39

4 Answers4

1

I'll address normal practice, not theory--

It depends upon where you put the

int arr[10];

If you put put within a function, that will result in something like:

SUB #40, SP

Subtracting 40 (assuming 4 byte integer) from the Stack Pointer register.

&arr then == SP

That assumes arr is the only variable allocation. Normally, all allocations are done in one step. The resulting variables are then offsets from the stack pointer.

If put the definition outside a function, the compiler and linker will create a program section for demand zero memory pages. When the program runs, the loader sets up the page and the first reference triggers a page fault that the OS will respond to by creating a zeroed out memory page.

user3344003
  • 20,574
  • 3
  • 26
  • 62
  • You could add that likewise all stack deallocations by a function are done in a single step, either by reincrementing the stack pointer by the same amount, or by loading a saved stack pointer from the stack. Which of the two is used depends on machine architecture. – cmaster - reinstate monica Mar 24 '15 at 21:58
0

If inside of a function, this will be allocated on the current stack frame, so that it can be reclaimed when that function returns.

If declared globally, it is allocated in the heap, and need not be "monitored".

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • A static array won't ever be allocated on the heap. That's for dynamic allocations via `malloc()` only. Static arrays go into the static section defined by the executable itself. – cmaster - reinstate monica Mar 24 '15 at 21:52
0

The memory in that case is allocated in the stack memory. The stack memory works, as its name suggests, as a stack: last-in, first-out.

For ex: When you call a function, the function frame goes to the top of the execution and is allocated in the stack of the frame the memory that you need. When you exit the function all the content related to the function (including your stack variables) is destroyed. That's why if you try to access an address of a stack variable outside its function you'll get garbage.

You can see more details in the wikipedia article about Stack-based memory allocation: http://en.wikipedia.org/wiki/Stack-based_memory_allocation

dfranca
  • 5,156
  • 2
  • 32
  • 60
0

The variable arr should be allocated in stack as if you put this line in a function. If you put this line in a global area, this variable will be in data segment in heap but it is still not as same as normal heap. The normal heap means we use new/malloc to allocate memory.

Here is example to show how to allocate memory in normal heap:

int *pInt = (int *)malloc(10*sizeof(int));