Thread: MSVS has triggered a breakpoint

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    42

    MSVS has triggered a breakpoint

    Hi guys,

    We are just learning how to implement calloc, malloc and free. At the end of my program, I keep getting a "windows has triggered a break point" error. Im pretty sure that this error is happening because of the way I am implementing free because this is the error I am getting in free.c - " retval = HeapFree(_crtheap, 0, pBlock);"

    here is the code which I allocate and free memory:

    Code:
    int main (){
         
        WORDS userWords[16];
        char **board;
        int i=0, n=0, numWords=0;
        srand(time(NULL));
          numWords = enterWords(userWords);
    
    
        board = (char **) calloc(20, sizeof(char));
        for (i=0; i<20; i++){
            board[i] = (char *) calloc(40, sizeof(char));
        }
        for(i=0; i<20; i++)
        {
            if(i != 0)
                printf("\n");
            for(n=0; n<40; n++){
                board[i][n] = '0';
                printf("%c", board[i][n]);
            }
        }
        
        inputWords(userWords, board, numWords);
        cls;
        for(i=0; i<20; i++)
        {
            if(i != 0)
                printf("\n");
            for(n=0; n<40; n++){
                printf("%c", board[i][n]);
            }
        }
        pause;
        populateGameBoard(userWords, board);
        displayGameBoard(userWords, board, numWords);
        pause;
        free(board);
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    63
    We are just learning how to implement calloc, malloc and free. At the end of my program, I keep getting a "windows has triggered a break point" error. Im pretty sure that this error is happening because of the way I am implementing free because this is the error I am getting in free.c - " retval = HeapFree(_crtheap, 0, pBlock);"
    When people say they are implementing a function, they usually mean that they are writing that function. It doesn't look like you are creating your own custom malloc, calloc, and free functions. I am assuming you meant that you are learning to use these functions rather than implementing custom versions of them.

    Code:
        board = (char **) calloc(20, sizeof(char));     
        for (i=0; i<20; i++){         
            board[i] = (char *) calloc(40, sizeof(char));     
        }
    You're not allocating enough space for board. board is a pointer to a char pointer. sizeof (char) = 1. Unless you're on some obscure system, sizeof (char *) will most likely be larger than that on your system.

    Try this:
    Code:
        board = calloc (20, sizeof (char *)) ;
        for (i=0; i<20; i++) {         
            board [i] = calloc (40, sizeof (char)) ;
        }
    I didn't look at the rest of your code.
    Last edited by failure67; 04-06-2013 at 07:54 PM.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    42
    I am getting "a value of type void* cannot be assigned to entity of type char**" with that calloc declaration. Is that because I declared char **board?

    Thanks

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    42
    Thanks Failure67,

    I got it to work with this:
    Code:
    board = (char **)calloc (20, sizeof (char* )) ;
    for (i=0; i<20; i++) {         
         board [i] = (char *)calloc (40, sizeof (char)) ;
    }
    I still don't quite understand why. It looks like I am allocating 40 bytes to every value of i. Its not giving me the break error anymore though.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    63
    I am getting "a value of type void* cannot be assigned to entity of type char**" with that calloc declaration. Is that because I declared char **board?
    My bad, I skipped the fact you're using MSVS. MSVS is a c++ compiler, and the c++ language requires the casts that you added to your code. Pure C does not require (or recommend) those casts.

    I still don't quite understand why. It looks like I am allocating 40 bytes to every value of i. Its not giving me the break error anymore though.
    The problem was specifically with this part:

    Code:
    board = (char **) calloc(20, sizeof(char));
    Because sizeof (char) < sizeof (char *), you have not allocated enough memory for 20 pointers. That's why you had to change it to this:

    Code:
    board = (char **)calloc (20, sizeof (char* )) ;
    You also have a memory leak in your code. You have to free the 40 byte buffers that each of these 20 pointers pointed to.

    So you need to add something like this to the end of your code:

    Code:
    for (i = 0; i < 20; ++i) {
        free (board [i]) ; // Free the 40 byte buffers of each pointer
    }
    
    free (board) ; // Free the array of pointers

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Then you should name your source files to be prog.c, not prog.cpp.
    This should give the compiler driver the clue to use the C rather than C++ compiler.

    Or use this flag to force language choice.
    /Tc, /Tp, /TC, /TP (Specify Source File Type) (C++)
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-04-2011, 10:29 AM
  2. negative edge triggered
    By galaxiluz in forum C Programming
    Replies: 3
    Last Post: 06-14-2010, 01:53 AM
  3. VS9 Breakpoint a dependency?
    By Glorfindel in forum C++ Programming
    Replies: 2
    Last Post: 04-07-2009, 10:47 PM
  4. can not set breakpoint
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 07-06-2008, 05:58 AM
  5. if statement triggered by a delay
    By MurrDogg4 in forum C Programming
    Replies: 2
    Last Post: 12-02-2005, 12:37 PM

Tags for this Thread