Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

farmerdoug

macrumors 6502a
Original poster
Sep 16, 2008
541
0
I read in the right number of bits but get the failure when I try to read them.
My memory has been properly allocated. The code compiles with no errors or warnings.


if ((mfilters[filtnum].filters[j] = (float *)calloc((12 + 3*j) , sizeof(float))) == NULL)
printf("calloc failure\n");
else
num = fread(mfilters[filtnum].filters[j], 1, (12 + 3*j)*sizeof(float), fp);

printf("%d %d \n",j, num);
printf("%f\n", mfilters[0].filters[0][0]);
 
Without code tags, the example is very hard to read. Without a compilable example it makes it very hard to look at this for you. If you can give more info, we're more likely to be able to help.

-Lee
 
more Kern Protection.

I'm sorry, What is a code tag?

The entire code includes some external libraries. If you are willing to look through the whole code, I'd be glad to send everything to you.
Thanks
Doug
 
code with tabs.

This code gets the kern protection error

PHP:
if ((mfilters[filtnum].filters[j] = (float *)calloc((12 + 3*j) , sizeof(float))) == NULL)
printf("calloc failure\n");
else
num = fread(mfilters[filtnum].filters[j], 1, (12 + 3*j)*sizeof(float), fp);

printf("%d %d \n",j, num);
printf("%f\n", mfilters[0].filters[0][0]);

This code prints the wrong number; fprow is not assigned to mfilters[0].fprow
PHP:
if ( (num = fscanf(map, "%d %d, %d %d\n", &fcol, &frow, &fpcol, &fprow)) == 4)
					{
						if( (col == fcol) && (row == frow ))
						{
						found = TRUE;
						rewind(map);
						mfilters[filtnum].col = col;
						mfilters[filtnum].row = row;
						mfilters[filtnum].fpcol = fpcol;
						mfilters[filtnum].fprow = fprow;
						printf("%d %d \n", fprow, mfilters[0].fprow);
                                          }
 
Does the final printf need a pointer dereference?

printf("%f\n", *(mfilters[0].filters[0][0]));

... or, is the printf already trying to deference it, but it's 0, so it's trying to follow a null pointer? That could cause a protection fault. Whether through the calloc or the loading, there must be a null pointer in the structure.
 
reply

no. Putting in the deference operator results in a compiler error.
The code compiles and does not give me any errors when allocating memory or when trying to write to the structure I only have problems when I try and read the values in the structure.
 
I've got no context here, but shouldn't this line:
printf("%f\n", mfilters[0].filters[0][0]);

be something like this:

printf("%f\n", mfilters[filtnum].filters[j][0]);[/QUOTE]

Is that the line it is crashing on?

Also, double-check that mfilters[filtnum].filters[j] exists and is properly allocated before you assign values to it. For example, perhaps filtnum or j is out-of-bounds.
 
reply

It crashes there and says
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION_FAILURE at address: 0x00000000

By the time it gets to that line, data should have been read in, as with the failure to print the right value for mfilters[filtnum].fprow
 
reply

Sure, but why would I be if I'm allocating memory with calloc and not getting any error messages?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.