Stack Memory Deallocation Problem

Discussion in 'C++' started by yccheok@gmail.com, Dec 17, 2004.

  1. Guest

    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++;
    }
    }
    , Dec 17, 2004
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Dec 17, 2004
    #2
    1. Advertising

  3. Paavo Helde Guest

    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
    Paavo Helde, Dec 18, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qazmlp
    Replies:
    1
    Views:
    733
    John Harrison
    Mar 7, 2004
  2. BigBrian
    Replies:
    12
    Views:
    579
    Pete Becker
    Apr 7, 2005
  3. Nicolas Matringe

    std.textio, readline and memory deallocation

    Nicolas Matringe, Sep 1, 2006, in forum: VHDL
    Replies:
    9
    Views:
    2,053
    Paul Uiterlinden
    Sep 4, 2006
  4. pasa_1
    Replies:
    19
    Views:
    509
    David Harmon
    Nov 2, 2006
  5. madhu
    Replies:
    6
    Views:
    624
    madhu
    Nov 13, 2006
Loading...

Share This Page