일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- Binary Tree
- binary search tree
- 데이터 엔지니어
- Computer Organization
- hash
- HEAPS
- priority queue
- 데이터 분석가
- Data Engineer
- Heap
- algorithm
- exam
- data scientist
- Linked List
- Computer Science
- Preparing for the Google Cloud Professional Data Engineer Exam
- Restroom
- Newyork
- 뉴욕 화장실
- 빅데이터
- Data Structure
- Study
- BST
- Algorithms
- dataStructure
- Data Analyst
- 빅데이터 지식
- 빅데이터 커리어 가이드북
- 화장실 지도
- data
- Today
- Total
Jaegool_'s log
Algorithm and Data Structure Study notes, Sep [Dynamic memory arrangements] 본문
Algorithm and Data Structure Study notes, Sep [Dynamic memory arrangements]
Jaegool 2023. 9. 14. 10:24Memory 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
'Courses' 카테고리의 다른 글
IT 279: Algorithms and Data Structures, Week 1 recap (0) | 2023.10.02 |
---|---|
IT 495 - Client meeting [Pixel Bit Road: funding platform for games] (0) | 2023.09.25 |
Computer Organization: CH7. I/O Systems (0) | 2023.04.26 |
Computer Organization ; Data Representation recap (0) | 2023.02.23 |
Planning for a Successful Study in the USA (0) | 2023.02.08 |