Need a short tutorial about create and delete objects.

Hello everybody:
I'm still a little confused with the create-delete steps for objects, vectors, etc.
I think every begginer has the same doubts, so the answers are going to be usefull.
Also to delete them I dont have clear when to use delete and when to assign 0 to the pointer.... And when to use free....

This are the questions:

Q1:
The objects, arrays, etc., I declare on the stack are automatically deleted?

For dynamic elements :

Q2:
For a class:
local var * myclass.
inside a method I have myclass = new Myclass();
Have I to delete it ? How ?

Q3:
For vectors: (and maps)
Somebody tells me that vectors store data on the heap. So can I declare them pacefully on the stack ???
I have : vector.pusback vector.pusback vector.pusback ....
When it won't be used nevermore, have I to delete or clear the vector ?

Q4:
For arrays :
int *my_int; my_int = new int[200];
Have I to delete it ? How ?

Q5:
For strings
char * my_char; my_char="hello, how are you";
Have I to delete it ? How ?


Any help would be appreciated.
Thanks




Last edited on
1: Yes. Objects created with new must be deleted using delete when they are no longer needed.
2: If it's a class member, in the destructor.
3: Yes, you can create them on the stack. The vector deletes all its elements when it is destroyed.
4: delete[] my_int;
5: Storage of string literals is handled automatically. You're not allowed to delete[] them.
Topic archived. No new replies allowed.