0

Error:

*** Error in `./main': free(): invalid next size (fast): 0x080e1008 ***
Aborted

This is my program, and it's crashing when I try to deallocate a struct.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
//struct words contains a word as well as a boolean
//to check if it was used yet or not.
struct words
{
    char * word;
    int bool;
};
//the main function in which everything happens.
//the controller, if you will.
int main()
{
    struct words * word_library = malloc(9);
    struct timeval start, end;
    free(word_library);
    return 0;
}

So this is the code making my program crash:

free(word_library);

What is causing it to crash? And how can prevent this in the future? I know that every use of malloc() requires the free() after to deallocate it. But when I don't use free() it ends just fine, but I'm sure there's a memory leak.

5
  • 2
    You don't check the return value of malloc and try to free() memory you don't even know got allocated? Start from checking if it is not NUL. Commented Apr 14, 2015 at 7:10
  • 2
    The code as shown is correct, and I strongly doubt it crashes. Please show the real code. Commented Apr 14, 2015 at 7:41
  • 2
    @zubergu: If malloc() failed, I'd return NULL and passing NULL to free() is perfectly alright, at least for (implementations of the C language) following the C Standard. Commented Apr 14, 2015 at 7:43
  • @zubergu , You can find what alk said in the C11 standard , 7.22.3.3/2:"The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation. If ptr is a null pointer, no action occurs. Otherwise, if the argument does not match a pointer earlier returned by a memory management function, or if the space has been deallocated by a call to free or realloc, the behavior is undefined." Commented Apr 14, 2015 at 8:54
  • IMO: the returned value from malloc should be checked. If the OP did not check it here in this 'example' code, then they probably did not check it in the 'real' code. Any reference to the allocated memory, when the allocation failed would result in undefined behaviour, leading to a seg fault event. Commented Apr 14, 2015 at 23:09

1 Answer 1

4

This:

struct words * word_library = malloc(9);

doesn't allocate space for an array of struct words of size 9. Instead, it allocates 9 bytes. You need

struct words * word_library = malloc(sizeof(struct words)*9);

to allocate an array of size 9.

You also don't need to allocate and deallocate memory for word in the struct if you are going to make them point to string literals.

Sign up to request clarification or add additional context in comments.

4 Comments

would this be the same for allocating everything else using malloc? Such as char etc. Something like char * temp = malloc(sizeof(char)*10);
Yes. The reason why you do not need it for char is because sizeof(char) is guaranteed to be 1
Because what I essentially did was create an array of struct word objects.
There are more errors than just that in your code. You are calling is_match() with an uninitialized variable, for example. Enable a suitably high warning level for your compiler and fix each of them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.