简体   繁体   English

是否可以将相同的指向 struct 的指针数组指向不同类型的 struct?

[英]Is it possible to have the same array of pointers to struct point to different types of struct?

I'm working with pointers to structs and have the following set up which has been working.我正在使用指向结构的指针,并进行了以下设置,该设置一直在工作。

/* Initialized. */
struct base { ... };  
struct base **db;
db = malloc( base_max * sizeof *db );

/* When a new struct base is required. */
db[ i ] = malloc( sizeof( struct base ) );

Now, if possible, although not really essential, I'd like only db[0] to point to a different struct, struct mem {... } .现在,如果可能的话,虽然不是很重要,但我只想db[0]指向一个不同的结构, struct mem {... } Is this possible and what is the proper way to do so?这是可能的吗?这样做的正确方法是什么?

I figured I can set db[0] = malloc( sizeof( struct mem ) ) ;我想我可以设置db[0] = malloc( sizeof( struct mem ) ) ; but db is already declared to point to a pointer pointing to a struct base ;但是db已经被声明为指向一个指向struct base的指针; and the memory allocation of db is based on that also. db的 memory 分配也是基于此。

I'm confused because I read pointers must have a type or pointer arithmetic won't work properly;我很困惑,因为我读到的指针必须有类型,否则指针算术将无法正常工作; but I also read you don't have to cast the pointers from malloc even though it returns void pointers.但我也读到你不必从malloc指针,即使它返回 void 指针。


Regarding the duplicate question, although it discusses void type it doesn't answer my question of how to accomplish this or the risks of it.关于重复的问题,虽然它讨论了 void 类型,但它并没有回答我关于如何完成这个或它的风险的问题。 The comment by @4386427 appears to point to the real issue to be considered which isn't addressed in the other question or the one answer to this one. @4386427 的评论似乎指向要考虑的真正问题,该问题未在其他问题或对此问题的一个答案中解决。 Thank you.谢谢你。

In majority of the architecture, the size of a pointer (to any type) is constant, and any pointer type will generally occupy the same amount of memory.在大多数架构中,指针(指向任何类型)的大小是恒定的,并且任何指针类型通常会占用相同数量的 memory。

Unless you're on some really uncommon hardware/platform, you can assign db[0] with any pointer returned by malloc() using a different structure type used for sizing calculation altogether.除非您在一些非常不常见的硬件/平台上,否则您可以使用malloc()返回的任何指针分配db[0] ,并使用完全用于大小计算的不同结构类型。

 db[0] = malloc( sizeof( struct mem ));

should do good, you just need to worry about memory leak, as you'll be overwriting the previous memory location returned by malloc() .应该做得很好,您只需要担心 memory 泄漏,因为您将覆盖malloc()返回的先前 memory 位置。

However, since you're storing a pointer to memory of a differnt type, while using db[0] , ie, dereferencing it, you need to cast it to the proper pointer type (ie, struct mem* ) before you can use the pointer to access /operate based on the struct mem type.但是,由于您要存储指向不同类型的 memory 的指针,因此在使用db[0]时,即取消引用它,您需要将其转换为正确的指针类型(即struct mem* ),然后才能使用基于struct mem类型访问 /operate 的指针。

That said,也就是说,

I'm confused because I read pointers must have a type or pointer arithmetic won't work properly;我很困惑,因为我读到的指针必须有类型,否则指针算术将无法正常工作;

That's true确实如此

but I also read you don't have to cast the pointers from malloc even though it returns void pointers.但我也读到你不必从 malloc 转换指针,即使它返回 void 指针。

Also true, as when you assign the pointer to a variable of a certain type (other than void ), the conversion is implicit.同样正确的是,当您将指针分配给某种类型的变量(除了void )时,转换是隐式的。 You can use that variable to perform pointer arithmetic.您可以使用该变量来执行指针运算。

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

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