使用 memset 初始化结构体数组

发布于 2024-09-12 07:38:05 字数 340 浏览 3 评论 0原文

gcc 4.4.4 c89

我有以下结构。

struct device_sys
{
    char device[STRING_SIZE];
    int id;
    char category;
};

int main(void)
{
    struct device_sys dev_sys[NUM_DEVICES];

    memset(dev_sys, 0, (size_t)NUM_DEVICES * sizeof(dev_sys));

    return 0; 
}

当我调用 memset 时,我得到了堆栈转储。这不是初始化结构体数组的正确方法吗?

gcc 4.4.4 c89

I have the following structure.

struct device_sys
{
    char device[STRING_SIZE];
    int id;
    char category;
};

int main(void)
{
    struct device_sys dev_sys[NUM_DEVICES];

    memset(dev_sys, 0, (size_t)NUM_DEVICES * sizeof(dev_sys));

    return 0; 
}

I get a stack dump when I call memset. Is this not the correct way to initialize an structure array?

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

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

发布评论

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

评论(4

心的憧憬 2024-09-19 07:38:05

要么

memset(&dev_sys, 0, sizeof dev_sys);

要么

memset(dev_sys, 0, NUM_DEVICES * sizeof(struct device_sys));

或者,如果您喜欢

memset(dev_sys, 0, NUM_DEVICES * sizeof *dev_sys);

但不喜欢原始变体中的内容。

请注意,在您的特定情况下,在所有变体中,您可以使用 &dev_sysdev_sys 作为第一个参数。效果是一样的。但是,&dev_sys 在第一个变体中更合适,因为 if 遵循 memset(ptr-to-object, object-size) 习惯用法。在第二个和第三个变体中,使用 dev_sys (或 &dev_sys[0])更合适,因为它遵循 memset(ptr-to-第一个元素,元素数量 * 元素大小) 习惯用法。

PS 当然,在您的特定情况下,您应该使用初始化器声明您的数组,而不是使用所有那些黑客 memset 技巧

struct device_sys dev_sys[NUM_DEVICES] = { 0 };

。必要的。

Either

memset(&dev_sys, 0, sizeof dev_sys);

or

memset(dev_sys, 0, NUM_DEVICES * sizeof(struct device_sys));

Or, if you prefer

memset(dev_sys, 0, NUM_DEVICES * sizeof *dev_sys);

but not what you have in your original variant.

Note, that in your specific case in all variants you can use either &dev_sys or dev_sys as the first argument. The effect will be the same. However, &dev_sys is more appropriate in the first variant, since if follows the memset(ptr-to-object, object-size) idiom. In the second and third variants it is more appropriate to use dev_sys (or &dev_sys[0]), since it follows the memset(ptr-to-first-element, number-of-elements * element-size) idiom.

P.S. Of course, instead of using all that hackish memset trickery, in your particular case you should have just declared your array with an initializer

struct device_sys dev_sys[NUM_DEVICES] = { 0 };

No memset necessary.

浅黛梨妆こ 2024-09-19 07:38:05

您的代码中有一个拼写错误。修复:

memset(dev_sys, 0, (size_t)NUM_DEVICES * sizeof(struct device_sys));

选择好名字可以避免一半的错误。我推荐“设备”。

There's a typo in your code. Fix:

memset(dev_sys, 0, (size_t)NUM_DEVICES * sizeof(struct device_sys));

Picking good names avoid half the bugs. I'd recommend "devices".

旧时浪漫 2024-09-19 07:38:05

对于数组,sizeof 获取数组的整个大小,而不是单个元素的大小。 sizeof 运算符是少数几个不将数组视为指向其第一个元素的指针的地方之一。

For an array, sizeof gets you the entire size of the array, not the size of an individual element. The sizeof operator is one of the few places where an array is not treated as a pointer to its first element.

清晰传感 2024-09-19 07:38:05

您必须向 sizeof 运算符传递类型而不是变量。

memset(dev_sys, 0, (size_t)NUM_DEVICES * sizeof(struct device_sys));

我更喜欢使用 typedef 作为结构。

typedef struct tag_device_sys
{
    char device[STRING_SIZE];
    int id;
    char category;
} device_sys;

您可以使用memset,如下所示:

    memset(dev_sys, 0, (size_t)NUM_DEVICES * sizeof(device_sys));

You have to pass the sizeof operator the type and not the variable.

memset(dev_sys, 0, (size_t)NUM_DEVICES * sizeof(struct device_sys));

I prefer to use typedef for the struct.

typedef struct tag_device_sys
{
    char device[STRING_SIZE];
    int id;
    char category;
} device_sys;

The you can use memset as follows:

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