[Solved] malloc: *** error for object 0x147606ac0: pointer being realloc’d was not allocated

Introduction

The malloc: *** error for object 0x147606ac0: pointer being realloc’d was not allocated is a common error encountered when attempting to reallocate memory in C programming. This error occurs when a pointer is passed to the realloc() function, but the pointer was not allocated with malloc() or calloc() beforehand. This error can be caused by a variety of issues, such as passing an invalid pointer to realloc(), or attempting to reallocate memory that has already been freed. In this article, we will discuss the causes of this error and how to solve it.

Solution

This error occurs when a pointer is passed to realloc() that was not allocated with malloc() or calloc(). To fix this, make sure that the pointer being passed to realloc() was allocated with malloc() or calloc().

The function is not updating the caller’s value of client_details, you need one more level of pointer:

void func(struct client_detail **client_details,int *size){
    *client_details = realloc(*client_details, (*size + 1) * sizeof(**client_details));
    *size = *size + 1;
}

int main()
{
    struct group_detail *group_details = malloc(0 * sizeof(*group_details));
    struct client_detail *client_details = malloc(1 * sizeof(*client_details));
    int size = 0;
    int j = 0;
    while(j < 100){
        func(&client_details,&size);
        j++;
    }
    return 0;
}

Similar as with size, if you just wanted the value, the type would be int. Changing an int size variable inside the function would not have any effect on the caller. You want to change it, so you make a pointer to int int * and dereference it to read/write the value.

With void func(struct client_detail *client_details,int *size){ you are getting the client_details pointer by value and changing that will have no effect on the caller’s variable. You need to add a pointer-to-it similar as was done for size.

0

solved malloc: *** error for object 0x147606ac0: pointer being realloc’d was not allocated

Solving the malloc Error: pointer being realloc’d was not allocated

If you’re a programmer, you’ve probably encountered the dreaded malloc error: pointer being realloc’d was not allocated. This error can be frustrating and difficult to debug, but it’s actually quite simple to solve. In this article, we’ll explain what this error means and how to fix it.

What Does the malloc Error Mean?

The malloc error occurs when you try to reallocate memory that has not been allocated. This means that you are trying to use memory that has not been allocated to your program. This can happen if you are trying to access memory that has already been freed, or if you are trying to access memory that has not been allocated yet.

How to Fix the malloc Error

The first step to fixing the malloc error is to identify where the error is occurring. This can be done by using a debugger to trace the program’s execution. Once you have identified the source of the error, you can then take steps to fix it. This may involve changing the code to ensure that the memory is allocated before it is used, or it may involve freeing the memory that has already been allocated.

Once you have fixed the error, you should also make sure to test your code thoroughly to ensure that the error does not occur again. This can be done by running the program in a debugger and checking for any other errors that may occur.

Conclusion

The malloc error: pointer being realloc’d was not allocated can be a frustrating and difficult error to debug. However, by understanding what the error means and how to fix it, you can quickly get your program running again. Just remember to test your code thoroughly to ensure that the error does not occur again.