DEV Community

yeonseong
yeonseong

Posted on

Memory layout in C++

automatic storage

  • exists till the function ends.** -> only useable in the scope
  • automatic variable is variable used in function.
  • allocated in stack -> last-in, first-out ## example in automatic storage
int main()
{
    char temp[42]; // this is automatic variable
    return (0);
}

Enter fullscreen mode Exit fullscreen mode

static storage

  • exists till program ends.
  • allocated in data

example in static storage

int main()
{
    static char temp[42]; // this is static variable
    return (0);
}

Enter fullscreen mode Exit fullscreen mode

dynamic storage

  • exits till delete is called.
  • new and delete operators manage memory poll (a.k.a free store)
  • allocated in heap
  • depend to programmer.
    • WATCH MEMORY LEAKS
      • recommend : locate new and delete close.

example in dymaic storage

int main()
{
    int *ptr = new int(42);
    delete ptr;
    return (0);
}

Enter fullscreen mode Exit fullscreen mode

reference

Top comments (0)