Dynamically allocated memory storage clarification

This question already has an answer here:

  • Stack Memory vs Heap Memory [duplicate] 3 answers

  • I thought I'd make this an answer:

    Very simple:

  • If you use "new", the memory will be allocated from the heap.

  • "p" is a local variable. It's a pointer, and it's stored on the stack.

  • The 10,000 ints it points to were allocated by "new"; they're stored in the heap.


  • int* p = new int[100000];
    

    Will always allocate memory from the heap (correct term is dynamic storage). That's implied using new or new[] .

    Only the pointer variable itself will get static storage allocation outside of main() , local storage inside respectively.

    链接地址: http://www.djcxy.com/p/14478.html

    上一篇: 在c中的主函数中初始化的变量存储在哪里?

    下一篇: 动态分配内存存储说明