Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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
#includeextern 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.

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.

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

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.

I just noticed the syntax that you used. In case it throws you,
mate->daveis 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...

![]() |
![]() |
![]() |

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