简体   繁体   English

初始化为NULL的指针数组的成员会占用内存吗?

[英]do members of an array of pointers initialized to NULL take up memory?

I use SDL_Surface pointers, and I'm wondering if this: 我使用SDL_Surface指针,我想知道这是否:

SDL_Surface* Images[8][7];

where some of the individual surface pointers are initialized to NULL in its class constructor and then never used, will take up more memory than if those were kept separate. 其中一些单独的表面指针在其类构造函数中初始化为NULL,然后从未使用过,将占用的内存比单独保存的内存多。 As you can imagine, using an enumeration would make accessing these surface pointers easier and require no if or switch statements. 可以想象,使用枚举可以更容易地访问这些表面指针,并且不需要if或switch语句。 For example, Images[0][3] to Images[0][7] may all be NULL, NULL, etc. Thanks in advance for replies! 例如,Images [0] [3]到Images [0] [7]可能都是NULL,NULL等。预先感谢回复!

Edit: "kept separate" meaning have a pointer variable name and a smaller array to avoid having NULL as a value for some of the images of which there are less than 7. 编辑:“保持独立”意味着有一个指针变量名称和一个较小的数组,以避免将NULL作为一些小于7的图像的值。

Addendum: Thanks everyone for the lightning replies, it's clear to me now; 附录:感谢大家的闪电回复,现在我很清楚; I'll go forward with my 2-dimensional array and there will be other arrays as well for other image groups. 我将继续使用我的二维数组,其他图像组也会有其他数组。

Do members of an array of pointers initialized to NULL take up memory? 初始化为NULL的指针数组的成员会占用内存吗?

A pointer is a type which occupies enough memory to store an address location. 指针是占用足够内存以存储地址位置的类型。
So Yes they will occupy/consume memory once they are declared just like any other data type which occupies memory irrespective of whether its variable is initialized or not. 因此,一旦它们被声明就像占用内存的任何其他数据类型一样,它们将占用/消耗内存,而不管其变量是否被初始化。

The array is not dynamically allocated, so each element has memory allocated at compile time. 数组不是动态分配的,因此每个元素都有在编译时分配的内存。 But the null-initialized pointers do not point to additional allocated memory. 但是,空初始化的指针并不指向额外的已分配内存。

I hope that's what you're asking. 我希望这就是你所要求的。

初始化为NULL的指针使用内存,但只使用指针所需的字节数。

Whether you declare your pointers in an array, or individually, one by one, the same amount of memory will be consumed. 无论是在数组中声明指针,还是逐个单独地声明指针,都会消耗相同数量的内存。 Of course, if you can declare fewer pointers by declaring them individually, then less space will be consumed. 当然,如果你可以通过单独声明它们来声明更少的指针,那么将消耗更少的空间。

So the following two lines both reserve space for 3 pointers: 所以以下两行都为3个指针预留了空间:

SDL_Surface* surfaces[3];
SDL_Surface *surface1, *surface2, *surface3;

Given the small number of pointers involved in your code sample, you should opt for the approach that results in the most readable code. 鉴于代码示例中涉及的指针数量很少,您应该选择导致代码最可读的方法。 Only if your array was huge and had only a small proportion of pointers non-null would it be worth getting concerned about the overhead of the unused pointers in the array. 只有当你的数组庞大并且只有一小部分指针非空时才值得关注数组中未使用的指针的开销。

A null pointer takes up the same space as any other pointer to data. 空指针占用与任何其他指向数据的指针相同的空间。 What you will save on will be the memory space allocated for data objects. 您将节省的是为数据对象分配的内存空间。

As to whether you should use an enum or something else - my main concern would be that C++ arrays of arrays aren't ideal. 至于你是否应该使用枚举或其他东西 - 我主要担心的是C ++数组阵列并不理想。 If you have an entire row or column of the array is empty, I certainly wouldn't bother defining it. 如果你的数组的整行或列是空的,我当然不会打扰定义它。 Just keep it readable and maintainable. 只是保持其可读性和可维护性。

A pointer is merely another data type, like int , double , etc. that happens to have the purpose of storing memory addresses. 指针只是另一种数据类型,如intdouble等,恰好具有存储内存地址的目的。 Typically, a pointer will take 32 or 64 bits depending on your architecture, but could take more/less on other hardware. 通常,指针将取32或64位,具体取决于您的体系结构,但在其他硬件上可能需要更多/更少。

Just like an int array takes space even if the values are 0's, so too does a pointer array take space even if its values are 0. (NULL is typically just represented as a 0.) 就像int数组占用空间一样,即使值为0,指针数组也占用空间,即使值为0.(NULL通常只表示为0)


For example, assuming your architecture/compiler uses a 32-bit int: 例如,假设您的体系结构/编译器使用32位int:

int array[30];

The above code will reserve 30 x 4 bytes = 120 bytes. 上面的代码将保留30 x 4字节= 120字节。 It doesn't matter what values you store. 您存储的值无关紧要。


Let's assume now that you're on a 64-bit machine: 现在让我们假设您使用的是64位计算机:

int *ptr_array[30];

The above code will reserve 30 x 8 bytes = 240 bytes, regardless of what pointer values you store. 无论您存储什么指针值,上面的代码都将保留30 x 8字节= 240字节。

You can imagine this as writing 30 individual pointers: 您可以将此想象为编写30个单独的指针:

int *ptr1, *ptr2, *ptr3, ..., *ptr30;

Note that we're only talking about the size that the pointers consume in memory. 请注意,我们只讨论指针在内存中消耗的大小。 Typically, you would have memory reserved elsewhere that the pointers point to (when non-null). 通常,您将在指针指向的其他位置保留内存(非空时)。 That memory would of course vary depending on how much you have allocated. 那个记忆当然会根据你分配的数量而有所不同。 In that sense, having the pointers be null implies that there's nothing for them to point to, and can translate to reduced memory consumption. 从这个意义上讲,指针为空意味着它们没有任何东西可以指向它们,并且可以转化为减少的内存消耗。

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

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