Clarification on stack & heap

I have read that stacks are using to manage function calls, where as heaps are used to store the objects that are allocating. What I understood that non primitive type objects will be stored in heap. I am confused on the following usages in the case of stack & heap:

1) Where the primitive data types, MACROS(#define), static objects, const, and extern?
2) Stacks manages the function calls, but I would like to know what info related to the functions are pushing to the stack?
3) I read somewhere that function related local objects are stored in Stack. So if any allocation of objects happens inside function, stack or heap is used? And if any primitive types objects are declared inside the function, where those are storing?

Sree.


  • Macros are resolved at compile time. Consts, globals, etc are part of the data segment - not the stack or the heap. https://en.wikipedia.org/wiki/Data_segment

  • Whenever you call a function it's parameters are pushed onto the stack.

  • All primitives, local variables, etc inside a function are allocated on the stack.

  • In simplified terms the compiler calculates how much memory each function needs (the total of all its variable usage). When the function is called that size is simply added to the stack and then it is subtracted in when it is finished.

    At the most primitive level the only time heap memory is used (excluding libraries) is when you call malloc .

    In the case of Objective-C pretty much every object is allocated dynamically on the heap whenever you call alloc or new . Objective-C is designed that way and heap allocation is normal. C and C++ tend to use the dynamic allocation on the heap less.


    To understand what's really happening, you should build a small c program and generate assembly code of it.

    What you will find there is the following:

  • Macros should be evaluated at compile time, not at runtime.
  • Constants, globals and static things are declared as constants and saved into the executable itself.
  • Primitive data variables or pointers are stored into CPU registers (extremely fast but limited in quantity) or stored in a stack frame in memory (more space but ca. 1000x slower). To understand stack frames, you should have a look at this explanation. Basically a stack frame is built by moving the stack pointer (which points to the memory location where new values are put onto the stack) down (the stack grows from big memory addresses to smaller ones) so there is some unused space on the stack, which can be locally used during a function.
  • If you call a function, the return address (the instruction pointer before the call + 1) is pushed onto the stack so when the function returns, the execution jumps to the return address by popping the return address from the stack and jumping to it.
  • If your function has a lot of arguments, the arguments 7, 8, etc... are stored on the stack before the function is called. All previous arguments are stored in registers
  • Unlike the stack, the heap space is assigned by the system and can only be accessed (to my knowledge) by interrupting the program and letting the operating system assign memory to the program (which will happen inside a malloc call). As objects are allocated ( [NSObject alloc] ) they can be found in the heap memory.
  • Summary

  • Primitive values and structs are stored next to return addresses of function calls on the stack
  • large memory allocations above a few bytes are made in heap space. This includes objects and arrays created with malloc.
  • 链接地址: http://www.djcxy.com/p/82882.html

    上一篇: Permgen是否是堆的一部分?

    下一篇: 澄清堆栈和堆