Dynamic memory Management (reference count)

Source: Internet
Author: User

C + + dynamic memory management is very important, improper operation can easily cause memory leaks,

Below I have detailed some memory management where this attention is made, including the implementation of reference counting


Deep copy Shallow copy

#include <iostream>
using namespace Std;

Class String
{
Public

String ()
: _str (new char[1])
{
*_str = ' + ';
}

String (char* str)
: _str (new Char[strlen (str) +1])//open up a new space for _STR
{
strcpy (_str, str);
}

The above two constructors can be used to synthesize one of the following
String (char* str= "")
: _str (new Char[strlen (str) + 1])//open up a new space for _STR
{
strcpy (_str, str);
}

String (const string& s)//copy Construction
: _str (New Char[strlen (S._STR) + 1])//open up a new space for _str, i.e. deep copy, so that they point to different spaces
{
strcpy (_str, S._STR);
}

string& operator= (const string& s)
{
if (This! = &s)
{
Delete[] _str;//Here must note that it is very easy to have a memory leak, because the original S3 there is a space, when assigned
Make the S3 point back to a space, the original space is leaked.
_str = new Char[strlen (S._STR) + 1];
strcpy (_str, S._STR);
}

return *this;
}

~string ()
{
if (_STR)
{
DELETE[]_STR;
}
}

char* Getstr ()
{
return _str;
}

char& operator[] (size_t index)//change string contents
{
return _str[index];
}

Private
Char *_str;
};

void Test1 ()
{
char* p1 = "ABCD";
String S1 (p1);
cout << S1. Getstr () << Endl;

S1[0] = ' x ';
cout << S1. Getstr () << Endl;

Shallow copy
String S2 (S1);//If you do not write your own copy constructor, using the system default copy construction, is a value copy, S1 and S2 point to the same piece of space, when the destruction
Can be refactored two times, causing a crash

String S3 ("Efgh");
s3 = S1; Overloaded assignment operator required

s3 = S3;
}

int main ()
{
Test1 ();
return 0;
}


#include <iostream>
using namespace Std;

Class String
{
Public
String (char* str = "")
: _str (new Char[strlen (str) + 1])
{
strcpy (_str, str);
}

String (const string& s)
: _str (NULL)
{
String tmp (S._STR);
Swap (_STR, TMP._STR);
}

string& operator= (string& s)
//{
if (This! = &s)
// {
String TMP (s);
Swap (_STR, TMP._STR);
// }

return *this;
//}

string& operator= (String s)
{
Swap (_STR, S._STR);

return *this;
}

~string ()
{
if (_STR)
{
Delete[] _STR;
}
}
Private
char* _str;
};

void Test1 ()
{
char* p1 = "ABCD";
String S1 (p1);

String S2 (S1);

String S3 ("Efgh");
s3 = S1;

s3 = S3;
}

int main ()
{
Test1 ();
return 0;
}


Reference count related actions

#include <iostream>
#include <windows.h>
using namespace Std;

Class String
{
Public
String (char* str = "")
: _str (new Char[strlen (str) + 1])
, _pcount (new int (1))
{
strcpy (_str, str);
}

String (const string& s)
: _str (S._STR)
, _pcount (S._pcount)
{
(*_pcount) + +;
}

string& operator= (const string& s)
{
if (This! = &s)
{
This->_release ();

_str = S._str;
_pcount = S._pcount;
(*_pcount) + +;
}
return *this;
}

~string ()
{
_release ();
}

Private
void _release ()
{
if (--(*_pcount) = = 0)
{
Delete _pcount;
Delete[] _STR;
}
}

Private
char* _str;
int* _pcount; Pointer to reference count
};


Dynamic memory Management (reference count)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.