C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Stack Memory Deallocation Problem

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
yccheok@gmail.com
Guest





PostPosted: Fri Dec 17, 2004 2:41 pm    Post subject: Stack Memory Deallocation Problem Reply with quote



Hi, i am from java background and c++ is quite new for me. Consider the
following code:

i obtain an warning from my compiler which states:
a.cpp:45: warning: taking address of temporary

it seems that i am facing problem in accessing an object which has
already been deallocated in stack memory.

unliked java, the object will not be allocated as long as there is a
reference pointing to it.

in c++ case, is there any way to solve this problem without using
dynamic memory allocation (new and delete)?

thank you very much!

-cheok

#include <iostream>

using namespace std;

class a {
int name;

public:
a(int);
~a();
int getName();
a createA(int);
};

a::a(int value) {
name = value;
cout<< "a named "<< name<< " is created"<< endl;
}

a::~a() {
cout<< "a named "<< name<< " is DELETED"<< endl;
}

int a::getName() {
return name;
}


a a::createA(int i) {
a aa(i);
return aa;
}

int main() {
int i=0;
a solidA(100);
a* tmp;

while(true) {
if(i > 0) {
cout<< "am i access 'a' which has been
deleted?? "<< tmp->getName()<< endl;
}

cout<< "creating 'a' and preparing to assign it to a
reference..."<< endl;
tmp = &(solidA.createA(i));

// tmp is now holding 'a' which already been deleted in
stack memory.
i++;
}
}

Back to top
Victor Bazarov
Guest





PostPosted: Fri Dec 17, 2004 2:55 pm    Post subject: Re: Stack Memory Deallocation Problem Reply with quote



[email]yccheok (AT) gmail (DOT) com[/email] wrote:
Quote:
Hi, i am from java background and c++ is quite new for me. Consider the
following code:

i obtain an warning from my compiler which states:
a.cpp:45: warning: taking address of temporary

it seems that i am facing problem in accessing an object which has
already been deallocated in stack memory.

unliked java, the object will not be allocated as long as there is a
reference pointing to it.

in c++ case, is there any way to solve this problem without using
dynamic memory allocation (new and delete)?

Yes, don't use the pointer, use an object (just like you would in Java).

Quote:
thank you very much!

-cheok

#include <iostream

using namespace std;

class a {
int name;

public:
a(int);
~a();
int getName();
a createA(int);
};

a::a(int value) {
name = value;
cout<< "a named "<< name<< " is created"<< endl;
}

a::~a() {
cout<< "a named "<< name<< " is DELETED"<< endl;
}

int a::getName() {
return name;
}


a a::createA(int i) {
a aa(i);
return aa;

The two lines can be simply merged resulting in

return a(i);

Quote:
}

int main() {
int i=0;
a solidA(100);
a* tmp;

Drop the asterisk.

Quote:

while(true) {
if(i > 0) {
cout<< "am i access 'a' which has been
deleted?? "<< tmp->getName()<< endl;

Replace -> with .

Quote:
}

cout<< "creating 'a' and preparing to assign it to a
reference..."<< endl;
tmp = &(solidA.createA(i));

There is no "reference" here to assign to. Straighten up your messages.

What happens here is simple: you're returning a value from a function.
It's what is known as "rvalue". First of all, you're not supposed to be
able to take an address of it. Second of all, the temporary that you try
to take the address of, only lives until the end of the full expression
(in this case until the semicolon).

Just do

tmp = solidA.createA(i);

Quote:

// tmp is now holding 'a' which already been deleted in
stack memory.

Yes, but if you switch to an object instead, everything is going to work
just fine.

Quote:
i++;
}
}


V

Back to top
Paavo Helde
Guest





PostPosted: Sat Dec 18, 2004 2:35 pm    Post subject: Re: Stack Memory Deallocation Problem Reply with quote



[email]yccheok (AT) gmail (DOT) com[/email] wrote in news:1103294504.201173.195040
@f14g2000cwb.googlegroups.com:

Quote:
Hi, i am from java background and c++ is quite new for me. Consider the
following code:

i obtain an warning from my compiler which states:
a.cpp:45: warning: taking address of temporary

it seems that i am facing problem in accessing an object which has
already been deallocated in stack memory.

You are guessing right.


Quote:
#include <iostream

using namespace std;

class a {
int name;

public:
a(int);
~a();
int getName();

This should be marked 'const' as it doesn't alter object state:

int getName() const;

Quote:
a createA(int);

A factory function is commonly declared static, so you don't need an
existing object for calling the function:

static a CreateA(int);


Quote:
};

a::a(int value) {
name = value;
cout<< "a named "<< name<< " is created"<< endl;
}

a::~a() {
cout<< "a named "<< name<< " is DELETED"<< endl;
}

int a::getName() {
return name;
}


a a::createA(int i) {
a aa(i);
return aa;
}

int main() {
int i=0;
a solidA(100);
a* tmp;

while(true) {
if(i > 0) {
cout<< "am i access 'a' which has been
deleted?? "<< tmp->getName()<< endl;
}

cout<< "creating 'a' and preparing to assign it to a
reference..."<< endl;
tmp = &(solidA.createA(i));

Yes, the temporary object returned by createA() is destroyed at the
semicolon.

You have two possibilities (apart of dynamic allocation): prolong the
temporary lifetime by binding it to a const reference:

const a& tmp = solidA.createA(i);
// temporary lifetime prolonged while 'tmp' is in scope.
// note: const keyword is essential here, and that's an additional
// reason why the getName() member fn should be const.

or alternatively, store the temporary object in some buffer:

a buffer;
a* tmp;
// ...
buffer = solidA.createA(i);
tmp = &buffer;


Quote:

// tmp is now holding 'a' which already been deleted in
stack memory.
i++;
}
}



Regards
Paavo

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.