6 Replies - 732 Views - Last Post: 27 May 2012 - 02:12 PM Rate Topic: -----

#1 tal1133  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 26-May 12

Can't free up memory of a two dimensional array

Posted 26 May 2012 - 03:48 AM

Hi there everyone, I'm having a little problem with freeing up memory of a two dimensional array in my program

This is my code:


void free_board(char** board,int size)
{
	int i;
	for(i = 0; i < size; i++)
	{
		free(board[i]);
	}
	free(board);
}




the for loop is just fine but when it gets to the free(board) line it gives me the following error:
Attached Image

I really have no idea what does it mean and will appreciate any help!!
Thank you all so much...

Is This A Good Question/Topic? 0
  • +

Replies To: Can't free up memory of a two dimensional array

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,311
  • Joined: 21-June 11

Re: Can't free up memory of a two dimensional array

Posted 26 May 2012 - 04:21 AM

It means that free contains an assertion that CrtIsValidHeapPointer(pUserData) is true, i.e. free asserts that the pointer you pass to it must be a valid pointer to memory allocated on the heap. This assertion failed.

In other words you passed a pointer to free that either did not point to valid memory on the heap. This could either mean that the memory you tried to free wasn't allocated on the heap or that one of your array accesses was out bounds (which would mean that you passed in a wrong value for size) which would give you an invalid pointer.


For more information we'd need to see the code where you allocate and populate board.

As a rule of thumb you should never call free on memory that you did not get from from malloc.
Was This Post Helpful? 1
  • +
  • -

#3 tal1133  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 26-May 12

Re: Can't free up memory of a two dimensional array

Posted 27 May 2012 - 01:04 PM

Hi,
Thank you for the response, anyway this it the code where i allocated board:


char** create_board(int size)
{
	int i, j;
	char **board;
	board = (char**)malloc(size);
	if(board == NULL)
	{
		return NULL;
	}
	for(i = 0; i < size; i++)
	{
		board[i] = (char*)malloc(size);
		if(board[i] == NULL)
		{
			for(j = 0; j < i; j++)
			{
				free(board[j]);
			}
			free(board);
		}
	}
	for(i = 0; i < size; i++)
	{
		for(j = 0; j < size; j++)
		{
			board[i][j] = EMPTY;
		}
	}


	return board;
}




Thanks again
Was This Post Helpful? 0
  • +
  • -

#4 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Can't free up memory of a two dimensional array

Posted 27 May 2012 - 01:23 PM

A couple of points on your create_board function.

1. Don't cast malloc.
2. On line 19, after you free(board), return NULL immediately afterwards. You don't want to continue on with the rest of the logic if memory allocation fails.

This isn't the source of the problem though.

There's a good chance, at some point in your code, you ended up freeing the board unintentionally. Here's a really easy way to help determine if that is the case. After every time you call free, set the pointer to null.

E.g.

free(board);
board = NULL;



It's a good defensive maneuver to help isolate bugs in your code.

Otherwise, you'll have to trace through your code and see where you mishandled the pointer.
Was This Post Helpful? 0
  • +
  • -

#5 tal1133  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 26-May 12

Re: Can't free up memory of a two dimensional array

Posted 27 May 2012 - 01:40 PM

Thanks for the tip, though I didn't get the first one...
Was This Post Helpful? 0
  • +
  • -

#6 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1764
  • View blog
  • Posts: 3,419
  • Joined: 30-May 10

Re: Can't free up memory of a two dimensional array

Posted 27 May 2012 - 01:48 PM

> 05 board = (char**)malloc(size);
Well it's already broken on the very first allocation.
You didn't scale size by the size of the thing you're allocating.

Eg.
board = malloc(size * sizeof(*board) );

It's easy to remember if you always use the form
p = malloc( num * sizeof(*p) );
Was This Post Helpful? 0
  • +
  • -

#7 tal1133  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 26-May 12

Re: Can't free up memory of a two dimensional array

Posted 27 May 2012 - 02:12 PM

haha That's so funny because i just learned it and I totally forgot about it, because it is an array of characters,
Anyway, my program runs smoothly now!!
Thank you so much, I really appreciate that!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1