Simple question: do I have to 'delete' or 'delete []' c? Does the language matter?
char c[] = "hello"
Simple question: do I have to 'delete' or 'delete []' c? Does the language matter?
char c[] = "hello"
In c++ that is not dynamic memory allocation. No delete[]
will be needed.
Your example is basically a short-cut for this:
char c[6]={'h','e','l','l','o','\0'};
The rule in C++ is that you use delete[]
whenever you use new[]
, and delete
whenever you use new
. If you don't use new
, as in your example, you do not need to delete
anything.
In your example, the six bytes for the c
array are allocated on the stack, instead of on the heap, if declared within a function. Since those bytes are on the stack, they vanish as soon as the function where they are declared returns.
If that declaration is outside any function, then those six bytes are allocated in the global data area and stay around for the whole lifetime of your program.
you dynamically allocate memory when you put something on the heap. here, you are allocating the variable on the stack. If you were using the new operator or the malloc call, you'd be putting the variable on the heap.
you need to use delete (w/new) or free (w/malloc) to free the memory on the heap. the stack will be freed automatically when the function/method returns.