Skip to main content



      Home
Home » Language IDEs » C / C++ IDE (CDT) » how to free doubled pointer
how to free doubled pointer [message #203523] Wed, 24 October 2007 10:38 Go to next message
Eclipse UserFriend
Originally posted by: qryt520.163.com

fig1:
char **data =(char **)malloc(100*sizeof(char **));
free(data);
fig2:
char **data =(char **)malloc(100*sizeof(char **));
free(*data);

which example is right???
would you tell the differences between them?
please explain the reseason or something i should konw.

thanks a lot?





--
Re: how to free doubled pointer [message #203627 is a reply to message #203523] Fri, 26 October 2007 05:19 Go to previous message
Eclipse UserFriend
Originally posted by: paul.mcconkey.net

> please explain the reseason or something i should konw.

This isn't really a forum for C language questions and the best reference
book is still http://cm.bell-labs.com/cm/cs/cbook/.

> char **data =(char **)malloc(100*sizeof(char **));

This line is wrong. malloc() returns a pointer to some memory that has been
allocated and it requires an argument that specifies the amount of memory to
allocate.

If I assume that (char ** data) is what you meant, then you are creating an
array of char pointers. The variable (data ) points to this array. malloc()
needs to know the size of the array which should be 100 * sizeof(char *),
not 100 * sizeof(char **). You want 100 items the size of a char pointer.

char **data =(char **)malloc(100*sizeof(char *));

You can write this like so:

char **data;
data = (char **) malloc(100*sizeof(char *));

the argument that free() takes is the address of the memory that malloc()
allocated. You can see above that you set the variable (data) to be the
address returned by malloc(), so to free this memory you need to call
free(data); The fact that data is a pointer to a pointer is irrelevant. The
value that data contains is the address returned by malloc().

Paul.
Previous Topic:Project Dependencies, multiple projects and CUTE
Next Topic:pwd in Makefile gives Windows path
Goto Forum:
  


Current Time: Wed May 07 16:15:53 EDT 2025

Powered by FUDForum. Page generated in 0.06603 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top