Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

c - Malloc call crashing, but works elsewhere

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...