Computing.Net > Forums > Programming > malloc/realloc problems inC

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

malloc/realloc problems inC

Reply to Message Icon

Name: smalltreee
Date: August 13, 2003 at 13:52:30 Pacific
OS: XP`
CPU/Ram: Athalon2000XP/512DDR266
Comment:

Hi, need to dynamicaly create/resize a char array
which is a field in a struct. The struct is an element
in an array of structs. the code compiles ok but
the program crashes whenever I run it, i.e. doesn't
end or exit.

The code:

#include
#include
#include
#include

extern int errno;


struct stuff {
char *dave, *temp_dave;
int steve;
};

int main() {


struct stuff *mate=NULL, *temp_mate;


mate=(struct stuff *)malloc(sizeof(struct stuff));
if(mate=NULL) {
perror("malloc\n");
exit(1);
}

mate[0].dave=(char *)malloc(sizeof(char));
if(mate[0].dave==NULL) {
perror("malloc\n");
exit(1);
}

temp_mate=(struct stuff *)realloc(mate, sizeof(struct stuff)+sizeof(struct stuff));
if(temp_mate==NULL) {
perror("realloc\n");
exit(1);
}

mate=temp_mate;
free(mate[0].dave);
free(temp_mate);
printf("\nexit ok\n");
return 0;
}

any ideas, or suggestions? Cos I'm all out. Any help would be greatly appreciated.



Sponsored Link
Ads by Google

Response Number 1
Name: micah
Date: August 13, 2003 at 14:04:09 Pacific
Reply:

Try making mate, mate[0], and temp_mate NULL terminated. That's fixed my problems before.


0

Response Number 2
Name: smalltreee
Date: August 13, 2003 at 14:40:53 Pacific
Reply:

thanks for your response.  Had a bit of a long day. 
could you explain a little more?

did you mean changing this line

struct stuff *mate=NULL, *temp_mate;

to this?

struct stuff mate[0]=NULL, *temp_mate=NULL;?

I've tried it and it generates loads of errors.


0

Response Number 3
Name: micah
Date: August 13, 2003 at 18:02:38 Pacific
Reply:

Alright, 2 problems:

if(mate=NULL) {

Should be:
if(mate==NULL) {

And:
mate[0].dave=(char *)malloc(sizeof(char));
if(mate[0].dave==NULL) {

mate[0].dave wont = NULL, it needs to be set first:

mate[0].dave=NULL;
mate[0].dave=(char *)malloc(sizeof(char));
if(mate[0].dave==NULL) {

hope that helps...

-Micah


0

Response Number 4
Name: smalltreee
Date: August 14, 2003 at 01:56:41 Pacific
Reply:

brilliant, thanks. I've had some sleep now and it all makes sense.


0

Response Number 5
Name: Don Arnett
Date: August 14, 2003 at 08:47:45 Pacific
Reply:

It looks like this code is just a test to see that you can realloc and doesn't really do anything. But, for the future, I see two 'issues':

mate[0].dave=(char *)malloc(sizeof(char));

You are allocating only one byte here for the array 'dave'. Obviously, not enough room to put a string in there. But, maybe that was just for this test.

More importantly, in the code:

temp_mate=(struct stuff *)realloc(mate, sizeof(struct stuff)+sizeof(struct stuff));


Note that for the structure 'mate', you did two mallocs, one to allocate the structure 'mate' and one to allocate some memory for the 'dave' array.

But in creating temp_mate, you use only one allocation call (realloc). So you've made a new copy of 'mate' in 'temp_mate'. But this realloc() does not allocate memory for the 'dave' array in 'temp_mate'. I'm 99% sure that realloc() copies the contents from 'mate' to 'temp_mate', so temp_mate->dave will contain a pointer to the same memory as mate->dave. If this is what you want, that's fine. But if you want temp_mate->dave to be a separate array from mate->dave, then you need to run another malloc for temp_mate->dave.


Finally:

mate=temp_mate;


Since you didn't free 'mate' before copying 'temp_mate' to it, you now have a bit of memory (the allocation of the original 'mate' structure) hanging out with no pointer, so you can't free it.


0

Related Posts

See More



Response Number 6
Name: Don Arnett
Date: August 14, 2003 at 08:50:26 Pacific
Reply:

I just noticed the syntax that you used. In case it throws you,


mate->dave

is the basically the same as

mate[0].dave

Personall, I wouldn't use mate[0] in this situation because that implies that mate is an array. I guess technically you could say that it is an array, an array of one element, but...


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: malloc/realloc problems inC

C++ Dynamic Array www.computing.net/answers/programming/c-dynamic-array/11481.html

arrays's size www.computing.net/answers/programming/arrayss-size/18378.html

using malloc and realloc www.computing.net/answers/programming/using-malloc-and-realloc/6496.html