C结构类型令人困惑

发布于 2025-01-23 15:11:44 字数 216 浏览 2 评论 0原文

struct payload{
    struct metadata meta;
    char data_pointer[0];
};

struct payload* mypayload = (struct payload*) malloc(sizeof(struct payload) + 50);

这里的mypayload类型是什么? 地址,指针或其他东西

struct payload{
    struct metadata meta;
    char data_pointer[0];
};

struct payload* mypayload = (struct payload*) malloc(sizeof(struct payload) + 50);

What is the type of mypayload here?
The address, pointer, or something else

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

给妤﹃绝世温柔 2025-01-30 15:11:44

malloc返回一块未型的内存,分配是您告诉编译器是什么类型的方式。在这种情况下,struct有效载荷 *是内存的指针(地址),如果分配失败,则分配或null。

您不需要Cast (struct Poreload*)

如果要data_pointer是灵活的数组成员,则将大小未指定的char data_pointer [];留下。大小0的数组是未定义的行为。

malloc returns an untyped chunk of memory, and the assignment is how you tell the compiler what type it is. In this case struct payload * which is a pointer (an address) of the memory that was allocated or NULL if the allocation failed.

You don't need the cast (struct payload*).

If you want data_pointer to be a flexible array member, then you leave the size unspecified char data_pointer[];. An array of size 0 is undefined behavior.

我们的影子 2025-01-30 15:11:44

mypayload的类型是什么?

mypayload指针,是struct> struct有效载荷的指针。 @kaylum


,例如char data_pointer [0];未由C定义,尽管某些实现使其作为灵活数组成员(fam)。

由于C99使用char data_pointer []; last 成员定义为A 灵活数组成员

要最好分配a 灵活数组成员,请计算2个尺寸的总和:

  • size of mypayload [0]:对象的大小到达但不包括FAM成员。这将包括FAM成员面前的任何填充。

  • mypayload-> data_pointer [0]:所需数组元素的数量50的FAM的大小。

  • 提示:在sizeof代码中不需要A 类型。易于正确编码,查看和维护以使用引用对象的大小。

  • 提示:不需要。

  • 提示:检查分配成功。

例子:

struct payload{
  struct metadata meta;
  char data_pointer[];  // No 0
};

struct payload* mypayload = malloc(
    sizeof mypayload[0] + 
    sizeof mypayload->data_pointer[0] * 50);
if (mypayload == NULL) {
  ; // TBD code to handle out-of-memory
} 

What is the type of mypayload here?

mypayload is a pointer, a pointer to a struct payload. @kaylum


A struct array member with size 0, like char data_pointer[0]; is not defined by C although some implementations had allowed it as a pre-cursor to a flexible array member (FAM).

Since C99 use char data_pointer[]; to define the last member as a flexible array member.

To best allocate for a flexible array member, compute the sum of 2 sizes:

  • sizeof mypayload[0]: Size of the object up to, but not including the FAM member. This will include any padding before the FAM member.

  • sizeof mypayload->data_pointer[0]: Size of the FAM referenced data times the count, 50, of array elements desired.

  • Tip: a type is not needed in the sizeof code. Easier to code right, review and maintain to use the size of the referenced object.

  • Tip: Cast not needed.

  • Tip: Check for allocation success.

Example:

struct payload{
  struct metadata meta;
  char data_pointer[];  // No 0
};

struct payload* mypayload = malloc(
    sizeof mypayload[0] + 
    sizeof mypayload->data_pointer[0] * 50);
if (mypayload == NULL) {
  ; // TBD code to handle out-of-memory
} 
马蹄踏│碎落叶 2025-01-30 15:11:44

mypayload是指针。这是通过malloc()分配的内存的指针。并且您将其配置为指向有效载荷的指针。

mypayload is a pointer. It's a pointer to the memory allocated by malloc(). And you have it configured as a pointer to a payload.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文