为什么要使用指针进行低内存分配?

时间:2022-09-06 15:19:15

So I was looking at the source code of unreal engine and I came across this:

所以我正在查看虚幻引擎的源代码,我遇到了这个:

class USphereComponent* CollectionSphere;

There are using pointer for something that is going to be initialized and spawned once when the game begins and some objects will be overlapping it,I thought pointers are mainly used for more mid-high memory allocation purposes So why are they using it now?

在游戏开始时有一些指针用于初始化和生成的东西,并且一些对象将重叠它,我认为指针主要用于更高的中高内存分配目的为什么他们现在使用它?

Keep in mind Im not really good at pointers so Im just trying learn and gather some information about it.

请记住我不是很擅长指针,所以我只是想学习并收集一些有关它的信息。

1 个解决方案

#1


3  

Whether to use pointers or not has almost nothing to do with "low" or "mid-high memory allocation purposes".

是否使用指针几乎与“低”或“中高内存分配目的”无关。

You use a pointer when you need a pointer, and often that's when the pointee (the thing being pointed to) will live for longer than an "automatic" object would in any of the program's existing, conveniently-accessible scopes.

当你需要一个指针时,你会使用一个指针,通常情况下,指针对象(被指向的东西)将比任何程序现有的,方便访问的范围中的“自动”对象存活的时间更长。

Basically, *CollectionSphere will be a global (or something similar), but we can't construct it right away. The author has decided that they need fine control over the lifetime of the object. So, they have created a pointer to point to that object when the time comes.

基本上,* CollectionSphere将是一个全局(或类似的东西),但我们无法立即构建它。作者已经决定他们需要对对象的生命周期进行精细控制。因此,他们在时机成熟时创建了一个指向该对象的指针。

#1


3  

Whether to use pointers or not has almost nothing to do with "low" or "mid-high memory allocation purposes".

是否使用指针几乎与“低”或“中高内存分配目的”无关。

You use a pointer when you need a pointer, and often that's when the pointee (the thing being pointed to) will live for longer than an "automatic" object would in any of the program's existing, conveniently-accessible scopes.

当你需要一个指针时,你会使用一个指针,通常情况下,指针对象(被指向的东西)将比任何程序现有的,方便访问的范围中的“自动”对象存活的时间更长。

Basically, *CollectionSphere will be a global (or something similar), but we can't construct it right away. The author has decided that they need fine control over the lifetime of the object. So, they have created a pointer to point to that object when the time comes.

基本上,* CollectionSphere将是一个全局(或类似的东西),但我们无法立即构建它。作者已经决定他们需要对对象的生命周期进行精细控制。因此,他们在时机成熟时创建了一个指向该对象的指针。