Malloc 调用崩溃,但在其他地方可以工作

发布于 2024-12-25 04:53:39 字数 678 浏览 0 评论 0原文

我想知道是否有人对此有任何见解...

我的程序在这个调用中崩溃了:

void subtract(data* array,data* inverse,int a, int b, int q, int n)
{

data* arraytomultiply;
arraytomultiply = (data *)malloc(sizeof(data*) * n);

其中 data 只是保存一个 int (这是为了以后切换类型时方便)

typedef struct { 
        int value;
}data;

我已经尝试了很多乱七八糟的方法来更改指针,如我对他们一点信心都没有,但没有什么用。

奇怪的是,在程序的更早阶段,同一个调用起作用了,我为其分配值并可以将它们打印出来以及所有内容......:

data* array;
array = (data*)malloc(sizeof(data*) * m * n); // m * n entries

可能有用的一件事(尽管我不知道为什么)是当它起作用时早些时候它是在一个 void 函数中,而当它崩溃时,它是在一个从算法内部调用的函数中。但我根本不明白这会如何影响它,因为我想做的不是使用任何参数等......

有什么想法吗?

I wonder if anyone might have any insight on this...

My program is crashing on this call:

void subtract(data* array,data* inverse,int a, int b, int q, int n)
{

data* arraytomultiply;
arraytomultiply = (data *)malloc(sizeof(data*) * n);

Where data just holds an int (it's for convenience when switching types later)

typedef struct { 
        int value;
}data;

I've tried lots of messing around with changing the pointers here as I'm not confident on them at all, but to no avail.

The strange thing is, much earlier in the program this same call works, I assign values to it and can print them out and everything..:

data* array;
array = (data*)malloc(sizeof(data*) * m * n); // m * n entries

One thing that might be of use (though I have no idea why) is that when it works earlier it's during a void function, whereas when it crashes it's in a function called from within an algorithm. but I don't see how this could affect it at all, given what I'm trying to do isn't using any of the arguments etc...

Any ideas?

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

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

发布评论

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

评论(4

这样的小城市 2025-01-01 04:53:39

因为您正在为数据结构分配空间,所以不应该是 sizeof(data) 而不是 sizeof(data*) 吗?

Shouldn't that be sizeof(data) instead of sizeof(data*) since you're allocating space for data structures?

云雾 2025-01-01 04:53:39

您正在分配 data *m * n 个元素,而不是 data。如果您想要指向 data 的指针数组,那么您在 malloc() 中所做的事情是正确的,但应该分配给 data **

array = (data*)malloc(sizeof(data*) * m * n); // m * n entries

它应该是

array = (data*)malloc(sizeof(data) * m * n); // m * n entries

,并且您应该始终检查 malloc() 的返回值以了解它是失败还是成功!

if ((array = (data*)malloc(sizeof(data) * m * n)) == NULL) {
    printf("unable to allocate memory");
    return; // you can return your error code here!
}

你的程序有各种理由崩溃。但是当你说,它早些时候工作但后来崩溃是为了做一些实验。我尝试了你的代码片段,发现它对我有用。我尝试了很多次,但从来没有崩溃过。我很困惑,我发布了一个问题来找出原因?! - 可在此处是“malloc(sizeof(struct a * ))" 和 "malloc(sizeof(struct a))" 相同吗?

+1 你的问题!

You are allocating m * n elements of data * and not data. If you want array of pointers to data then what you are doing in malloc() is correct but that should be assigned to data **

array = (data*)malloc(sizeof(data*) * m * n); // m * n entries

It should be

array = (data*)malloc(sizeof(data) * m * n); // m * n entries

and, you should always check the return value of malloc() to find whether it fails or succeeds!

if ((array = (data*)malloc(sizeof(data) * m * n)) == NULL) {
    printf("unable to allocate memory");
    return; // you can return your error code here!
}

Your program has all the reason to crash. But when you said, it worked earlier but crashed later made be to do some experiments. I tried your code snippet and found it working for me. I tried many a times but it never crashed. I got puzzled and I posted a question to find out why?! - It is available here Are "malloc(sizeof(struct a *))" and "malloc(sizeof(struct a))" the same?

+1 to your question!

半步萧音过轻尘 2025-01-01 04:53:39

你们都是对的,但无论如何我想知道为什么会崩溃。

我想知道,因为 data 的大小(如上面定义的)预计小于或等于 data* 的大小。

You all are right, but anyhow I wonder why this crashes.

I wonder because the size of data (as defined above) is expected to be less or equal to the size of data*.

贱贱哒 2025-01-01 04:53:39

当 malloc 崩溃时,通常是因为您弄乱了它用来跟踪堆上其他地方的内存的结构。你的程序是多线程的吗?尝试使用 helgrind 或 drd (都是 valgrind 工具)运行它。这些可以帮助您追踪竞争条件、不受保护的数据访问或其他线程问题。

When malloc crashes, it is usually because you have messed up the structures it uses to track memory on the heap somewhere else. Is your program multi-threaded? Try running it with helgrind or drd (both valgrind tools). These can help you track down race conditions, unprotected data accesses, or other threading issues.

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