0

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?

kuafu
  • 1,466
  • 5
  • 17
  • 28
  • 3
    In one you're declaring an array and not initialising it. In the other you're declaring a pointer and initialising it with a pointer to the first element of a dynamically allocated array. – Mankarse May 24 '13 at 12:01
  • 5
    [Get a good book.](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – BoBTFish May 24 '13 at 12:01
  • http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap you can find an answer here – Dariusz May 24 '13 at 12:02
  • `char list[20]` will be located on the stack. Using `new` will locate it on the heap. – Xale May 24 '13 at 12:03
  • so the only difference is the location of memory.right?I should manually delete the alloc memory,the usage is the same. – kuafu May 24 '13 at 12:06
  • I've seen this puzzle before – Salgar May 24 '13 at 12:23

3 Answers3

1

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).

Liviu
  • 1,859
  • 2
  • 22
  • 48
  • If the array is declared as a global variable, it's not on the stack nor the heap, but in the data/bss area loaded with the program. – Some programmer dude May 24 '13 at 12:03
  • Your answer only scratches the top of the differences between the two declarations. – Dariusz May 24 '13 at 12:04
  • @JoachimPileborg actually the second case would still be on the heap. And yeah, there are many more differences. For example, sizeof will be different for the second. – Dave May 24 '13 at 12:05
1

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.

Perry Horwich
  • 2,798
  • 3
  • 23
  • 51
1

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.

Agentlien
  • 4,996
  • 1
  • 16
  • 27