开发者

C access violation after using calloc

开发者 https://www.devze.com 2023-02-07 14:12 出处:网络
Note: C is Microsoft C Compiler. I\'m having trouble with the following code. *Roomsize = (int*)c开发者_如何学编程alloc(sizeof(int),sched->numberOfRooms);

Note: C is Microsoft C Compiler.

I'm having trouble with the following code.

*Roomsize = (int*)c开发者_如何学编程alloc(sizeof(int),sched->numberOfRooms);

roomIndex = 0;
for(roomIndex=0; roomIndex< sched->numberOfRooms; roomIndex++)
{
    fscanf(inputFile,"%d",&lineInput);
    numberOfLinesRead++;
    *Roomsize[roomIndex] = lineInput;
}

This is in a separate C file. I wasn't having this problem until I decided to separate things out to make them more maintainable and I think I'm just getting a little mixed up with pointers.

The calloc works fine.

On the first iteration of the loop, element zero of roomIndex gets set properly.

However the second element (element 1) in the loop, always results in an access violation at runtime.

I run into this problem later in my code too with a 2d array, but I'm figuring it's the exact same problem and this is just the most simple case.

Can anyone help me understand why it seems impossible to set anything but the first element here?


*Roomsize[roomIndex] is the same as *(Roomsize[roomIndex]). You want to say (*Roomsize)[roomIndex].

(I'm assuming that Roomsize is actually a int**. If that's not correct, then the problem may lie elsewhere.)


Your first line, when you allocate Roomsize, looks wrong. If I am correct in assuming Roomsize is an int *, you should just say Roomsize = (int *) calloc... As @Daniel posted, your assignment should also be changed to get rid of the asterisk.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号