kuafu
kuafu

Reputation: 1486

memory alloc puzzle in array declare

In cpp,I can declare a arry as :

char list[20];

it will alloc 20 from memory,we can confirm it using sizeof

and also we can declare

char *list = new char[20];

so what's the difference between the two declare?

Upvotes: 0

Views: 152

Answers (3)

Agentlien
Agentlien

Reputation: 5126

The main difference between the two options is the storage class of your array. This has two interesting effects.

  1. Where in memory the object is allocated
  2. Lifetime of the allocated object

Let's look at the two options and compare.

char list[20];

With this option, the storage class of list depends on where it is declared. In either case, the lifetime of list ends at the end of the scope where it is declared. At that point, the memory will be automatically freed. Until then, the memory belongs to the list object and any pointers or references to this memory stay valid. You could also put this declaration inside a class definition, in which case the lifetime of list would coincide with that of the class instance it is bound to.

char *list = new char[20];

Given type char*, list is simply a pointer to a character in memory. As this is stated (using new in the initialization), that character will be the first in an array (of type char[20]) allocated on the free store. Again, the lifetime of list is the same as above. And list itself will be destroyed when it goes out of scope (or the class instance it is part of is destroyed). However, in this case list is only the name of the pointer, and does not refer to the array itself. This array has dynamic storage and will remain allocated until you manually state that it should be freed. This can be done like this:

delete[] list;

Note the [], which are required because list is allocated as an array (using "array new"). If you do not delete[] the list, you will leak memory, which is bad since it means the system has less useable memory available for you (and other processes) until your program terminates. To avoid having to remember this, you should usually rely on smart pointers such as shared_ptr and unique_ptr.

The main reason one would use dynamic storage is if you are allocating something which (in your use case) is too large for automatic storage, or you need the object to outlive the context in which it is declared. The downside is that dynamic memory allocation is much more expensive (takes a lot of time) than your other alternatives.

Upvotes: 1

Liviu
Liviu

Reputation: 1917

The main difference is the memory where the array resides: the first one is on the stack (or in the data segment for globals), the second one is on the heap. Being on the heap means you're responsible of the allocated memory (you have to 'delete' it somewhere).

Upvotes: 1

Perry Horwich
Perry Horwich

Reputation: 2846

1) The space for char list[20]; will live on the stack.
2) The space for char *list = new char[20]; will live in the heap. This can then be released with the 'delete' operator. Method 1 cannot.
The allocated space for method 1 will become available again when the function in which it is declared returns.

Upvotes: 1

Related Questions