How to find & remove memory leaks and errors in my C program after checked it with Valgrind?

The first thing to do is get rid of all the memory error(buffer overruns). The first error occurs in get_input. C at line 45, so go to that line and figure out what's going wrong.

The first thing to do is get rid of all the memory error(buffer overruns). The first error occurs in get_input. C at line 45, so go to that line and figure out what's going wrong.

One thing that's surely going wrong is this place=(place_t)malloc(sizeof(place_t)*num); You're allocating a place_t , however you have defined a place_t as this: typedef struct { }*place_t; i.e. A place_t is just a pointer. That means that e.g. Malloc(sizeof(place_t)) just allocates space for a pointer, not for a whole place_t.

Don't hide structs names as pointers with a typedef, but if you must, change your malloc statement to place=(place_t)malloc(sizeof *place)*num); Another problem is that you free() the struct and afterwards try to access a member inside it, which is invalid. Free(place); free(place->name); You have to do it in this order free(place->name); free(place); You are also leaking memory e.g. Here: for(i=0;iname=(char *)malloc(sizeof(char)*MAX); What happens the 2. Time in this loop?

You assign to place->name , losing the pointer to the memory you allocated in the first iteration of the loop. Maybe you meant to use placenum inside the loop as you do try to allocate many place_t's in the place=(place_t)malloc(sizeof(place_t)*num); statement.

The problem isn't so much the malloc statements, but rather the order in which you're freeing the memory up once you're finished with it. E.g. Free(place); free(place->name); should be free(place->name); free(place); otherwise memory allocated to name won't be freed.

Similarly, calling free(place); won't free memory allocated in place->name=(char *)malloc(sizeof(char)*MAX); You must manually free the memory allocated to each Malloc statement. It's always best to check whether pointers to memory locations are null before using them again with malloc. If they're not null then free them and then set them to null.

This will help in ensuring that you don't have memory leaks.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions