Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   simple memory question (http://www.velocityreviews.com/forums/t284549-simple-memory-question.html)

Shailesh Humbad 07-22-2004 11:36 PM

simple memory question
 
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;
}

Gryff 07-23-2004 01:07 AM

Re: simple memory question
 
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


Shailesh Humbad 07-23-2004 04:59 AM

Re: simple memory question
 
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++.




All times are GMT. The time now is 02:10 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.