Thread: Exc_bad_access

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    3

    Exc_bad_access

    Hi, I am writing a tool in C, but can't get rid of this error. I get a "EXC_BAD_ACCESS" on line afile = fopen(argv[3], "r");, which is right after where I try to init a few matrices.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main (int argc, const char * argv[])
    {  
        int w, h;
        char line[64];
        char * pch;
        int frame, idx_x, idx_y, depth;
        int mvx, mvy;
        FILE *afile;
        FILE *bfile;
        
        if (argc != 5) {
            printf("Usage: [...]");
            exit(1);
        }
        
        w = atoi(argv[1]);
        h = atoi(argv[2]);
    
        int mtrx_depth[w][h];
        int mtrx_mvx[w][h];
        int mtrx_mvy[w][h];
        
        afile = fopen(argv[3], "r");
        bfile = fopen(argv[4], "r");
            
        if (afile == NULL || bfile == NULL) {
            printf("ERROR: Could not read file");
            fclose(afile==NULL ? bfile : afile);
            exit(1);
        }
        
    // Main loop 1: iterate over depth values in bfile, fill table
        while (fgets(line, sizeof line, bfile) != NULL) {
                pch = strtok(line, ",");
            frame = atoi(pch);
                pch = strtok(NULL, ",");
            idx_x = atoi(pch);
                pch = strtok(NULL, ",");
            idx_y = atoi(pch);
                pch = strtok(NULL, ",");
            depth = atoi(pch);
            mtrx_depth[idx_x][idx_y] = depth;
        }
        
    // Main loop 2: iterate over lines in afile, fill table
        while (fgets(line, sizeof line, afile) != NULL) {
            // Read line vars
            {
            pch = strtok(line, "\t");
            frame = atoi(pch);
            pch = strtok(NULL, "\t");
            mvx = atoi(pch);
            pch = strtok(NULL, "\t");
            mvy = atoi(pch);
            }
            mtrx_mvx = mvx;
            mtrx_mvy = mvy;
        }
        fclose(afile);
        fclose(bfile);
        return 0;
    }
    The error:
    Running…
    Program received signal: “EXC_BAD_ACCESS”.
    sharedlibrary apply-load-rules all
    warning: Unable to restore previously selected frame.
    No memory available to program now: unsafe to call malloc
    Data Formatters temporarily unavailable, will re-try after a 'continue'. (Unknown error loading shared library "/Developer/Applications/Xcode.app/Contents/PlugIns/GDBMIDebugging.xcplugin/Contents/Resources/PBGDBIntrospectionSupport.A.dylib")
    My guess is it's something to do with a (lack of) malloc for the three matrices, but I thought it worked earlier and don't really see how I should malloc them then...

    Any help/suggestions would be welcome.

    Thanks in advance!

    Xavier

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I suppose that depends on how big w and h are. Variable-length arrays may still end up on the stack, I think, and you've only got 1MB on the stack (usually) to play with.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
        w = atoi(argv[1]);
        h = atoi(argv[2]);
    
        int mtrx_depth[w][h];
        int mtrx_mvx[w][h];
        int mtrx_mvy[w][h];
    So what values are you storing in w and h?

    Local variables are typically created on the stack - which is usually set to be a lot smaller than the memory pool used by malloc.
    Try doing a hand calculation of the sizes of these arrays (w*h*4*3) and see what you get. If it's more than say 1MB, I think you need another plan.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    3
    Quote Originally Posted by Salem View Post
    Code:
        w = atoi(argv[1]);
        h = atoi(argv[2]);
    
        int mtrx_depth[w][h];
        int mtrx_mvx[w][h];
        int mtrx_mvy[w][h];
    So what values are you storing in w and h?

    Local variables are typically created on the stack - which is usually set to be a lot smaller than the memory pool used by malloc.
    Try doing a hand calculation of the sizes of these arrays (w*h*4*3) and see what you get. If it's more than say 1MB, I think you need another plan.
    w and h will typically be 1024 * 768 (1920 * 1088 worst case), which makes at least 3 times 3.14 MB (= up to 25 MB worst case).

    What should the code look like you suggest, if this was using the stack rather than the main memory pool? I'm not sure how this works with the 2D int matrices...

    Kind regards,

    Xavier


    edit: less urgent but also interesting, can someone shed some light on the meaning of this "EXC_BAD_ACCESS" interrupt? I wonder what's going on under the hood there...
    Last edited by xsmet; 05-24-2011 at 08:30 AM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well for an easy life (at the expense of a bit of memory), you could just do
    static int mtrx_depth[1920][1088];

    To allocate it using malloc, it would be
    Code:
    int ** mtrx_depth = malloc( 1920 * sizeof(*mtrx_depth) );
    for ( i = 0 ; i < 1920 ; i++ ) {
        mtrx_depth[i] = malloc( 1088 * sizeof(*mtrx_depth[i]) );
    }
    Then free each mtrx_depth[i] before freeing mtrx_depth.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    3
    That seemed to do it, thanks! Will test more extensively tomorrow, but it's looking good

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string compare “EXC_BAD_ACCESS”.
    By dunsta in forum C Programming
    Replies: 3
    Last Post: 05-03-2010, 10:30 PM