0
点赞
收藏
分享

微信扫一扫

(3)深度学习学习笔记-简单线性模型

自由的美人鱼 2023-06-29 阅读 71

Lesson5–C/C++内存管理

【本节目标】

  • 1.C/C++内存分布
  • 2.C语言中动态内存管理方式
  • 3.C++中动态内存管理方式
  • 4.operator new与operator delete函数
  • 5.new和delete的实现原理
  • 6.定位new表达式
  • 7.常见面试题

C/C++内存分布

先看一段C语言代码和相关问题

int globalVar = 1;
static int staticGlobalVar = 1;
void Test()
{
 static int staticVar = 1;
 int localVar = 1;
 
 int num1[10] = {1, 2, 3, 4};
 char char2[] = "abcd";
 char* pChar3 = "abcd";
 int* ptr1 = (int*)malloc(sizeof (int)*4);
 int* ptr2 = (int*)calloc(4, sizeof(int));
 int* ptr3 = (int*)realloc(ptr2, sizeof(int)*4);
 free (ptr1);
 free (ptr3);
}
1. 选择题:
 选项: A.栈 B.堆 C.数据段 D.代码段
 globalVar在哪里?__C__ staticGlobalVar在哪里?__C__
 staticVar在哪里?__C__ localVar在哪里?__A__
 num1 在哪里?__A__
 
 char2在哪里?__A__ *char2在哪里?__A__
 pChar3在哪里?__A__ *pChar3在哪里?__D__
 ptr1在哪里?__A__ *ptr1在哪里?__B__
2. 填空题:
 sizeof(num1) = __40__;
 sizeof(char2) = __5__; strlen(char2) = __4__;
 sizeof(pChar3) = __4__; strlen(pChar3) = __4__;
 sizeof(ptr1) = __4__;

 char2局部变量在栈区  

 char2是一个数组,把后面常量串拷贝过来到数组中,数组在栈上,所以*char2在栈上



  pChar3局部变量在栈区   *pChar3得到的是字符串常量字符在代码段

  ptr1局部变量在栈区     *ptr1得到的是动态申请空间的数据在堆区

C语言中动态内存管理方式(malloc/calloc/realloc/free)

void Test ()
{
 int* p1 = (int*) malloc(sizeof(int));
 free(p1);
 
 // 1.malloc/calloc/realloc的区别是什么?
 int* p2 = (int*)calloc(4, sizeof (int));
 int* p3 = (int*)realloc(p2, sizeof(int)*10);
 
 // 这里需要free(p2)吗?
 free(p3 );
}

一般来说这里是不用free的,因为p3realloc的原空间地址就是p2realloc有两种实现方式,就是当p2后面的空间还足够的时候会在原地扩容,反之就会在其他一个地方找一块空间然后把p2的内容拷贝进去,但是不会freep2,所以最好还是free一下比较好

【面试题】

malloc/calloc/realloc

C++中内存管理的方式

new/delete操作内置类型
void test()
{
	int* ptr = new int;//动态申请一个int类型的空间

	int* ptr1 = new int(10);//动态申请一个int类型的空间并且初始化为10

	int* ptr2 = new int[3];//动态申请3个int类型的空间
	
	delete ptr;
	delete ptr1;
	delete[] ptr2;
}

new/delete操作自定义类型
class Test
{
public:
	Test()
		: _data(0)
	{
		cout << "Test():" << this << endl;
	}
	~Test()
	{
		cout << "~Test():" << this << endl;
	}

private:
	int _data;
};

void Test2()
{
	// 申请单个Test类型的对象
	Test* p1 = new Test;
	delete p1;

	// 申请10个Test类型的对象
	Test* p2 = new Test[10];
	delete[] p2;
}

运行结果:

operator new和operator delete函数

/*
operator new:该函数实际通过malloc来申请空间,当malloc申请空间成功时直接返回;申请空间失败,
尝试执行空 间不足应对措施,如果改应对措施用户设置了,则继续申请,否则抛异常。
*/
void *__CRTDECL operator new(size_t size) _THROW1(_STD bad_alloc)
{
 // try to allocate size bytes
 void *p;
 while ((p = malloc(size)) == 0)
 if (_callnewh(size) == 0)
 {
 // report no memory
 // 如果申请内存失败了,这里会抛出bad_alloc 类型异常
 static const std::bad_alloc nomem;
 _RAISE(nomem);
 }
 return (p);
}
/*
operator delete: 该函数最终是通过free来释放空间的
*/
void operator delete(void *pUserData)
{
 _CrtMemBlockHeader * pHead;
 RTCCALLBACK(_RTC_Free_hook, (pUserData, 0));
 if (pUserData == NULL)
 return;
 _mlock(_HEAP_LOCK); /* block other threads */
 __TRY
 /* get a pointer to memory block header */
 pHead = pHdr(pUserData);
 /* verify block type */
 _ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));
 _free_dbg( pUserData, pHead->nBlockUse );
 __FINALLY
 _munlock(_HEAP_LOCK); /* release other threads */
 __END_TRY_FINALLY
 return;
}
/*
free的实现
*/
#define free(p) _free_dbg(p, _NORMAL_BLOCK)

通过以上代码可以知道operator new实际也是通过malloc来申请的空间

operator new与operator delete的类专属重载

下面代码演示了,针对链表的节点ListNode通过重载类专属 operator new/ operator delete,实现链表节

点使用内存池申请和释放内存,提高效率。

struct ListNode
{
 	ListNode* _next;
 	ListNode* _prev;
 	int _data;
 	void* operator new(size_t n)
 	{
 	void* p = nullptr;
 	p = allocator<ListNode>().allocate(1);
 	cout << "memory pool allocate" << endl;
 	return p;
	}
 void operator delete(void* p)
 {
 	allocator<ListNode>().deallocate((ListNode*)p, 1);
 	cout << "memory pool deallocate" << endl;
 }
};
class List
{
public:
 List()
 {
 	_head = new ListNode;
 	_head->_next = _head;
 	_head->_prev = _head;
 }
 ~List()
 {
 	ListNode* cur = _head->_next;
 	while (cur != _head)
 {
 	ListNode* next = cur->_next;
 	delete cur;
 	cur = next;
 }
 	delete _head;
 	_head = nullptr;
 }
private:
 ListNode* _head;
};
int main()
{
 	List l;
 	return 0;
}

new和delete的实现原理

内置类型
自定义类型

定位new表达式是(placement-new)

	A* a = new A;
	new(a)A;//显示地调用构造函数,如果A构造函数有参数的话需要传参
	a->~A();//显示地调用析构函数

常见面试题

malloc/free和new/delete的区别
内存泄漏

什么是内存泄漏,内存泄漏的危害是什么?

void MemoryLeaks()
 {
 	// 1.内存申请了忘记释放
 	int* p1 = (int*)malloc(sizeof(int));
 	int* p2 = new int;
 
 	// 2.异常安全问题
 	int* p3 = new int[10];
 
 	Func(); // 这里Func函数抛异常导致 delete[] p3未执行,p3没被释放.
 
 	delete[] p3;
 }
内存泄漏的分类
举报

相关推荐

0 条评论