C++ dynamically allocated memory

前端 未结 8 1917
难免孤独
难免孤独 2020-12-10 16:16

I don\'t quite get the point of dynamically allocated memory and I am hoping you guys can make things clearer for me.

First of all, every time we allocate memory we

8条回答
  •  悲&欢浪女
    2020-12-10 16:56

    An object created like this:

    int foo;
    

    has automatic storage duration - the object lives until the variable foo goes out of scope. This means that in your first example, dynInt will be an invalid pointer once someInt goes out of scope (for example, at the end of a function).

    An object created like this:

    int foo* = new int;
    

    Has dynamic storage duration - the object lives until you explicitly call delete on it.

    Initialization of the objects is an orthogonal concept; it is not directly related to which type of storage-duration you use. See here for more information on initialization.

提交回复
热议问题