simple memory question

S

Shailesh Humbad

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

Gryff

G'day,

Shailesh said:
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;

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

Shailesh Humbad

Gryff said:
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++.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,225
Latest member
Top Crypto Podcasts

Latest Threads

Top