0

I got and error which says

Debug assertation failed and heat corruption detected

like everything is working good in my program but I get that error. Where is the memory leak here? I have to free that memory in the main because my functions need to return pointers.

My code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int *dynamic_reader(unsigned int n) {

    /*making new array and checking if allocation succeeded*/
    int *mem;
    mem = malloc(n * sizeof(int));
    if (!mem) {
        printf("Memory allocation failed\n");
        exit(-1);
    }

    /*letting the user to input the values for the array*/
    int i = 0;
    while (i < n) {
        scanf("\n%d", &mem[i]);
        i++;
    }

    /*printing the array just to make sure everything is good*/
    for (int j = 0; j < n; j++) {
        printf("%d  ", mem[j]);
    }

    return mem;
}
int *insert_into_array(int *arr, unsigned int num, int newval) {

    /*making new bigger array*/
    int *newarr = realloc(arr, (num + 1) * sizeof(int));

    /*adding the integer to this new array */
    newarr[num] = newval;
    printf("\n");

    /*printing to make sure everything is correct*/
    for (int j = 0; j < num + 1; j++) {
        printf("%d  ", newarr[j]);
    }
    return newarr;
}
int main(void) {
    /*In dynamic_reader function I need to make an array which size is given as a parameter*/
    /*In this case I choosed 3*/
    int *arr = dynamic_reader(3);
    int num = 3;
    /*In insert_into_array function I need to add one integer to this array I made in dynamic_reader*/
    /*The parameters are the array, the number of elements in the array already done and the integer I want to add*/
    int *c = insert_into_array(arr, num, 9);

    /*I free the memory here because I need to return the pointers of these arrays in the function so it cant be done there*/
    free(arr);
    free(c);
}
Pang
  • 9,564
  • 146
  • 81
  • 122
michael
  • 87
  • 7
  • Do you know if there's a memory leak? Have you looked at your system's memory when you run the program? – Matthew Kerian Feb 21 '18 at 21:56
  • 2
    Nothing in that message says *memory leak*. It says **heap corruption**. **Corruption** is not **leak**. Something you're doing before calling free() is making the memory invalid. Use the debugger to figure out what your code is doing wrong. – Ken White Feb 21 '18 at 21:58
  • `int *newarr = realloc(arr, (num + 1) * sizeof(int));` This looks bad. – Christian Gibbons Feb 21 '18 at 22:00
  • Once you do the `realloc(arr, ...`, the pointer `arr` is no longer valid, so you can't free it. The usual way one uses realloc() is to reuse the same pointer, i.e., `arr = realloc(arr, ...`. – Lee Daniel Crocker Feb 21 '18 at 22:05
  • Its also worth mentioning you usually don't want to be constantly reallocating an array every time you add another element. It's typical to grow an array to twice it's previous size when the number of elements grow beyond the length of the array, thus minimizing the frequency of costly memory operations. – Christian Gibbons Feb 21 '18 at 22:12
  • Do you have some better way to add an element than this @ChristianGibbons? Would like to hear it! – michael Feb 21 '18 at 22:39
  • Did it say *"**heat** corruption detected"* or *"**heap** corruption detected"*? – Pang Feb 22 '18 at 02:08
  • @michael Here's a link to an existing example on StackOverflow: https://stackoverflow.com/a/3536261/8513665 – Christian Gibbons Feb 22 '18 at 19:07

3 Answers3

3

You are double freeing your memory. Check the documentation for realloc. Realloc will either 1) expand the passed buffer or 2) will allocate a new buffer, copy the data, and free the original buffer. When you do:

free(arr);
free(c);

You are double freeing a value that was either once already freed by realloc or already freed by the first free(arr)

Additionally, you should check if realloc fails (returns NULL) and if so, handle the case appropriately.

MFisherKDX
  • 2,840
  • 3
  • 14
  • 25
1

First you malloc an array, which you return to your main function as arr. Then in another function, you realloc where arr is an argument to the realloc, but some other pointer stores the results. Depending on what happened in realloc, you've either got arr and newarr pointing to the same location, or newarr pointing to a valid location and arr pointing to an invalid location that has been freed. Either way, freeing both of them at the end is a problem.

Christian Gibbons
  • 4,272
  • 1
  • 16
  • 29
1

No need to free(arr), that is taken care of when you realloc() it. The pointer returned by realloc() will either point to memory which includes the original memory, or free the old memory after copying its content to a new, larger chunk of memory.

Stephen Docy
  • 4,738
  • 7
  • 18
  • 31