Jaegool_'s log

Algorithm and Data Structure Study notes, Sep [Dynamic memory arrangements] 본문

Courses

Algorithm and Data Structure Study notes, Sep [Dynamic memory arrangements]

Jaegool 2023. 9. 14. 10:24

Memory allocation, C++

 

When a variable is declared compiler automatically allocates memory for it. This is known as compile-time memory allocation or static memory allocation. Memory can be allocated for data variables after the program begins execution. This mechanism is known as runtime memory allocation or dynamic memory allocation.

 

// static memory allocation: int a;
// dynamic memory allocation: allocating memory for variable while running time.

#include <iostream>

using namespace std;

int main() {
	int *a = new int(5);
    
    cout << a << endl; // address
    cout << *a << endl; // value
    
    *a = 10;
    
    cout << a << endl;
    cout << *a << endl;
    
    delete a; // deallocate memory for a C++ class object
}

 

// DMA for array

#include <iostream>

using namespace std;

int main() {
	int *arr;
    int len;
    
    cout << "enter the length of the array: ";
    cin >> len;
    
    arr = new int[len];
    
    for (int i = 0; i < len; i++) {
    	arr[i] = len - i;
    }
    for (int i = 0; i < len; i++) {
    	cout << arr[i] << endl;
    }
    
    // delete arr; // it delete only for arr[0]
    
    delete[] arr;
    
}

 

// Copy

int main() {
	int *a = new int(5);
    int *b = new int(3);
    
    // shallow copy: copy only reference
    // a = b; // a(address) point to b(address). address of a is lost 

	// deep copy: copy values
	*a = *b;
    
    delete a;
    delete b;
}

 

 

// Copy constructor

#include <iostream>

using namespace std;

class String {
public:
	String() {
    	cout << "constructor call String()" << end;
    	strData = NULL;
        len = 0;
    }
    String(const char *str){ // 'str' is the pointer to point 'const char'
    	// strData = str; // shallow don't do this.
        cout << "String(const char*) constructor call" << endl;
        len = strlen(str);
        strData = new char[len + 1]; // every string has a '\0' (null char)in their last. so, len+1.
        cout << "strData 할당: " << (void*)strData << endl;
        strcpy(strData, str); // Deep copy
    }
    String(const String &rhs){ // copy constructor
    	// strData = rhs.strData; // shallow
        cout << "String(String &rhs) 생성자 호출" << endl;
        strData = new char[rhs.len + 1];
        cout << "strData 할당 : " << (void*)strData << endl;
        strcpy(strData, rhs.strData); // deep copy
        len = rhs.len; // it can be deep copy b/c len is int from seperate object
    }
    
    ~String(){
    	cout <<"~String() destructor" << endl;
    	delete[] strData;
        cout << "strData is deallocated : " <<(void*)strData << endl;
        strData = NULL;
    }
    
    char *GetStrData() const{
    	return strData;
    }
    
    int GetLen() const {
    	return len;
    }

private:
	char *strData;
    int len;
};

int main() {
	String s1("Hello");
	String s2(s1); // 복사 생성자
    String s3 = s1;
    
    cout << s1.GetStrData() << endl;
    cout << s2.GetStrData() << endl;
}

 

Necessary Methods for management of dynamic memory
The destructor

The copy constructor

The assignment operator( the copy assignment operator)

 

 

Destructors

// Destructor

~Classname()
{
	delete dynamicData;
}

Called when the object is destroyed.

The deletion code needs to correspond to the dynamic data.

 

 

Copy constructors

// Passing as parameter:
vector<MyClass> myCollection;
MyClass myObject;
myCollection.push_back(myObject);

// Explicit copy constructor call:
MyClass myObject = new MyClass(myOldObject);

// Declaration-initialization:
MyClass myObject = myOldObject;

 

Correct copy constructor

// Needs to allocate the memory and copy ALL of the data
className::className(const ClassName& orig)
{
	this->staticData = orig.staticData;
    this->allocatedData = new type;
    *(this->allocatedData) = *(orig.allocatedData);
}

 

Helper methods

Create a private copy method.

The copy constructor contains only a call to that method.

Create a private clear/destroy method (containing exactly the code needed in the destructor)

Destructor contains a call to that method.

 

Recap

Need a destructor that frees all dynamically allocated memory the object controls.

Need a copy constructor that allocates the dynamic memory and copies all data.

Need an assignment operator that does both (unless self-assignment.)

 

 

 

References: examples are from Youtube(두들낙서) & IT 279 at Illinois State University