problem with "delete"

hi,
i need to ask a general question about the delete:

for example,if i have implemented a class name_class

class name_class{
....
...
};

once i call it in the main an object of class_name type
is it right to get rid of it in the following way?
int main(){
class_name obg1;
....
..
class_name *ptr=&obj1;
delete ptr;
return 0;
}

No.

You only delete things that you have new'd.

Since you did not create the object with new, (in this case, the object is 'obj1' -- which was created with local scope... not with new)... you must not delete it.
Last edited on
No. If you didn't create obg1 on the heap, there's no need to delete it. Attempting to do so will most likely cause a crash.

Edit: Ninja'd by Disch.
Last edited on
He's a pretty fast ninja if he beat you by a whole 15 minutes!

EDIT: @Zexd below: The question was already answered by Disch, who posted a full 24 minutes ago! There's no need to post an additional post to potentially cause confusion with conflicting information.
Last edited on
delete is used to de-allocate the memory that is allocated with new.

Here is an example:
1
2
int *pointer = new int; // int is here just to show you what it does. You'll use much bigger structs or arrays
delete pointer; // de-allocating the memory allocated with new 
Last edited on
He's a pretty fast ninja if he beat you by a whole 15 minutes!

Well, I started writing my reply before Disch's got posted, and then got distracted by actual, y'know, work. Then I came back to it and finished it, forgetting to check whether someone else had answered in the meantime
Topic archived. No new replies allowed.