Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > simple memory question

Reply
Thread Tools

simple memory question

 
 
Shailesh Humbad
Guest
Posts: n/a
 
      07-22-2004
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;
}
 
Reply With Quote
 
 
 
 
Gryff
Guest
Posts: n/a
 
      07-23-2004
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

 
Reply With Quote
 
 
 
 
Shailesh Humbad
Guest
Posts: n/a
 
      07-23-2004
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++.


 
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
Simple region code question... simple answer?? joseph.greer@gmail.com DVD Video 7 01-26-2007 09:07 PM
Simple Question - Simple Answer? Daniel Frey XML 4 01-12-2005 04:25 PM
Differences between Sony Memory Stick & memory Stick Pro vs Memory Stick Duo? zxcvar Digital Photography 3 11-28-2004 10:48 PM
Re: Simple Simple question!!! Kevin Spencer ASP .Net 0 06-25-2004 05:25 PM
Re: Simple Simple question!!! ashelley@inlandkwpp.com ASP .Net 0 06-25-2004 04:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57