Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Deleting an object
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
boogerlad




PostPosted: Mon Dec 12, 2011 10:56 pm   Post subject: Deleting an object

code:
int x = 1;
vector<int> * herp = new vector<int>;
vector<int> derp;
delete x;
delete herp;
delete derp;


The program runs and compiles, but I get a warning
warning: the address of 'derp' will never be NULL

What does that warning mean? It just doesn't make any sense to me. If I can delete x, then why can't I delete derp without the warning?
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Mon Dec 12, 2011 11:18 pm   Post subject: RE:Deleting an object

it doesn't make sense to delete items on the stack. You can't delete x either.
Quote:

tony$ cat test.cc
#include <new>
int main(){
int x = 1;
delete x;
return 0;
}

tony$ g++ test.cc
test.cc: In function ?int main()?:
test.cc:4: error: type ?int? argument given to ?delete?, expected pointer
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
boogerlad




PostPosted: Tue Dec 13, 2011 12:06 am   Post subject: Re: Deleting an object

sorry, I meant this:
code:

    delete &derp;
    delete &x;

deletion of x doesn't result in a warning, but derp does.
Tony




PostPosted: Tue Dec 13, 2011 12:15 am   Post subject: RE:Deleting an object

No. You cannot delete objects from within the stack, even if you try. If you have to ask why, then you need to review how stack memory works.

Edit: it might not give you a compile-time exception (static analysis is hard), but it will break at run-time.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
md




PostPosted: Tue Dec 13, 2011 12:17 am   Post subject: RE:Deleting an object

Why you get that specific warning I'm not sure but really doing things like that *will* cause trouble.
boogerlad




PostPosted: Tue Dec 13, 2011 12:21 am   Post subject: RE:Deleting an object

Fair enough. That might explain those random crashes. Sorry for the noob questions, I just started learning c++. I just came from java. Deleting the herp object is okay, because I used the new keyword correct? Anything that uses the new key word must be deleted manually?
md




PostPosted: Tue Dec 13, 2011 2:07 am   Post subject: Re: RE:Deleting an object

boogerlad @ 2011-12-13, 12:21 am wrote:
Fair enough. That might explain those random crashes. Sorry for the noob questions, I just started learning c++. I just came from java. Deleting the herp object is okay, because I used the new keyword correct? Anything that uses the new key word must be deleted manually?


In a word, yes. Herp can be deleted because it was allocated from the heap (hence why you have a pointer to it). Derp and x are both allocated on the stack. In Java *every* object is allocated from the heap, only references and basic numeric types go on the stack; hence why you *always* use new to create new objects. With C++ you can create objects by allocating memory from the heap with new *or* you can create objects on the stack by simply declaring a variable. Things on the stack get automatically destroyed when the stack frame closes (not always the end of a function), things allocated from the heap must be deleted manually.

In your original post what you were doing was attempting to delete stack-allocated objects from the heap - which clearly makes no sense. In fact, it's an undefined operation IIRC and might do any number of things depending on the specific compiler you're using.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: