473,387 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

freeing of char**

Hello all,
I have a char **, which points to a number of strings (char*).
When comes the moment to free it, i have to free all the (char*) inside.
Yet when i do this:

int i;

for ( i=0; *(myChar+i) != NULL; i++ )
free (*(myChar+i)); /* to free the (char*) inside
free(myChar);
well, on execution the system says 'junk pointer, too low to make sense'
(and the pointers aren't freed)

of course, when i write (*(myChar+i) = NULL), it's all okay
(except the memory hasn't been freed ?!)

so how should i free the (char**) to make it work, and also be clean ?

thx
gregg

Nov 14 '05 #1
8 10961
gregg wrote:

I have a char **, which points to a number of strings (char*).
When comes the moment to free it, i have to free all the (char*)
inside. Yet when i do this:

int i;

for ( i=0; *(myChar+i) != NULL; i++ )
free (*(myChar+i)); /* to free the (char*) inside
free(myChar);

well, on execution the system says 'junk pointer, too low to
make sense' (and the pointers aren't freed)

of course, when i write (*(myChar+i) = NULL), it's all okay
(except the memory hasn't been freed ?!)

so how should i free the (char**) to make it work, and also
be clean ?


You should post complete compilable programs, cut to the minimum
from the original, for questions like this. Your description is
missing the essentials, i.e. how was myChar declared and
initialized, and how was *myChar initialized.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #2

"gregg" <gr******@netJUSTSAYNOcourrier.com> wrote in message
news:40***********************@news.free.fr...
Hello all,
I have a char **, which points to a number of strings (char*).
When comes the moment to free it, i have to free all the (char*) inside.
Yet when i do this:

int i;

for ( i=0; *(myChar+i) != NULL; i++ )
free (*(myChar+i)); /* to free the (char*) inside
free(myChar);
well, on execution the system says 'junk pointer, too low to make sense'
(and the pointers aren't freed)

of course, when i write (*(myChar+i) = NULL), it's all okay
(except the memory hasn't been freed ?!)

so how should i free the (char**) to make it work, and also be clean ?


Regardless of the types of objects, only 'free()' what memory
you 'malloc()' or 'realloc()'. Your code snipped doesn't show
whether you've done any allocations or not.

-Mike
Nov 14 '05 #3
gregg <gr******@netjustsaynocourrier.com> spoke thus:

for ( i=0; *(myChar+i) != NULL; i++ )
Nice try, but no. First of all, just because a pointer isn't NULL
doesn't mean you should attempt to free it (you may not have even
malloc()'ed it!). Secondly, you seem to be treating your char** like
a char* - i.e., that it is NULL terminated. If it were, you'd be
fine. However, unless you've made specific arrangements (i.e., myChar
points to enough space for a pointer to NULL in addition to all the
char pointers), this will almost assuredly fail. It's a much better
plan to just know how many character pointers you allocated, and then
free() exactly that many.
free (*(myChar+i)); /* to free the (char*) inside
Why not just free( myChar[i] ); ? It looks cleaner and accomplishes
the same thing.
of course, when i write (*(myChar+i) = NULL), it's all okay
(except the memory hasn't been freed ?!)
Quite correct - setting a pointer to NULL in no way frees any memory
it might have been pointing at.
so how should i free the (char**) to make it work, and also be clean ?


Keep track of how many you have (say j):

for( i=0 ; i<j ; i++)
free( myChar[i] );
free( myChar );

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #4
In <40***********************@news.free.fr> gregg <gr******@netJUSTSAYNOcourrier.com> writes:
I have a char **, which points to a number of strings (char*).
When comes the moment to free it, i have to free all the (char*) inside.
Yet when i do this:

int i;

for ( i=0; *(myChar+i) != NULL; i++ )
free (*(myChar+i)); /* to free the (char*) inside */
free(myChar);
well, on execution the system says 'junk pointer, too low to make sense'
(and the pointers aren't freed)


Are you sure that *(myChar+i) (usually written myChar[i]) has been
initialised with a malloc (or friends) call? Are you sure that your
array of pointers is terminated by a null pointer?

Only pointer values obtained from malloc and friends need to be freed.
It is a major mistake to free any other pointer values.

So, without seeing how myChar and myChar[i] have been initialised, there
is no way to telling what you're doing wrong. This is an initialisation
scenario compatible with your freeing code (error checking omitted for
brevity):

char **myChar = malloc(10 * sizeof *myChar);
for (i = 0; i < 3; i++) myChar[i] = malloc(25);
myChar[3] = NULL;

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #5
CBFalconer wrote:
You should post complete compilable programs, cut to the minimum
from the original, for questions like this. Your description is
missing the essentials, i.e. how was myChar declared and
initialized, and how was *myChar initialized.


This is the function:

char ** resetResults (char ** res) {

int i;

if ( res != NULL ) { /* if not NULL, has to be freed */
for ( i=0; *(res+i) != NULL; i++ ) {
free(*(res+i));
}
free(res);
}
/* new allocation, shall contains one char* to NULL */
res=(char**)realloc(NULL, sizeof(char*));

if ( res != NULL ) return res;
else exit(RESULT_ALLOC_PB);
}
The purpose is to stock an indefinite number of strings (ie. char*).
The end is marked with NULL pointer (a bit like **argv).

Now comes a moment when a new search is to be made: thus the strings
contained in char** must be freed, and a new char** containing one NULL
pointer (meaning it is empty) has to be created.

That's my point: i want to free all strings, then the char** which
contained them. and then create a fresh new one.
(To keep memory clean, i don't want to use *(res+i)= NULL, which works,
but does not free memory !)

Nov 14 '05 #6
gregg wrote:

CBFalconer wrote:
You should post complete compilable programs, cut to the minimum
from the original, for questions like this. Your description is
missing the essentials, i.e. how was myChar declared and
initialized, and how was *myChar initialized.

This is the function:

char ** resetResults (char ** res) {

int i;

if ( res != NULL ) { /* if not NULL, has to be freed */
for ( i=0; *(res+i) != NULL; i++ ) {
free(*(res+i));
}
free(res);
}
/* new allocation, shall contains one char* to NULL */
res=(char**)realloc(NULL, sizeof(char*));


Add: *res = NULL;
if ( res != NULL ) return res;
else exit(RESULT_ALLOC_PB);
}


--
Er*********@sun.com
Nov 14 '05 #7
Eric Sosman wrote:

gregg wrote:

CBFalconer wrote:
You should post complete compilable programs, cut to the minimum
from the original, for questions like this. Your description is
missing the essentials, i.e. how was myChar declared and
initialized, and how was *myChar initialized.


This is the function:

char ** resetResults (char ** res) {

int i;

if ( res != NULL ) { /* if not NULL, has to be freed */
for ( i=0; *(res+i) != NULL; i++ ) {
free(*(res+i));
}
free(res);
}
/* new allocation, shall contains one char* to NULL */
res=(char**)realloc(NULL, sizeof(char*));


Add: *res = NULL;
if ( res != NULL ) return res;
else exit(RESULT_ALLOC_PB);
}


Ah, yes, well, what I *really* meant, of COURSE,
was add the assignment after checking that `res' itself
is non-NULL. (Harrumph.) While you're at it, replace
the peculiar realloc() with the simpler malloc() and
clean things up a trifle:

/* new allocation, shall contains one char* to NULL */
res = malloc(sizeof *res);
if (res != NULL) {
*res = NULL;
return res;
}
exit (RESULT_ALLOC_PB);

--
Er*********@sun.com
Nov 14 '05 #8
gregg wrote:
CBFalconer wrote:
You should post complete compilable programs, cut to the minimum
from the original, for questions like this. Your description is
missing the essentials, i.e. how was myChar declared and
initialized, and how was *myChar initialized.


This is the function:

char ** resetResults (char ** res) {

int i;

if ( res != NULL ) { /* if not NULL, has to be freed */
for ( i=0; *(res+i) != NULL; i++ ) {
free(*(res+i));
}
free(res);
}
/* new allocation, shall contains one char* to NULL */
res=(char**)realloc(NULL, sizeof(char*));

if ( res != NULL ) return res;
else exit(RESULT_ALLOC_PB);
}

The purpose is to stock an indefinite number of strings (ie. char*).
The end is marked with NULL pointer (a bit like **argv).


That is still not a complete compilable program. I, and many
others, will not bother to look at it. Dan and Mike have given
you good advice.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: disco | last post by:
I am working on this example from a book "C Primer Plus" by Prata 4th edition - p. 672. There is no erata on this problem at the publisher's website. 1) Is it a violation of copyright laws to...
12
by: f.oppedisano | last post by:
Hi, i would like to allocate two structures making only one malloc call. So i do prt=malloc(sizeof(struct1)+sizeof(struct2)); After this operation i make two pointers one to the first struct...
5
by: Amogh | last post by:
Hi, My question is related to setting freed pointers to NULL. After freeing a pointer: 1) Should the freeing routine also be responsible for setting the pointer to null? 2) Or, should the...
10
by: Michael Goerz | last post by:
Hi, how can I free a matrix that was created with this function: double **my_matrix(long number_of_rows, long number_of_columns){ double **matrix; matrix = calloc(number_of_rows,...
1
by: Ryan Knopp | last post by:
I can't seem to figure out why this is happening. I even tried to use gdb but it seems it should work. I've commented below where this is happening Here's a snippet. int main() { /*...
66
by: karthikbalaguru | last post by:
Hi, Will 'free' return the memory Immediately to the OS ? Thx in advans, Karthik Balaguru
9
by: david | last post by:
I will past only two segments from the code it should be enough to see what I did wrong, I think I know there I made a mistake, but how to fix it I can not tell. This why I need help from you all....
11
by: vivek | last post by:
Hello, I have a pointer to a main structure which again consists of structures, enums, char, int, float and again complex structures. When i free all the contents of the main structure, it...
11
Dheeraj Joshi
by: Dheeraj Joshi | last post by:
The output what i am expecting is a segmentation fault but some output is coming. char *str = get_time();//malloc is done in this function and returning that char* char *str1...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.