c 中结构体的大小 - size_t

发布于 2024-08-24 11:14:09 字数 333 浏览 10 评论 0原文

由于某种原因,当我尝试获取结构的大小时,我不断遇到分段错误。

struct my_struct {
  char *a;
  int  b;
};

int main(int argc, char *argv[])
{
    struct my_struct dastruct;
    size_t len = sizeof(dastruct) / sizeof(struct my_struct); // error
    qsort(dastruct, len, sizeof(struct my_struct), cmp);
    ...
}

有什么想法我做错了吗?谢谢

For some reason I keep getting segmentation fault when I try to get the size of my struct.

struct my_struct {
  char *a;
  int  b;
};

int main(int argc, char *argv[])
{
    struct my_struct dastruct;
    size_t len = sizeof(dastruct) / sizeof(struct my_struct); // error
    qsort(dastruct, len, sizeof(struct my_struct), cmp);
    ...
}

Any ideas what I'm doing wrong? Thanks

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

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

发布评论

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

评论(4

月棠 2024-08-31 11:14:09

如果您在该行发生崩溃

size_t len = sizeof(dastruct) / sizeof(struct my_struct); 

,则可能是由于 sizeof(struct my_struct) 计算结果为 0 引起的。(即问题实际上是除以零)。当类型 struct my_struct 不完整(即未定义)时,某些编译器可能会发生这种情况。在 sizeof 中使用不完整类型在 C 中是非法的,但某些编译器出于某种原因允许这样做,并将其计算为 0。(尽管我希望编译器在编译时捕获此除以 0 的情况。)

你的代码显然是假的,并不能说明问题。如果上述理论是正确的,那么很可能在您的实际代码中,您在 sizeof 中输错了类型 struct my_struct 的名称,或者忘记包含类型 的定义>结构my_struct

(可能性很小,但无论如何......)

If you are getting the crash specifically at the

size_t len = sizeof(dastruct) / sizeof(struct my_struct); 

line, it might be caused by sizeof(struct my_struct) evaluating to 0. (I.e the problem is actually the division by zero). This might happen in some compilers when type struct my_struct is incomplete, i.e. it is not defined. Using incomplete type in sizeof is illegal in C, but some compilers allow it for some reason, evaluating it to 0. (Although I'd expect the compiler to catch this division by 0 at compile time.)

Your code is obviously fake and doesn't illustrate the problem. If the above theory is correct, most likely in your real code you either mistyped the name of the type struct my_struct in sizeof or forgot to include the definition of the type struct my_struct.

(Highly unlikely, but anyway...)

薄暮涼年 2024-08-31 11:14:09

您可能想让 dastruct 成为一个数组。

   struct my_struct dastruct[10];

编辑:您也没有给出 cmp,因此不可能说那里是否有问题(我认为这将是发生分段错误的地方)。

You probably want to make dastruct an array.

   struct my_struct dastruct[10];

Edit: You've also not given cmp, so its not possible to say if something is wrong there (which I see would be the place the segmentation fault occurs).

一城柳絮吹成雪 2024-08-31 11:14:09

我唯一能看到的是你忘记了#include。请记住,C 在第一次使用时隐式声明了 qsort

The only thing I can possibly see is that you forgot to #include <stdlib.h>. Remember that C implicitly declares a qsort at the first use.

诗笺 2024-08-31 11:14:09

如果您发布的代码是完整的代码,那么您未能初始化 dastruct 的 a 指针成员,因此它指向某个无效位置。当 qsort() 调用可能使用该指针的 cmp() 时(您尚未显示该代码),您可能会遇到段错误。

请注意,我假设您的段错误发生在除仅除两个常量的 len 初始化之外的某个地方(无论如何,这很可能在编译时发生)。

If your code as posted is your complete code, then you have failed to initialise the a pointer member of dastruct, so it's pointing off into some invalid location. When qsort() calls cmp() which presumably uses that pointer (you haven't shown that code), you are likely to get a segfault.

Note that I'm assuming your segfault happens at some place other than the initialisation of len that only divides two constants (which is likely to happen at compile time anyway).

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