简体   繁体   English

C语言中的struct的指针数组

[英]Array of pointers to struct in C

I have a struct in my c code of about 300Bytes (5xint + 256chars), and I wish to have a good mechanism of array for handling all my 'objects' of this struct. 我的c代码中有一个约300Bytes(5xint + 256chars)的结构,并且我希望有一个很好的数组机制来处理该结构的所有“对象”。 I want to have a global array of pointers, so that at first all indices in the array points to NULL, but then i initialize each index when I need it (malloc) and delete it when im done with it (free). 我想要一个全局的指针数组,这样一来数组中的所有索引都指向NULL,但是当我需要它时,我会初始化每个索引(malloc),在我完成它时将其删除(免费)。

typedef struct myfiles mf;
mf* myArr[1000];

Is that what Im looking for? 我在找什么吗? Pointers mixed with arrays often confuse me. 指针与数组混合经常使我感到困惑。 If so, just to clerify, does 如果是这样,只是为了澄清一下,

mf myArr[1000];

already allocates 1000 structs on the stack, where my first suggestion only allocates 1000pointers? 已经在堆栈上分配了1000个结构,而我的第一个建议仅分配了1000个指针?

You are correct. 你是对的。 Former allocates 1000 pointers, none of which are initialized, latter initializes 1000 objects of ~300 bytes each. 前者分配1000个指针,没有一个被初始化,后者则初始化1000个每个〜300字节的对象。

To initalize to null: foo_t* foo[1000] = {NULL}; foo_t* foo[1000] = {NULL};为null: foo_t* foo[1000] = {NULL};

But this is still silly. 但这仍然很愚蠢。 Why not just mf* myArr = NULL ? 为什么不只是mf* myArr = NULL Now you have one pointer to uninitialized memory instead of 1000 pointers to initialized memory and one pointer to keep track of. 现在,您有一个指向未初始化内存的指针,而不是1000个指向初始化内存的指针,还有一个指针可以跟踪。 Would you rather do 你愿意吗

myArraySingle = malloc(sizeof(mf)*1000); or 要么

for(int i = 0; i < 1000; i++) {
    myArray[i] = malloc(1000);
}

And access by myArraySingle[300] or *(myArray[300])`? 并通过myArraySingle[300]或*(myArray [300])`访问? Anyway my point is syntax aside don't create this unnecessary indirection. 无论如何,除了语法之外,不要创建这种不必要的间接寻址。 A single pointer can point to a contiguous chunk of memory that holds a sequence of objects, much like an array, which is why pointers support array-style syntax and why array indices start at 0. 单个指针可以指向包含一系列对象的连续内存,就像数组一样,这就是为什么指针支持数组样式语法以及为什么数组索引从0开始的原因。

typedef struct myfiles mf;
mf* myArr[1000];

This is what you are looking for. 这就是您要寻找的。 This will allocate array of 1000 pointers to the structure mf. 这将为结构mf分配1000个指针的数组。

You seem to understand correctly. 您似乎理解正确。

More accurately, I believe mf* myArr[1000] = { 0 }; 更准确地说,我相信mf* myArr[1000] = { 0 }; would better meet your requirements, because you want a guarantee that all of the elements will be initialised to null pointers. 会更好地满足您的要求,因为您希望保证所有元素都将初始化为空指针。 Without an initialisation, that guarantee doesn't exist. 没有初始化,这种保证就不存在。

There is no "global" in C. You're referring to objects with static storage duration, declared at file scope. C中没有“全局”。您指的是具有静态存储持续时间的对象,在文件作用域声明。

typedef struct myfiles mf;
mf* myArr[1000];

yes, it will initialize 1000 pointers, you have to allocate memory to each one using malloc/calloc before use. 是的,它将初始化1000个指针,使用前必须使用malloc / calloc为每个指针分配内存。

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

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