Skip to main content
GameDev.net gamedev.net
🔒 Locked

malloc problems

Started by cold_storm Jul 30, 2006 at 10:25 PM 0 replies 800+ views
Original Post
cold_storm
cold_storm
i am a relative beginner. this is the problem. as i allocate memory for a string with malloc(), it messes up other parts of the memory. kinda like this: int ParseMaterial(char *temp, struct f *file, int mn, int on){ ... ... file->obj[on].materials[mn].name = (char *) malloc(name_length); /*at this point the data that the char *temp points to is screwed up, starting in random spots, but always leaving the start of the data intact*/ ... ... } im really confused by this, and am not quite sure what is causing the problem. if i could figure out what was causing the problem, im pretty sure i could fix it..thanks in advance.
Null and Void
Null and Void
It's unlikely that malloc itself that is causing the problem (feel free to call it but not store the returned value anywhere as a test). If the temp is mangled after that statement but not before, one or more of the following is likely the cause: file points at the wrong address, file->obj points at the wrong address or on is outside its bounds, file->obj[on].materials points at the wrong address or mn is outside its bounds. Any of that could have occured due to an even earlier memory corruption issue, or temp points at the wrong address or overwrote its bounds or otherwise.

If the code is relatively independent or less than enormous, feel free to post it so we can get a closer look.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.