Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

ugor

Need Help with realloc

This topic is 5274 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is my code typedef struct { char *LastName; } Name; typedef struct { int numberOfNames; LastName *link; } ArrayOfNames; int getArray(FILE *in, ArrayOfNames *array); int getName(FILE *in, LastName *name); int copyName(FILE *in, LastName *name) { int ok = 0; char temp[75]; char *string = malloc(sizeof(char));; fgets(temp,100,in); if(temp==NULL) { return 1; } temp[strlen(temp)-2] = ''\0''; if(temp[0]==''\0'') { return -1; } strcpy(string,temp); rec->value = string; return 1; } int getArray(FILE *in, ArrayOfName *array) { int counter = 0; LastName *new_name = (LastName*)malloc(sizeof(LastName)); LastName *array_name = (LastName*)calloc(1,sizeof(LastName)); while(copyRec(in,new_name)==1) { array_name[counter].code = new_name->code; array_name[counter].value = new_name->value; counter++; array_name=(DxRec*)realloc(array_name,sizeof(DxRec)*counter); array->numberOfNames++; } array->link = array_name; return 0; } The thing that i wanna do. I want my getArray function to call my copyName function so that it can store all of the name in the array of structers. My problem is when i use realloc, it changes the memory location of my pointer(?) and the program crashes after the first function has been called two time. Does anybody know what a possible solution might be for this. I was thinking about double pointer, put the thing is i don''t really know how to use them. If anybody can help I would be grateful.

Share this post


Link to post
Share on other sites
Advertisement
char *string = malloc(sizeof(char));
...
strcpy(string,temp);


are you sure?

My problem is when i use realloc, it changes the memory location of my pointer(?) and the program crashes after the first function has been called two time. Does anybody know what a possible solution might be for this.

Handles.




“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
In addition your memory handling is far from attractive. There''s no error handling whatsoever. Either check return values from each allocation and handle NULL values, or use new/delete and have them throw bad_alloc on failure.

Especially realloc is dangerous unless you know it. If
ptr = realloc(ptr, ...)
fails, the original ptr is leaked. Thus you need a temporary variable for value, then do the NULL check, and eventually set ptr=temp.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!