GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 20-Apr-2005, 10:17
nusstu nusstu is offline
New Member
 
Join Date: Mar 2004
Posts: 27
nusstu is on a distinguished road

Can't free after malloc


I've the following code which creates a 2-D array.
How do i free it properly. apparently, my method of free gives
the error:
free(): invalid pointer 0x80f3080!

CPP / C++ / C Code:
   int ** array;
  array = (int **)malloc(2*sizeof(int *)); 
  array[0] = (int*) malloc (2 * 2 * sizeof(int)); 
  if (array[0] == NULL) exit (-1); 

  free(array[0]);
  free(array);

Thanks.
  #2  
Old 20-Apr-2005, 10:28
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by nusstu
I've the following code which creates a 2-D array.
How do i free it properly.


That is the correct way. Just make sure you aren't changing the value of array or array[0] anywhere else in your program.

You could try testing with this:

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>

int main()
{

  int **array;
  array = malloc(2*sizeof(int *)); 
  printf("array    = %p\n", array);

  array[0] = malloc(2 * 2 * sizeof(int)); 
  printf("array[0] = %p\n", array[0]);

  if (array[0] == NULL) {
    free(array);
    return 1;
  }
  free(array[0]);
  free(array);
  return 0;

}


Now, if this works, but it doesn't work with your program (with other stuff between malloc() and free()), then put printf after each malloc() and printf before each free() statement to see the value of the pointer that you are giving to free() is the same that you got from malloc().

Regards,

Dave
Last edited by davekw7x : 20-Apr-2005 at 11:27.
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Hosting Serenity - Try us for a 10 day FREE trial today! Paradoxic Web Hosting Advertisements & Offers 2 08-May-2005 16:10
50% OFF Basic Reseller Plan - FREE Domain and DOUBLE Bandwidth - From $5 per month Dailyhosting Web Hosting Advertisements & Offers 0 16-Mar-2005 17:06
holy free hosting enlightment Nzip Free Web Hosting 2 07-Mar-2004 05:04

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:11.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.