Is it on the Stack or Heap?

馋奶兔 提交于 2019-12-04 07:12:30

Leaving global variables and variables declared with static modifier (which are allocated in a separate memory region) aside, local variables declared in a function body are allocated on the stack whereas whatever you call malloc for is allocated on the heap. Also, C99 variable sized arrays and memory allocated with _alloca will end up on stack.

int* x[10];   // The addresses are held on the stack
int i;        // On the stack
for(i = 0; i < 10; ++i)
   x[i] = malloc(sizeof(int)*10);  // Allocates memory on the heap

For example, in the above code, there's an array of int* values on the stack holding addresses to 10 different locations in the heap.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!