Error with free()

Jan 24, 2014 at 6:50pm
Hi, I am trying to play around with malloc() and free() to learn and test them. So I wrote a little program that stores all the even numbers till 2000 in an array that is allocated using malloc. It is then freed using free(). However, I get an error when the program is ended. Any help is appreciated.

Error:

*** Error in `./a.out': free(): invalid next size (fast): 0x089bb008 ***
a.out: malloc.c:2369: sysmalloc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.
Aborted


Here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
	int *myarray = (int *)malloc(sizeof(int));

	for(int i = 1; i <= 1000; i++)
	{
		myarray[i-1] = 2*i;
	}

	for(int k = 0; k < 1000; k++)
		cout << myarray[k] << endl;

	free(myarray);
	return 0;
}
Jan 24, 2014 at 6:53pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
	int *myarray = (int *)malloc(sizeof(int) * 1000);

	for (int i = 1; i <= 1000; i++)
	{
		myarray[i - 1] = 2 * i;
	}

	for (int k = 0; k < 1000; k++)
		cout << myarray[k] << endl;

	free(myarray);
	cin.ignore();
	return 0;
}
Jan 24, 2014 at 6:57pm
Yanson is correct.

To clarify, you are not allocating enough memory with malloc. You are only allocating space for a single int... whereas you need to allocate space for 1000 ints.


Also, FWIW, you generally should avoid malloc/free in C++.
new[]/delete[] are better
and std::unique_ptr is better still:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <memory>

using namespace std;

int main()
{
	std::unique_ptr<int[]> myarray( new int[1000] );

	for(int i = 1; i <= 1000; i++)
	{
		myarray[i-1] = 2*i;
	}

	for(int k = 0; k < 1000; k++)
		cout << myarray[k] << endl;

	// free(myarray); <- no need to free/delete.  unique_ptr does it
	  //  automatically.
	return 0;
}
Last edited on Jan 24, 2014 at 6:57pm
Jan 24, 2014 at 6:57pm
int *myarray = (int *)malloc(sizeof(int));

This does not allocate a dynamic array of 1000 int's.

1
2
3
int *myarray = (int *)malloc(sizeof(int) * 1000);
// or
int *myarray = reinterpret_cast<int *> (std::calloc(1000, sizeof (int)));


http://www.cplusplus.com/reference/cstdlib/calloc/

If I were you, I'd leave the malloc() family to C where they belong.

This is because they do not play nice with objects that have a non-trivial constructor or destructor i.e. constructors and destructors are not called to initialize the chunk of memory that is allocated.
Jan 24, 2014 at 7:02pm
Thank you all for your replies and sugessions. :)
Topic archived. No new replies allowed.