双免费或腐败(!prev),但肯定没有双免费()

发布于 2025-01-24 09:19:44 字数 1177 浏览 1 评论 0原文

我得到双免费或损坏(!prev),但我不知道为什么。到目前为止,我了解它会告诉我什么。至少我没有做双人免费,但它可以肯定在free()命令上失败。

好的,一些代码。我的结构定义为如下:

#define OWMINDEV 2

struct owdevice_struct {
        char    name[30];
        float   value;
};

struct ow_struct {
        int     countdev;
        struct owdevice_struct *owdevice;
};

struct ow_struct ow;

最初,我分配值并使用*owdevice作为数组,我可以访问和分配值:

ow.countdev = OWMINDEV;
ow.owdevice = malloc( OWMINDEV * sizeof(struct owdevice_struct));

strcpy(ow.owdevice[0].name,"t_abl_i");
ow.owdevice[0].value = 3.5;

strcpy(ow.owdevice[1].name,"t_out");
ow.owdevice[1].value = 1.5;

我可以轻松访问和读取所有这些值。到目前为止很好。现在,我有了一个例程,可以用不同的计数(动态数组)重新分配这些变量。因此,我的想法是释放由OW.OWDEVICE指向的内存,然后再次使用新数量的设备进行Alloc。

m = 5;
free(ow.owdevice);
ow.owdevice = NULL;
ow.owdevice = calloc( m, sizeof(struct owdevice_struct));

但是,一旦程序转到free()我会遇到错误。不,关于OW变量,代码中的任何地方都没有其他free()呼叫。由于它不能是双重的,它似乎是腐败,对吗?但为什么?

有趣的是,上述代码的第一个呼叫(countdev的3次释放和分配给20的Countdev)无问题。只是第二个呼叫会导致错误。

有什么想法,伙计们吗?

提前致谢!

/knebb

I am getting a double free or corruption (!prev) but I do not know why. So far I understand what it will tell me. At least I am not doing a double free but it fails for sure on the free() command.

Ok, some code. I have a structure defined as follows:

#define OWMINDEV 2

struct owdevice_struct {
        char    name[30];
        float   value;
};

struct ow_struct {
        int     countdev;
        struct owdevice_struct *owdevice;
};

struct ow_struct ow;

Initially I assign values and use the *owdevice as array and I can access and assign values:

ow.countdev = OWMINDEV;
ow.owdevice = malloc( OWMINDEV * sizeof(struct owdevice_struct));

strcpy(ow.owdevice[0].name,"t_abl_i");
ow.owdevice[0].value = 3.5;

strcpy(ow.owdevice[1].name,"t_out");
ow.owdevice[1].value = 1.5;

I can easily access and read all these values. Fine so far. Now I have a routine which does some sort of re-assigning these variables with a different count (dynamic array). So my idea is to free up the memory which is pointed to by ow.owdevice and do an alloc again with the new number of devices.

m = 5;
free(ow.owdevice);
ow.owdevice = NULL;
ow.owdevice = calloc( m, sizeof(struct owdevice_struct));

But as soon as the program goes to free() I am getting the error. And no, there are really no other free() calls anywhere in the code regarding the ow variable. As it can not be a double free it appears to be a corruption, right? But why?

Interestingly, the first call of the above code (with a countdev of 3 freeing and allocating to a countdev of 20) works without any issues. Just the second call then causes the error.

Any ideas here, guys?

Thanks in advance!

/KNEBB

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

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

发布评论

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

评论(1

眸中客 2025-01-31 09:19:44

好的,我找到了这个问题。

我的功能可以重新排列上述阵列中的项目。最初,我配置了三个项目要修复并在重新安排时跳过此项目。
进一步编码,我添加了另外两个固定设备,但我也忘了跳过这些设备。因此,我的代码包括在重排中,并写信给[20]和[21],而不是[19] max。

它在分配的内存后写入,该内存在尝试释放时会导致错误()。

感谢您的所有提示!

Ok, I found the issue.

I had a function which rearranged the items in the above array. Originally I configured three items to be fixed and skipped this items when rearranging.
Further coding I added another two fixed devices but I forgot to skip these, too. So my code included these in rearrangement and wrote to [20] and [21] instead of [19] max.

It wrote after the allocated memory which caused the error when trying to free().

Thanks for all your hints!

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