simple memory question

Discussion in 'C++' started by Shailesh Humbad, Jul 23, 2004.

  1. In the code below, I don't understand how push_front can make a deep
    copy of the locally declared dataElement object. I thought the
    default copy constructor only copies the members of the class, or is
    it just that the local dataElement object persists in the linked list
    even after addTo returns. Can someone explain why, or point to an
    appropriate section of the C++ faq/reference?

    -----------COMPLETE CODE-----------
    #include <iostream>
    #include <list>
    using namespace std;

    class dataElement {
    public:
    char * szM;
    };

    void addTo(list<class dataElement> &L) {
    class dataElement myD;
    myD.szM = (char*)malloc(sizeof(char)*3);
    myD.szM[0] = 'a';
    myD.szM[1] = 'b';
    myD.szM[2] = '\0';
    L.push_front(myD);
    }

    int main(int argc, char* argv[])
    {
    class dataElement deTemp;
    list<class dataElement> L;

    addTo(L);

    deTemp = L.front();

    cout << "output: " << deTemp.szM << "\n";
    // Prints "output: 100 ab"

    return 0;
    }
     
    Shailesh Humbad, Jul 23, 2004
    #1
    1. Advertising

  2. Shailesh Humbad

    Gryff Guest

    G'day,

    Shailesh Humbad wrote:

    > In the code below, I don't understand how push_front can make a deep
    > copy of the locally declared dataElement object. I thought the
    > default copy constructor only copies the members of the class, or is
    > it just that the local dataElement object persists in the linked list
    > even after addTo returns. Can someone explain why, or point to an
    > appropriate section of the C++ faq/reference?
    >
    > -----------COMPLETE CODE-----------
    > #include <iostream>
    > #include <list>
    > using namespace std;
    >
    > class dataElement {
    > public:
    > char * szM;


    If you really want to use char* - std::string would be better.

    > };
    >
    > void addTo(list<class dataElement> &L) {
    > class dataElement myD;


    The 'class' keyword is redundant here. Simply "list<dataElement>" and
    "dataElement myD"

    > myD.szM = (char*)malloc(sizeof(char)*3);


    Better to use new, ie "myD.szM = new char[3]"

    > myD.szM[0] = 'a';
    > myD.szM[1] = 'b';
    > myD.szM[2] = '\0';
    > L.push_front(myD);
    > }
    >
    > int main(int argc, char* argv[])
    > {
    > class dataElement deTemp;
    > list<class dataElement> L;
    >
    > addTo(L);
    >
    > deTemp = L.front();
    >
    > cout << "output: " << deTemp.szM << "\n";
    > // Prints "output: 100 ab"
    >
    > return 0;
    > }


    The push_front method is indeed making a shallow copy of your dataElement.
    The fact that the data persists after the addTo call is due to the memory
    allocated there not being freed up so the copy of the pointer in the list
    is still referring to a valid location.

    --
    Cheers,
    Gryff
     
    Gryff, Jul 23, 2004
    #2
    1. Advertising

  3. Gryff wrote:
    > G'day,
    >
    > Shailesh Humbad wrote:
    >
    >
    >>In the code below, I don't understand how push_front can make a deep
    >>copy of the locally declared dataElement object. I thought the
    >>default copy constructor only copies the members of the class, or is
    >>it just that the local dataElement object persists in the linked list
    >>even after addTo returns. Can someone explain why, or point to an
    >>appropriate section of the C++ faq/reference?
    >>
    >>-----------COMPLETE CODE-----------
    >>#include <iostream>
    >>#include <list>
    >>using namespace std;
    >>
    >>class dataElement {
    >>public:
    >>char * szM;

    >
    >
    > If you really want to use char* - std::string would be better.
    >
    >
    >>};
    >>
    >>void addTo(list<class dataElement> &L) {
    >>class dataElement myD;

    >
    >
    > The 'class' keyword is redundant here. Simply "list<dataElement>" and
    > "dataElement myD"
    >
    >
    >>myD.szM = (char*)malloc(sizeof(char)*3);

    >
    >
    > Better to use new, ie "myD.szM = new char[3]"
    >
    >
    >>myD.szM[0] = 'a';
    >>myD.szM[1] = 'b';
    >>myD.szM[2] = '\0';
    >>L.push_front(myD);
    >>}
    >>
    >>int main(int argc, char* argv[])
    >>{
    >>class dataElement deTemp;
    >>list<class dataElement> L;
    >>
    >>addTo(L);
    >>
    >>deTemp = L.front();
    >>
    >>cout << "output: " << deTemp.szM << "\n";
    >>// Prints "output: 100 ab"
    >>
    >>return 0;
    >>}

    >
    >
    > The push_front method is indeed making a shallow copy of your dataElement.
    > The fact that the data persists after the addTo call is due to the memory
    > allocated there not being freed up so the copy of the pointer in the list
    > is still referring to a valid location.
    >


    Thank you very much. I am slowly trying to transition my code and my
    mindset from C to C++.
     
    Shailesh Humbad, Jul 23, 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. Replies:
    0
    Views:
    585
  2. Kevin Spencer

    Re: Simple Simple question!!!

    Kevin Spencer, Jun 25, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    694
    Kevin Spencer
    Jun 25, 2004
  3. Daniel Frey

    Simple Question - Simple Answer?

    Daniel Frey, Dec 28, 2004, in forum: XML
    Replies:
    4
    Views:
    871
    Daniel Frey
    Jan 12, 2005
  4. jason
    Replies:
    8
    Views:
    305
    jason
    Jun 24, 2005
  5. Oli

    simple simple question

    Oli, Jan 26, 2004, in forum: ASP General
    Replies:
    10
    Views:
    390
    Roland Hall
    Jan 26, 2004
Loading...

Share This Page