Declaring with new & delete

Apr 9, 2012 at 2:35pm
Hi there, all things declared with new must be deleted? For example in a function i have a char declared with new, after function ends things declared in it aren't automatically destroyed? Or that char remains in memory/heap and must be deleted with delete[] ?
Apr 9, 2012 at 3:07pm
it's a rule of thumb in C++. Every new has to have a delete. But new operator can only be used on pointers, so it doesn't make sense to delete non-pointer variables. Here's an example:

1
2
3
4
5
6
7
8
9

char mychar = 'x'; //this is not pointer, and doesn't need neither new nor delete, it's just a single character on the stack

char* myword = "Hello man, I love C++"; //this is a pointer, and it was implicitly initialised with on the heap, so you have to delete it, it's a NULL terminated string. Btw, this implicit conversion is deprecated, and the compiler will most likely issue a warning about it. 

char* myword2 = new char[50]; //here you implicitly reserved 50 characters for your word, and you have to delete it when you're done. To be able to use this with C-string algorithms, you have to terminate your words with a NULL

delete[] myword;
delete[] myword2;


So: whenever you have a * or pointer which is initialised, then you have to delete it.
Apr 9, 2012 at 3:10pm
For example in a function i have a char declared with new, after function ends things declared in it aren't automatically destroyed?


No, you have a char-pointer declared locally, and you set it equal to a pointer returned by new. When the function ends, that local char-pointer is destroyed. All the memory it pointed to, that was given to you by new, is still allocated and not touched.

Edit: Nin-JAR!
Last edited on Apr 9, 2012 at 3:28pm
Apr 9, 2012 at 3:23pm
char* myword = "Hello man, I love C++"; //this is a pointer, and it was implicitly initialised with on the heap, so you have to delete it
No, you cannot delete that. no new[] - no delete[], simple.
Apr 9, 2012 at 4:24pm
Ok, i have asked because that function is repeated every x seconds where x >0, isn't this dangerous with new / delete[] ? Can delete[] cause crashes by running in some memory problems?
Also i have a pointer to a struct declared with new, this must be also deleted? If yes instead of delete[] i must use just delete right?
Last edited on Apr 9, 2012 at 4:27pm
Apr 9, 2012 at 4:29pm
Ok, i have asked because that function is repeated every x seconds where x >0, isn't this dangerous with new / delete[] ?


No. It's dangerous if you forget to delete things, or if you delete something and then try to use it again, or if you mix new/delete[] or new[]/delete, but just using new and using delete is not dangerous.

Can delete[] cause crashes by running in some memory problems?

Only if you get it wrong and try to delete[] something that you allocated with new, or delete[] something that has already been deleted, or delete[] something that was never allocated with new[]. If you don't get it wrong, then it's fine.
Apr 9, 2012 at 5:00pm
Ok thanks, so:
1. char *c = new char[10] this must be deleted with delete[] c
2. byte *b = new byte(10) this must be deleted with delete b
3. test_struct *ts = new test_struct where test_struct is a struct must be deleted with delete ts
1,2,3 am I right?
Last edited on Apr 9, 2012 at 5:01pm
Apr 9, 2012 at 5:12pm
Yup. New byte(10) almost got me, until I realized curved brackets :S
Also, remember that whenever you delete something, the destructor of that object is called.
If you're allocating dynamic memory inside a class remember to always delete it in the destructor.
Topic archived. No new replies allowed.