realloc pointer array - binary tree

K

kfaeqsuvty

Hello, ....

I have a question regarding realloc() .

Is it possible to ``savely'' realloc() a pointer array
within a structure which is previously allocated by
malloc ?

When I try the c code as listed in the addtree() function
below, it works one time, but fails the other time a.k.a. buggy.

I tried to find some answers online and in some of my books but
I couldn't find any regarding this question.

Your info and/or suggestions are appreciated...

/* the tree node */
struct tnode {
char *md5sum; /* md5sum */
char **filenames; /* filenames */
int count; /* number of occurences */
struct tnode *left;
struct tnode *right;
};

struct tnode *addtree(struct tnode *n, char *sum, char *fname) {
int compare = 0;

if(n == NULL) {
n = (struct tnode *)malloc(sizeof(struct tnode));
n->md5sum = NULL;
n->filenames = NULL;
n->count = 0;

n->md5sum = strdup(sum);
n->filenames = (char **)realloc(n->filenames, (n->count + 1) * sizeof(char *));
n->filenames[n->count++] = strdup(fname);

n->left = n->right = NULL;
} else if((compare = strcmp(sum, n->md5sum)) == 0) {

n->filenames = (char **)realloc(n->filenames, (n->count + 1) * sizeof(char *));
n->filenames[n->count++] = strdup(fname);

} else if(compare < 0)
n->left = addtree(n->left, sum, fname);
else
n->right = addtree(n->right, sum, fname);

return n;
}

p.s.
I left out all of the error checking, to make the code
somewhat more readable.
thnkx....
 
M

Mark Gordon

Hello, ....

I have a question regarding realloc() .

Is it possible to ``savely'' realloc() a pointer array
within a structure which is previously allocated by
malloc ?

When I try the c code as listed in the addtree() function
below, it works one time, but fails the other time a.k.a. buggy.

I tried to find some answers online and in some of my books but
I couldn't find any regarding this question.

Your info and/or suggestions are appreciated...

It would have helped if you had provided a small COMPLETE compilable
program exhibiting the problem, as it is we can only guess what you have
failed to supply and something you have not included may be the problem
you are looking for. However, I'll tell you what I see wrong...

#include said:
/* the tree node */
struct tnode {
char *md5sum; /* md5sum */
char **filenames; /* filenames */
int count; /* number of occurences */
struct tnode *left;
struct tnode *right;
};

struct tnode *addtree(struct tnode *n, char *sum, char *fname) {
int compare = 0;

if(n == NULL) {
n = (struct tnode *)malloc(sizeof(struct tnode));

Don't cast the return value of malloc, it will hide a failure to include
stdlib.h. If you use the form

n = malloc(sizeof *n);

you are much less likely to allocate the wrong amount.
n->md5sum = NULL;
n->filenames = NULL;
n->count = 0;

n->md5sum = strdup(sum);

strdup is a non-standard function and so off topic here. However, I
don't think it is your problem.
n->filenames = (char **)realloc(n->filenames, (n->count + 1) *
sizeof(char *));

/* Obviously tmp will need to be declared somewhere above */
tmp = realloc(n->filenames, (n->count + 1) * sizeof *n->filenames);
if (tmp==NULL) {
/* handle failure to realloc remembering that n->filenames still
points to allocated memory unless it was NULL on entry, which
it was because you assigned it above */
}
else {
n->filenames = tmp;
}
n->filenames[n->count++] = strdup(fname);

n->left = n->right = NULL;
} else if((compare = strcmp(sum, n->md5sum)) == 0) {

n->filenames = (char **)realloc(n->filenames, (n->count + 1) *
sizeof(char *));

/* Obviously tmp will need to be declared somewhere above */
tmp = realloc(n->filenames, (n->count + 1) * sizeof *n->filenames);
if (tmp==NULL) {
/* handle failure to realloc remembering that n->filenames still
points to allocated memory */
}
else {
n->filenames = tmp;
}
n->filenames[n->count++] = strdup(fname);

} else if(compare < 0)
n->left = addtree(n->left, sum, fname);
else
n->right = addtree(n->right, sum, fname);

return n;
}

p.s.
I left out all of the error checking, to make the code
somewhat more readable.
thnkx....

Apart from being incomplete and lacking error checking there was nothing
in what you posted that obviously causes a problem. So perhaps your
error checking was wrong, maybe you did not include stdlib.h or possibly
there is an error in what you did not post.
 
L

lvmi

Hello, ....

I have located the segfault part! It wasn't in the
code I had listed.. Sorry, ....

However, I toke your comments and suggestions seriously
and I updated the code. I have learned something...

Thnkx for putting the time and effort in to reply
in such a complete and constructive manner.

Again Thnkx...

J.
>On 01 Dec 2003 10:22:22 GMT
>kfaeqsuvty said:
>> Hello, ....
>>
>> I have a question regarding realloc() .
>>
>> Is it possible to ``savely'' realloc() a pointer array
>> within a structure which is previously allocated by
>> malloc ?
>>
>> When I try the c code as listed in the addtree() function
>> below, it works one time, but fails the other time a.k.a. buggy.
>>
>> I tried to find some answers online and in some of my books but
>> I couldn't find any regarding this question.
>>
>> Your info and/or suggestions are appreciated...
>
>It would have helped if you had provided a small COMPLETE compilable
>program exhibiting the problem, as it is we can only guess what you have
>failed to supply and something you have not included may be the problem
>you are looking for. However, I'll tell you what I see wrong...
>
>#include said:
>> /* the tree node */
>> struct tnode {
>> char *md5sum; /* md5sum */
>> char **filenames; /* filenames */
>> int count; /* number of occurences */
>> struct tnode *left;
>> struct tnode *right;
>> };
>>
>> struct tnode *addtree(struct tnode *n, char *sum, char *fname) {
>> int compare = 0;
>>
>> if(n == NULL) {
>> n = (struct tnode *)malloc(sizeof(struct tnode));
>
>Don't cast the return value of malloc, it will hide a failure to include
>stdlib.h. If you use the form
>
>n = malloc(sizeof *n);
>
>you are much less likely to allocate the wrong amount.
>
>> n->md5sum = NULL;
>> n->filenames = NULL;
>> n->count = 0;
>>
>> n->md5sum = strdup(sum);
>
>strdup is a non-standard function and so off topic here. However, I
>don't think it is your problem.
>
>> n->filenames = (char **)realloc(n->filenames, (n->count + 1) *
>> sizeof(char *));
>
>/* Obviously tmp will need to be declared somewhere above */
>tmp = realloc(n->filenames, (n->count + 1) * sizeof *n->filenames);
>if (tmp==NULL) {
>/* handle failure to realloc remembering that n->filenames still
>points to allocated memory unless it was NULL on entry, which
>it was because you assigned it above */
>}
>else {
>n->filenames = tmp;
>}
>
>> n->filenames[n->count++] = strdup(fname);
>>
>> n->left = n->right = NULL;
>> } else if((compare = strcmp(sum, n->md5sum)) == 0) {
>>
>> n->filenames = (char **)realloc(n->filenames, (n->count + 1) *
>> sizeof(char *));
>
>/* Obviously tmp will need to be declared somewhere above */
>tmp = realloc(n->filenames, (n->count + 1) * sizeof *n->filenames);
>if (tmp==NULL) {
>/* handle failure to realloc remembering that n->filenames still
>points to allocated memory */
>}
>else {
>n->filenames = tmp;
>}
>
>> n->filenames[n->count++] = strdup(fname);
>>
>> } else if(compare < 0)
>> n->left = addtree(n->left, sum, fname);
>> else
>> n->right = addtree(n->right, sum, fname);
>>
>> return n;
>> }
>>
>> p.s.
>> I left out all of the error checking, to make the code
>> somewhat more readable.
>> thnkx....
>
>Apart from being incomplete and lacking error checking there was nothing
>in what you posted that obviously causes a problem. So perhaps your
>error checking was wrong, maybe you did not include stdlib.h or possibly
>there is an error in what you did not post.
>--
>Mark Gordon
>Paid to be a Geek & a Senior Software Developer
>Although my email address says spamtrap, it is real and I read it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top