more Kern Protection Failure errors

Discussion in 'Mac Programming' started by farmerdoug, May 1, 2009.

  1. farmerdoug macrumors 6502a

    Joined:
    Sep 16, 2008
    #1
    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]);
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    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
     
  3. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #3
    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
     
  4. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #5
    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",jnum);
    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"fprowmfilters[0].fprow);
                                              }
     
  5. SydneyDev macrumors 6502

    Joined:
    Sep 15, 2008
    #6
    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.
     
  6. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #7
    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.
     
  7. iSee macrumors 68040

    iSee

    Joined:
    Oct 25, 2004
    #8
    I've got no context here, but shouldn't this line:
    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.
     
  8. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #9
    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
     
  9. SydneyDev macrumors 6502

    Joined:
    Sep 15, 2008
    #10
    Address 0 - that's a sure sign you're trying to dereference a null pointer.
     
  10. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #11
    reply

    Sure, but why would I be if I'm allocating memory with calloc and not getting any error messages?
     

Share This Page