Malloc 调用崩溃,但在其他地方可以工作
我想知道是否有人对此有任何见解...
我的程序在这个调用中崩溃了:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
因为您正在为数据结构分配空间,所以不应该是 sizeof(data) 而不是 sizeof(data*) 吗?
Shouldn't that be sizeof(data) instead of sizeof(data*) since you're allocating space for data structures?
您正在分配
data *
的m * n
个元素,而不是data
。如果您想要指向data
的指针数组,那么您在malloc()
中所做的事情是正确的,但应该分配给data **
它应该是
,并且您应该始终检查
malloc()
的返回值以了解它是失败还是成功!你的程序有各种理由崩溃。但是当你说,
它早些时候工作但后来崩溃
是为了做一些实验。我尝试了你的代码片段,发现它对我有用。我尝试了很多次,但从来没有崩溃过。我很困惑,我发布了一个问题来找出原因?! - 可在此处是“malloc(sizeof(struct a * ))" 和 "malloc(sizeof(struct a))" 相同吗?+1 你的问题!
You are allocating
m * n
elements ofdata *
and notdata
. If you want array of pointers todata
then what you are doing inmalloc()
is correct but that should be assigned todata **
It should be
and, you should always check the return value of
malloc()
to find whether it fails or succeeds!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!
你们都是对的,但无论如何我想知道为什么会崩溃。
我想知道,因为
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 ofdata*
.当 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.