malloc调用崩溃,但在其他地方工作 [英] Malloc call crashing, but works elsewhere

查看:557
本文介绍了malloc调用崩溃,但在其他地方工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道是否有人可能会对此有什么见解...

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);

如果数据只是持有一个int(这是为了方便切换类型时,更新版本)

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

这可能是使用的(虽然我不知道为什么),有一件事是,当早期的作品是一个void函数中的,而当它崩溃它在一个从算法中调用的函数。但我看不出这可能会影响它在所有,因为我尝试不使用任何的参数做等...

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...

任何想法?

推荐答案

难道不应该的sizeof(数据),而不是sizeof的(数据*)因为你的数据结构分配空间?

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

这篇关于malloc调用崩溃,但在其他地方工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆