Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Stack Memory Deallocation Problem

Reply
Thread Tools

Stack Memory Deallocation Problem

 
 
yccheok@gmail.com
Guest
Posts: n/a
 
      12-17-2004
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++;
}
}

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-17-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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).

> 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);

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


Drop the asterisk.

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


Replace -> with .

> }
>
> 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);

>
> // 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.

> i++;
> }
> }
>


V
 
Reply With Quote
 
 
 
 
Paavo Helde
Guest
Posts: n/a
 
      12-18-2004
(E-Mail Removed) wrote in news:1103294504.201173.195040
@f14g2000cwb.googlegroups.com:

> 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.


> #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;

> 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);


> };
>
> 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;


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


Regards
Paavo
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C/C++ compilers have one stack for local variables and return addresses and then another stack for array allocations on the stack. Casey Hawthorne C Programming 3 11-01-2009 08:23 PM
memory allocation deallocation ... pasa_1 C++ 19 11-02-2006 07:56 PM
std.textio, readline and memory deallocation Nicolas Matringe VHDL 9 09-04-2006 12:15 PM
memory allocation/deallocation question BigBrian C++ 12 04-07-2005 12:32 AM
Automatic memory deallocation by std::string ?? qazmlp C++ 1 03-07-2004 02:00 PM



Advertisments