使用C ++中的new创建参考

编程入门 行业动态 更新时间:2024-10-26 14:37:37
本文介绍了使用C ++中的new创建参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下代码,似乎可以用new创建引用,但是,当涉及到使用new创建对象时,当我尝试重新分配分配的内存时会崩溃

I have the following code and it seems that to create a reference with new is okay, however when it comes to creating an object with new, it crashes when i try to recollect the memory allocated

float &f = *new float(1.3); delete &f; float f1 = *new float; delete &f1;

我想知道两者之间的区别,谢谢!

I'd like to know the difference, thanks !

推荐答案

首先,使​​用new创建引用"并不是您要做的.要理解这一点,需要对代码进行分解...

Firstly, "creating a reference with new" isn't exactly what you're doing. To understand this, the code needs to be broken up...

new float(1.3);

这会为浮点数分配4个字节的内存,使用1.3个double常量构造浮点,并返回一个指向内存开头的指针-4个字节中的第一个.

this allocates 4 bytes of memory for a float, constructs the float using a 1.3 double constant, and returns a pointer to the start of the memory - the first of the 4 bytes.

*new float(1.3);

此操作将取消引用"新操作符返回的指针,因此,您无需读取指针值,而是读取它指向的4字节 float 数据.

this "dereferences" the pointer returned by the new operator, so instead of reading the pointer value, you're reading the 4-byte float data it points to.

float &f = *new float(1.3);

使用new提供的 float 构造引用f.尽管(AFAIK)引用被实现为具有更大潜力进行编译器优化(?)的指针,但这是人们抱怨的一部分.从概念上讲,引用是浮点数,您不应该假设可以检索地址,释放内存并因此在以后使引用无效.

constructs a reference f, using the float provided by new. While (AFAIK) references are implemented as pointers with greater potential for compiler optimizations (?), this is one part people are complaining about. Conceptually, the reference is the float and you shouldn't be assuming you can retrieve the address, free the memory and hence invalidate the reference later.

但是,正如您所发现的,它确实可以工作,您可以检索地址...

However, as you have found, it does actually work and you can retrieve the address...

&f

在引用的位置生成指向内存的指针,然后您就可以

produces a pointer to the memory at the reference's location, which you can then

delete &f;

要回答您的实际问题...

To answer your actual question...

float f1 = *new float;

可以重写如下

float f1; //a float on the stack! f1 = *new float; //should probably be: *new float(1.3)

这不会初始化引用,而是复制由new分配给堆栈中浮点数的数据.复制后, new 返回的内存地址将永远丢失-您已经泄漏"了内存.

which does not initialize a reference, but instead copies the data allocated by new to the float on the stack. After the copy, the memory address returned by new is lost forever - you have "leaked" memory.

现在继续说明崩溃的原因.

Now on to why it crashes.

&f1;

在堆栈上创建一个指向浮点数 f1 的指针.尚未由 new 创建此内存,或者new最终使用了该内存分配库.试图释放内存分配库中不存在的内存地址...

creates a pointer to the float f1, on the stack. This memory has not been created by new or the memory allocation library new ultimately uses. Attempting to free a memory address that doesn't exist in the memory allocation library...

delete &f1;

...导致崩溃.

float& f 和 float f1 之间的差异:一个是引用,在内部实现为指针(假定从不进行更改),其中之一是在堆栈上声明的实际float变量,该变量不引用其他内存.

The difference between float &f and float f1: one is a reference, internally implemented as a pointer (which is assumed never to change) and one is an actual float variable declared on the stack which does not reference other memory.

您应该做的是

float *f = new float(1.3f); // access f as *f = 1.2f or cout << *f delete f;

更多推荐

使用C ++中的new创建参考

本文发布于:2023-11-05 19:22:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561641.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!