简体   繁体   English

C ++中的内存分配和回收

[英]memory allocation and reclaim in C++

I am new to c++ and just read some online tutorial about it. 我是c ++的新手,只是阅读了一些有关它的在线教程。 I am pretty curious about my experiment result in pointer. 我对指针的实验结果非常好奇。 Here is my code: 这是我的代码:

int *p_value;
sizeof(*p_value) <--- I expected 0 but it shows 4
p_value = new int;
sizeof(*p_value) <--- it shows 8, make sense
delete(p_value)
sizeof(*p_value) <--- I expect 0, but it shows 4 again...

More importantly, I don't understand why does p_value point to the same memory address after delete operation. 更重要的是,我不理解为什么删除操作后p_value指向相同的内存地址。 Is it necessary to be true? 是否必须真实?


Thanks for pointing out my misunderstanding over the operator "sizeof". 感谢您指出我对运算符“ sizeof”的误解。 But here is something I didn't state it too clear in my question. 但是,在我的问题中,我没有明确指出这一点。

here is another code: int *p_value = new int; 这是另一个代码:int * p_value = new int; cout << p_value <-- it prints the address of the new created int, say 0x000A; cout << p_value <-打印新创建的int的地址,例如0x000A; delete p_value; 删除p_value; cout << p_value <-- it shows the same address before the "delete" called cout << p_value <-它在“删除”之前显示相同的地址

Can i consider the "delete" just mark the 0x000A is not occupied in Map, I guess. 我可以认为“删除”只是将0x000A标记为未在Map中占用。 But it won't change the content of the p_value? 但这不会改变p_value的内容吗?

Your results cannot be true. 您的结果可能不正确。 All your cases output size of type int in bytes (and sizeof(int) cannot be changed due program run). 您的所有情况下的输出size类型的intbytes (和sizeof(int)不能改变,由于运行的程序)。

I think the key thing your missing is that sizeof is not a function. 我认为您缺少的关键是sizeof不是函数。 It doesn't look at the value of its argument at run time like a function does, just its type at compile time. 它不会像函数那样在运行时查看其参数的 ,而只是在编译时查看其类型

int *p_value;   
sizeof(*p_value) <--- I expected 0 but it shows 4

Since p_value is a pointer to an integer, *p_value is an integer. 由于p_value是指向整数的指针,因此*p_value是整数。 Integer are four bytes on your platform. 整数是平台上的四个字节。

p_value = new int;   
sizeof(*p_value) <--- it shows 8, make sense

That's odd. 真奇怪 Since p_value is a pointer to an integer, *p_value is an integer. 由于p_value是指向整数的指针,因此*p_value是整数。 So apparently, integers are 8 bytes on your platform. 因此,显然,平台上的整数是8个字节。

delete(p_value)  
sizeof(*p_value) <--- I expect 0, but it shows 4 again...

Why do you expect 0? 你为什么期望0? Since p_value is a pointer to an integer, p_value is an integer. 由于p_value是指向整数的指针,因此p_value是整数。 So its size is 4 (or however many bytes an integer occupies on your platform). 因此,它的大小为4(或平台上占用多个字节)。 How else would p_value = malloc (sizeof (*p_value)); p_value = malloc (sizeof (*p_value));将会如何p_value = malloc (sizeof (*p_value)); work? 工作?

more importantly, I don't understand why p_value point to the same memory address after delete operation. 更重要的是,我不理解为什么删除操作后p_value指向相同的内存地址。 Is it necessary to be true? 是否必须真实?

Passing the value of a variable to a function doesn't change the value of that variable. 将变量的值传递给函数不会更改该变量的值。 So delete (p_value) doesn't change the value of p_value , except it now points to garbage. 因此delete (p_value)不会更改p_value的值,只是它现在指向垃圾。

All of the results returned by the above sizeof(* ptr) give you information about the size of the type it points to, specific to your platform , specifically the size of int , ie 4 means size: 4 bytes or 32 bits, thus the int can hold up to 2 32 - 1-sign-bit different numbers. 上述sizeof(* ptr)返回的所有结果都为您提供了有关它指向的类型的大小的信息, 具体取决于您的平台 ,特别是int的大小,即4表示size:4个字节或32位,因此int最多可以容纳2 32-1个符号位的不同数字。

You could think of the returned results as results from: sizeof(int) , which won't change regardless of the location* of your int of whether you free dynamically allocated memory, with delete or not, fact that, as @ForEveR, already said, makes your results dubious**. 您可以将返回的结果视为来自以下内容的结果: sizeof(int) ,无论您的int位置如何,*是否释放动态分配的内存(是否delete不会改变,事实上,作为@ForEveR说,让您的结果令人怀疑**。


*Static, stack or heap memory. *静态,堆栈或堆内存。

**Checked on two different platforms and the result is 4 , not 8 . **在两个不同的平台上检查,结果为4 ,而不是8

int *p_value;
sizeof(*p_value) <--- I expected 0 but it shows 4

*p_value is an integer, the size is typically 4 bytes, thus it shows 4. This does depends on the compiler too (C++ standard only mentioned that it should be at least 4 bytes). *p_value是一个整数,大小通常为4个字节,因此显示为4。这也取决于编译器(C ++标准仅提及它应至少为4个字节)。 My test returns 8 though. 我的测试虽然返回8。

p_value = new int;
sizeof(*p_value) <--- it shows 8, make sense

If the above shows 4, then this one should shows 4 as well (unless you're using different compiler/target arch). 如果上面显示4,则该位置也应显示4(除非您使用其他编译器/目标架构)。 The sizeof operator shows the number of bytes taken by the data type. sizeof运算符显示数据类型占用的字节数。 The value itself is irrevalent. 该值本身是不等价的。

delete(p_value)
sizeof(*p_value) <--- I expect 0, but it shows 4 again...

The same as above. 与上述相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM