是否有C或C ++初始化一个指针使用? [英] Is there a use for uninitialized pointers in C or C++?

查看:87
本文介绍了是否有C或C ++初始化一个指针使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在<一的评论之一href=\"http://stackoverflow.com/questions/1910832/c-why-arent-pointers-initialized-with-null-by-default\">this问题,它被运出来的默认初始化C ++指针将打破与C兼容性。

这很好,但为什么会是这样的事?我认为它实际上事情是,如果我的想要的只是时间的出于某种原因未初始化的指针。但我想不出一个原因,我想有。

是否有未初始化的指针一用?抑或是兼容性问题仅仅是兼容的行为(即不增加开销)之一,并没有违反code之一?


解决方案

这是一个非常特殊的情况下优化了视频游戏(基本上是嵌入式系统)。我们以前使用它们在我们的视频游戏加载就地数据的行为,加快加载(和避免碎片)。

基本上,我们会在PC电磁炉创建控制台端(游戏机)的对象。然后,以减少碎片超载,我们就收拾在一个连续的缓冲区中的数据对象与单一ALLOC。引用该缓冲区的数据对象然后将改为减去指针偏移基地(电话拔去 - 我们也有这花了缓冲基地,并可能偏移和指针之间进行转换的虚拟补丁/拔去电话)

当我们加载的数据,它装载在一个大的块。由根引用的所有数据都被关根对象。我们可以做一个就地上,将初始化适当的VF表的对象和修正内容所有附加块(做就地新建,然后分别向上固定连接块)的根新。

我们需要所谓的(替代新)生成的对象适当的VF-表的构造函数。但是,如果指针是在构造过程中自动清除为NULL,则我们将失去的偏移数据和不能够连续数据块内重建对象之间的指针。


FWIW,这是在视频游戏世界的常用技术。这 Gamasutra的文章(而不是由我或我的同事写的)详细介绍了他们在另一家公司做了类似的事情:

此外,中讨论<一这个话题href=\"http://sourceforge.net/search/index.php?group%5Fid=7932&form%5Fsubmit=Search&search%5Fsubject=1&type%5Fof%5Fsearch=mlists&all%5Fwords=&exact%5Fphrase=In-place+loaded+data+structures&some%5Fword=&without%5Fwords=&ml%5Fname%5B%5D=gdalgorithms-list&posted%5Fdate%5Fstart=&posted%5Fdate%5Fend=\">SourceForge.

有对这个问题甚至过几次GDC(游戏开发者大会)举行了会谈。

搜索在谷歌负荷就地将使用这种技术,基本是要求未初始化的指针给人们许多其他的例子。


请注意:目前,这是实际答案问的问题的唯一回应(?有没有用C或C ++未初始化的指针一个使用)通过给予特定的用途对于必须保持未初始化的指针

所有其它反应都为<一个更好的答案href=\"http://stackoverflow.com/questions/1910832/c-why-arent-pointers-initialized-with-null-by-default\">original问题引用([C ++]为什么不是默认初始化指针为NULL?)引起的海报问这个问题。

In one of the comments in this question, it was brought out that initializing C++ pointers by default would break compatibility with C.

That's fine, but why would something like this matter? I would think the only time it would actually matter is if I wanted an uninitialized pointer for some reason. But I can't think of a reason why I would want to have that.

Is there a use for uninitialized pointers? Or is the compatibility issue merely one of compatible behavior (i.e., not increasing overhead) and not one of breaking code?

解决方案

This is a very specialized optimized case for Video Games (basically an embedded system). We used to use them for Load-In-Place data behavior in our Video Games to speed up loading (and avoid fragmentation).

Basically we would create console-side (Playstation) objects in a PC cooker. Then to reduce fragmentation overload, we would pack the data objects in a contiguous buffer with a single alloc. References to the data objects in this buffer would then be changed to subtract the base from pointers to offsets (unfix call -- we also had a virtual fix / unfix calls that took the buffer base and could convert between offsets and pointers).

When we loaded the data, it loaded in one large block. All data referenced by the root was off the root object. We could do an inplace "new" on the the root that would initialize the proper VF tables for the object and fixup all the attached blocks (by doing inplace new and then fixing up attached blocks respectively).

We needed the constructors called (in place new) to generate the proper VF-Tables in the objects. However, if the pointers were automatically cleared to NULL during the constructor, we would have lost the offset data and not been able to recreate the pointers between the objects within the contiguous block.


FWIW, this is a common technique in the Video Game world. This Gamasutra article (not written by me or my coworkers) explains in detail the similar thing they did at another company:

Also, this topic of discussion on SourceForge.

There have even been several GDC (Game Developer Conference) talks on the subject.

Searching on Google for "load-in-place" will give many other examples of people using this technique that basically requires uninitialized pointers.


NOTE: Currently, this is the only response that actually answers the question asked ("Is there a use for uninitialized pointers in C or C++?") by giving a specific use for pointers that must remain unitialized.

All the other responses are better answers for the original question referenced ("[C++] Why aren’t pointers initialized with NULL by default?") that caused the poster to ask this question.

这篇关于是否有C或C ++初始化一个指针使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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