C中的“没有声明类型的已分配对象”的C ++等价物是什么? [英] What is the C++ equivalent of an 'allocated object having no declared type' in C?

查看:92
本文介绍了C中的“没有声明类型的已分配对象”的C ++等价物是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++中为我的VM写了一个内存管理器。嗯,更准确地说,VM指令将被嵌入式内存管理器编译成C ++。我更容易处理C,但现在我需要本机支持异常处理,几乎是我使用C ++的唯一原因。

I'm writing a memory manager for my VM in C++. Well, more exactly the VM instructions will be compiled into C++ with an embedded memory manager. I'm much more comfortable in handling C, but now I do need native support of exception handling, pretty much the only reason I'm using C++.

C和C ++有严格的别名规则,两个不兼容类型的对象不会重叠,在C中有一个小的异常。但是要定义内存分配函数的行为,例如 malloc calloc alloca 等,C标准有以下段落。

Both C and C++ have the strict aliasing rule that two objects of incompatible types shall not overlap, with a small exception in C for unions. But to define the behaviour of memory allocation functions such as malloc, calloc, alloca etc., the C standard has the following paragraph.


6.5-6 访问其存储值的对象的类型是对象的声明类型(如果有)。分配的对象
没有声明类型。如果一个值存储到没有
声明类型的对象中,通过类型不是字符
类型的左值,则左值的类型成为
对象的有效类型对于该访问和对不修改
的后续访问存储的值。如果使用 memcpy memmove 将值复制到没有声明类型的对象中,或者复制为数组字符类型,则该访问的修改对象的有效类型和不修改该值的后续访问的有效类型是该值被复制的对象的有效类型(如果有的话)。对于没有声明类型的对象的所有其他访问,对象的有效类型只是用于访问的左值类型。

6.5-6 The effective type of an object for an access to its stored value is the declared type of the object, if any. Allocated objects have no declared type. If a value is stored into an object having no declared type through an lvalue having a type that is not a character type, then the type of the lvalue becomes the effective type of the object for that access and for subsequent accesses that do not modify the stored value. If a value is copied into an object having no declared type using memcpy or memmove, or is copied as an array of character type, then the effective type of the modified object for that access and for subsequent accesses that do not modify the value is the effective type of the object from which the value is copied, if it has one. For all other accesses to an object having no declared type, the effective type of the object is simply the type of the lvalue used for the access.

这有效地使用任何类型的原始分配的内存在C中良好定义的行为。我试图在C ++标准文档中找到一个类似的段落,但是找不到一个。我认为C ++在这方面有一个不同的方法。在C中,一个没有声明类型的分配对象的C ++等价物是什么,C ++标准如何定义它?

This effectively makes using raw allocated memory for any type a well defined behaviour in C. I tried to find a similar paragraph in the C++ standard document but could not find one. I think C++ has a different approach in this regard. What is the C++ equivalent of an 'allocated object having no declared type' in C, and how does the C++ standard define it?

推荐答案

我认为,C ++在这方面的方法可以总结为: malloc()是邪恶的使用 new 。没有这样的东西,没有声明类型的对象。当然,C ++实现需要定义全局运算符new(),它基本上是的C ++版本malloc()(并且其可以由用户提供)。这个操作符的存在证明了在C ++中没有声明类型的对象,但是标准不会承认它。

I think, the approach of C++ in this regard can be summarized as: "malloc() is evil. Use new instead. There is no such thing as an object without a declared type." Of course, C++ implementations need to define the global operator new(), which is basically the C++ version of malloc() (and which can be provided by the user). The very existence of this operator proves that there is something like objects without a declared type in C++, but the standard won't admit it.

如果我是你,我采取务实的做法。全局运算符new() malloc()在C ++中可用,因此任何实现必须能够使用他们的回报价值明智。特别是 malloc()将在C和C ++中表现相同。所以,只需处理这些无类型的对象,就像在C中处理它们一样,你应该很好。

If I were you, I'd take the pragmatic approach. Both the global operator new() and malloc() are available in C++, so any implementation must be able to use their return values sensibly. Especially malloc() will behave identical in C and C++. So, just handle these untyped objects the same way as you would handle them in C, and you should be fine.

这篇关于C中的“没有声明类型的已分配对象”的C ++等价物是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆