Error free(): Invalid next size (fast) - c

I have a very simple program that malloc()'s some data, initializes it, then frees it and I keep getting this error. I have no idea why this is happening, any ideas?
#include <stdlib.h>
#include <stdio.h>
int main(){
int * big_mem;
int row_size = 4096; //not sure about this....
int num_rows = 5;
int const_val = 0xFFFFFFFF;
big_mem = (int *) malloc(row_size * num_rows);
if(!big_mem){
printf("Failed to malloc, exiting...");
return 0;
}
i = 0;
for(; i < row_size * num_rows; ++i){
big_mem[i] = const_val;
}
printf("Initialized mem, ");
free(big_mem);
big_mem = NULL;
return 0;
}
Output:
*** Error in `./test': free(): invalid next size (fast): 0x0000000000819010 ***

Use malloc(row_size * num_rows * sizeof(int)) .
You didn't malloc enough memory so your loop wrote past your malloc()ed memory. I'm surprised you didn't just get a segmentation fault.

You should write:
big_mem = (int *) malloc(row_size * num_rows * sizeof(int));

Related

Freeing arr returns error malloc: *** error for object pointer being freed was not allocated

I'm trying to write a function which takes 2 arrays and their size (both same size) and returns an array with each number from the first array appearing the amount of times in the same index in the second array. Example: input: {2,5,3,7,8},{5,2,0,4,3},5 output: {2,2,2,2,2,5,5,7,7,7,7,8,8,8}
My current code:
#include <stdio.h>
#include <stdlib.h>
int* blowUpArray(int numArray[], int amountArray[], int size);
int* reallocateArr(int* arr, int currLogSize, int newSize);
int main() {
int arr1[] = {2,5,3,7,8},arr2[] = {5,2,0,4,3}
int* res;
res = blowUpArray(arr1,arr2,5);
for (int i = 0; i < 14; i++)
printf("%d ", res[i]);
return 0;
}
int* blowUpArray(int numArray[], int amountArray[], int size)
{
int i = 0,currSize = 0,j;
int* blownUp;
while (i < size)
{
blownUp = reallocateArr(numArray,currSize,amountArray[i]);
for (j = currSize; j < currSize + amountArray[i]; j++)
{
blownUp[j] = numArray[i];
}
currSize = currSize + amountArray[i];
i++;
}
return blownUp;
}
int* reallocateArr(int* arr, int currLogSize, int addedSize)
{
int* newArr;
int i;
newArr = (int*)malloc((currLogSize + addedSize) * sizeof(int));
if (newArr == NULL)
{
printf("Memory allocation failed.\n");
exit(1);
}
for (i = 0; i < currLogSize; i++)
newArr[i] = arr[i];
free(arr);
return newArr;
}
I'm getting this error:
malloc: *** error for object 0x7ffee644b6f0: pointer being freed was not allocated
malloc: *** set a breakpoint in malloc_error_break to debug
Any ideas?
The reallocateArr function is freeing the arr parameter. It is called like this:
reallocateArr(numArray,currSize,amountArray[i]);
Where numArray is a parameter to the function blowUpArray which is called like this:
res = blowUpArray(arr1,arr2,5);
Where arr is an array local to the main function. Because this array was declared as a local variable, you can't pass it to free.
You probably wanted to pass blownUp to reallocateArr:
reallocateArr(blownUp,currSize,amountArray[i]);
You should also initialize this variable to NULL:
int* blownUp = NULL;
So you can safely pass it to free on the first iteration of the loop in blowUpArray.

Debug Error! HEAP CORRUPTION DETECTED in C

I tried to use dynamic allocation and I didnt sucsses to write it without warnings. I get debbug error with the 'free' function:
This is my program:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define size 1
int* symetri(int set1[size], int set2[size]);
int main() {
int* difference;
int set1[size], set2[size];
for (int i = 0; i < size; i++)
scanf("%d", &set1[i]);
for (int i = 0; i < size; i++)
scanf("%d", &set2[i]);
difference = symetri(set1, set2);
free(difference);
return 0;
}
int* symetri(int set1[size], int set2[size])
{
int* new_arr = (int*)malloc(size * sizeof(int));
while (new_arr == NULL)
new_arr = (int*)malloc(size * sizeof(int));
int counter = 0;
bool sym;
for (int i = 0; i < size; i++) {
new_arr[counter] = set1[i];
counter++;
}
new_arr[counter] = -1;
return new_arr;
}
If free crashes with an error message about heap corruption in a program this simple, it's almost certainly because you wrote to memory that doesn't belong to you. And you did, with this line at the end of symetri:
new_arr[counter] = -1;
You need to make your allocation of new_arr another sizeof(int) longer if you want to write to the end after looping over it.

dynamic memory allocation of 2D array for a parallel MPI algorithm of a large size problem (from 32768) in C

I am facing to an issue when I want to allocate a memory for a problem of size n = 32678 a 2D array of size tab[2n][n+1]. This is a code :
#include <stdio.h>
#include <stdlib.h>
int main(){
int n = 32768;
unsigned int** tab = (unsigned int**) malloc(2 * n * sizeof * tab);
if(tab == NULL){
printf("insuffisant memory\n");
exit(EXIT_FAILURE);
}
tab[0] = (unsigned int*) malloc(2 * n * (n+1) * sizeof * tab[0]);
if(tab[0] == NULL){
printf("insuffisant memory\n");
exit(EXIT_FAILURE);
}
for(int i = 1; i < 2 * n; i++)
tab[i] = tab[0] + i * (n+1);
tab[2*n - 1][0] = 0;
}
The last instruction end up to an error:
Segmentation fault (core dumped)
I don't understand what happen because I don't have problem when n = 1 to 32767. From n = 32768 I have this error.
Please do you understand what happen?

Freeing realloc causing error

I am trying to free my memory allocated in the following program:
include <stdio.h>
#include <stdlib.h>
void main(int argc, char** argv) {
int* x = (int *) malloc(4 * sizeof(int));
int i;
for(i = 0; i < 4; i++) {
x[i] = i;
printf("X[%d] = %d\n", i, x[i]);
}
x = (int *) realloc(x, 4 * sizeof(int));
for(i = 0; i < 8; i++) {
x[i] = i;
printf("X[%d] = %d\n", i, x[i]);
// free(x[i]);
}
free(x);
}
I get the following:
libC abort error: * glibc detected ./a.out: free(): invalid next size (fast): 0x00000000014f8010 **
i tried to free inside the last loop, but that still gives a problem. Where am I going wrong?
realloc() does not increase the allocated memory by the specified amount. It reallocates exactly the requested amount of bytes. So if you want to resize your array to be 8 integers long, you'll need to:
x = realloc(x, 8 * sizeof(int));
Btw, do not cast the returned value of malloc() and realloc() in C:
http://c-faq.com/malloc/mallocnocast.html

Realloc error in C

I've just started C read the man page but could not find the proper answer. So the code is below
void *p = malloc(10*sizeof(int));
int *q = p;
int NUMOFINT = 10;
for (int i = 0; i < NUMOFINT; i++){
printf("%i ", q[i]);
}
void *realloc(void *p, 20*sizeof(int));
for (int i = 0; i < 21; i++){
printf("%i ", q[i]);
and it is giving this error:
malloc.c: In function ‘main’:
malloc.c:31:24: error: expected declaration specifiers or ‘...’ before numeric constant
I did not quite understand that the size format was ok for malloc() but not ok for realloc. So how do I correct the error?
Edit:
so when I make it as:
void *morep = realloc(p, 20*sizeof(int));
int *q2 = morep;
for (int i = 0; i < 20; i++){
printf("%i ", q2[i]);
}
it prints out q2[11] as 135121
This
void *realloc(void *p, 20*sizeof(int));
is wrong. You want
p = realloc(p, 20*sizeof(int));
Incidentally, this line is also a problem (but will compile fine):
for (int i = 0; i < 21; i++){
You allocated p to 20*sizeof(int) bytes, so element 20 is past the end of the array. You want:
for (int i = 0; i < 20; i++){
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
int main(int argc, char **argv)
{
int *temp; /* To use with realloc, it's more secure */
void *p = malloc(10*sizeof(int));
int *q = p;
int NUMOFINT = 10;
/* dont forget to compile with -std=99 flag, if you use this kind of for loop */
for (int i = 0; i < NUMOFINT; i++){
printf("%i ", q[i]);
}
printf("\n");
/* It's more secure to use this kind of realloc */
temp = realloc (p, 20 * sizeof(int) );
if (temp == NULL)
{
fprintf(stderr,"Reallocation failed --> %s\n", strerror(errno));
free(p);
exit(EXIT_FAILURE);
}
else
{
p = temp;
}
/* Zeroing after realloc */
for (int i = NUMOFINT; i < 21; i++)
q[i] = 0;
for (int i = 0; i < 21; i++)
{
printf("%i ", q[i]);
}
printf("\n");
return 0;
}
realloc can tries to expand the existing memory block in heap, if its not possible it will allocate a new separate block of memory for the new size and it will copy the data from old memory block to the new memory block also.
So if realloc returns the address same as p then it just expands the old memory block. Or else we have to free the old memory block and start using the new memory block returned by realloc.
#define NUMOFINT 10
#define NUMOFINT_NEW 20
void *p = malloc(NUMOFINT * sizeof(int));
int *q = p;
void *temp = NULL;
for (int i = 0; i < NUMOFINT; i++)
{
printf("%i ", q[i]);
}
temp = realloc(p, NUMOFINT_NEW * sizeof(int));
if (temp == NULL)
{
exit(0);
}
else if (temp != p)
{
free(p);
p = temp;
}
//else temp is equal to p
for (int i = 0; i < NUMOFINT_NEW; i++)
{
printf("%i ", q[i]);
}
Here q[0] to q[9] will have the values(0, 1, 2..9) assigned, and the rest (q[10] to q[19]) will have garbage value. We have not memset the newly allocated memory to 0 also.
you can memset also before for loop,
memset((q + NUMOFINT), 0, (NUMOFINT_NEW - NUMOFINT));
Mistakes in your program are
1) compilation error because
void *realloc(void *p, 20*sizeof(int)); This statement doesn;t looks like a function call.
2) for (int i = 0; i < 21; i++) - After memory is realloced to 20 elements, you can access only upto 19th, accessing 20th may leads to crash(undefined behaviour).
3) it prints out q2[11] as 135121 - Newly reallocated memory is neither memset to 0 nor assigned any meaningful values.

Resources