Thread: Yet another free() question: user breakpoint called

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    2

    Yet another free() question: user breakpoint called

    Hi all,

    Hoping you can help with a free() problem I'm having. I have the following struct:
    Code:
     typedef struct fillline{
    	// to be used in an array. one filline correponds to a scanline, ie a Yvalue
    		int X1;		// Left most Xcoord
    		int X2;		// rightmost Xcoord
    	 } fillline;
    which is then allocated in an array as follows:

    Code:
    fillline *pointarray = (struct fillline *)malloc(sizeof(fillline)*Dy);
    	for (int i = 0; i< Dy; i++)
    	{
    		pointarray[i].X1  = SCREENX + SCREENY; //initialise to be a big number 
    		pointarray[i].X2  =-1;					// initialise to be a small number
    	}
    after i've done stuff with my array (passed pointarray to a function and filled up the X1 and X2 values etc I try this:

    Code:
    free(pointarray);
    which when running in debug mode in VC6 tells me that a user breakpoint has been called - this apparently happens when you try to free memory that hasn't been malloc'd, but as far as I can tell I've malloc'd pointarray fine.

    Any ideas people? I've also tried keeping a copy of pointarray and freeing that, in case pointarray had changed value, but I think I saw somewhere that free() is actually working by value so that wouldn't have made much of a difference anyway...

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    you should start with testing malloced memory for NULL first.

    For the rest this code looks fine to me, the memory does get allocated otherwise it would've failed inside the loop and passing a NULL pointer to free is safe. The problem is probably somewhere else.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    2

    Smile

    cheers.
    I've just read that the fact that it's failing at the call to free doesn't necessarily mean that's where the problem is.
    ....which i wasn't aware of, but am now.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > fillline *pointarray = (struct fillline *)malloc(sizeof(fillline)*Dy);
    Don't cast malloc in C
    See the FAQ
    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. question about free()
    By TalosChen in forum C Programming
    Replies: 5
    Last Post: 05-20-2006, 06:11 PM
  2. Help needed with backtracking
    By sjalesho in forum C Programming
    Replies: 1
    Last Post: 11-09-2003, 06:28 PM
  3. Newbie question (user error checking)
    By eno in forum C++ Programming
    Replies: 6
    Last Post: 11-27-2002, 09:28 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. what does this warningmean???
    By kreyes in forum C Programming
    Replies: 5
    Last Post: 03-04-2002, 07:53 AM