一、New和Malloc的作用
New和Malloc都是動態內存分配的函數,可以在程序運行時為變數動態分配內存。
New是C++的標準運算符,它為對象動態分配內存,並調用構造函數初始化對象。在分配內存時,不需要指定內存空間大小,因為編譯器會通過類型推斷自動計算內存大小。
Malloc是C語言的標準函數,它允許程序在運行時為變數分配內存。在分配內存時需要指定內存空間大小,也就是分配的位元組數。
// New的示例代碼 #include using namespace std; int main() { int *p = new int; *p = 10; cout << *p << endl; delete p; return 0; } // Malloc的示例代碼 #include #include int main() { int *p = (int*)malloc(sizeof(int)); *p = 10; printf("%d\n", *p); free(p); return 0; }
二、New和Malloc的實現方式
New和Malloc的實現方式不同。
New是調用operator new函數來實現動態內存分配。當執行new操作時,編譯器首先調用operator new函數來分配內存,然後才調用構造函數來初始化對象。用戶可以重載operator new來改變內存分配策略。
Malloc是調用操作系統提供的動態內存分配函數來實現的。操作系統為Malloc分配的內存會放在內存堆中,由程序員負責管理。Malloc分配的內存空間可以通過free釋放掉。
// operator new的示例代碼 #include using namespace std; void* operator new(size_t n) { cout << "Allocating " << n << " bytes." << endl; void* p = malloc(n); return p; } void operator delete(void* p) { cout << "Deallocating memory." << endl; free(p); } class Student { public: Student() { cout << "Constructing Student object." << endl; } ~Student() { cout << "Destructing Student object." << endl; } }; int main() { Student* pStu = new Student; delete pStu; return 0; }
三、New和Malloc的錯誤處理
New和Malloc的錯誤處理方式也不同。
New在分配內存失敗時,會拋出std::bad_alloc異常。程序員可以通過捕獲這個異常來處理分配內存失敗的情況,也可以選擇不處理,讓程序崩潰。
而Malloc在分配內存失敗時,返回NULL指針。程序員需要在分配內存操作後檢查返回值是否為NULL,如果是,說明內存分配失敗。
// std::bad_alloc的示例代碼 #include #include using namespace std; int main() { try { int* p = new int[1000000000000000]; } catch (bad_alloc& e) { cout << "Allocation failed: " << e.what() << endl; } return 0; } // Malloc返回NULL指針的示例代碼 #include #include int main() { int *p = (int*)malloc(sizeof(int)*1000000000000000); if(p == NULL) { printf("Allocation failed\n"); exit(1); } free(p); return 0; }
四、New和Malloc的適用場景
New和Malloc適用於不同的場景。
New適用於C++中對象的動態分配,可以自動調用構造函數初始化對象。
而Malloc適用於C語言中變數的動態分配,不會調用構造函數,程序員需要手動初始化變數。Malloc還適用於內存池的實現,因為它可以在堆中分配一段連續的內存空間。
// 適用New的示例代碼 #include using namespace std; class Person { public: Person(int age) : m_age(age) { } void printAge() { cout << "Age: " << m_age <printAge(); delete p; return 0; } // 適用Malloc的示例代碼 #include #include struct Student { int id; int age; }; int main() { // 手動初始化 struct Student* pStu = (struct Student*)malloc(sizeof(struct Student)); pStu->id = 1; pStu->age = 20; printf("id: %d, age: %d\n", pStu->id, pStu->age); free(pStu); return 0; }
五、New和Malloc的內存對齊
New和Malloc在分配內存時都要考慮內存對齊的問題。
C++中的new運算符會保證分配的內存空間滿足所分配類型的對齊要求。例如,如果分配一個int類型的內存空間,那麼它的起始地址必定是4的倍數。
而C語言的malloc函數則需要手動指定分配的內存空間大小,也需要手動保證內存對齊。
// New的內存對齊示例代碼 #include using namespace std; class Student { public: Student(int age) : m_age(age) { } void printAge() { cout << "Age: " << m_age << endl; } private: int m_age; }; int main() { Student* p = new Student(20); cout << "sizeof(Student): " << sizeof(Student) << endl; cout << "p address: " << p << endl; delete p; return 0; } // Malloc的內存對齊示例代碼 #include #include int main() { // 分配8位元組內存,但只使用了4位元組 int* p = (int*)malloc(sizeof(int)); // 輸出指針地址,和所佔位元組數 printf("p address: %p, size: %u\n", p, sizeof(*p)); free(p); return 0; }
原創文章,作者:ELLAX,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/332798.html