free memory (0xc0000005)

Hi
It seems that I have a problem with the memory handling. Everything works but stops after several iterations with the code 0xc0000005. I have two 2D vectors, I am allocating the memory for the pointer to them, do the calculation, returning the result, freeing the memory. It works for ~15 iterations, and then I get the error. Could someone see the problem?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
   struct bd bd1;
    struct bd bd2;

    bd1.numpoints = nvrtx1;
    bd1.coord = (double **) malloc(8 * sizeof(double *));

    for (int32_t i = 0; i < 8; i++)
    {
        bd1.coord[i] = (double *) malloc(3 * sizeof(double));
    }

    bd2.numpoints = nvrtx2;
    bd2.coord = (double **) malloc(8 * sizeof(double *));

    for (int32_t i = 0; i < 8; i++)
    {
        bd2.coord[i] = (double *) malloc(3 * sizeof(double));
    }

    for (int32_t i = 0; i < 8; i++)
    {
        for (int32_t j = 0; j < 3; j++)
        {
            bd1.coord[i][j] = cppdata1[i][j];
        }
    }

    for (int32_t i = 0; i < 8; i++)
    {
        for (int32_t j = 0; j < 3; j++)
        {
            bd2.coord[i][j] = cppdata2[i][j];
        }
    }
          //do the function for both 2D vectors
       dd = gjk(bd1, bd2);

         //free memory and return the result

    for (int32_t i = 0; i < 8; i++)
    {
        free(bd1.coord[i]);
    }
    free(bd1.coord);

    for (int32_t i = 0; i < 8; i++)
    {
        free(bd2.coord[i]);
    }
    free(bd2.coord);


    if (dd < 1E-6)  return true;

    return false;
where exactly does it crash in the debugger?
like 36 seems likely.

why is this memory dynamic?
when you hard code the sizes, use an array. you are making a headache for yourself.
what is nvrtx? Number of vertex? It isn't being used. Should you malloc this value instead of 8 or 3 or something??

wordy return (not a bug, just wordy).
why not
return (dd<1E-6);
Last edited on
Thanks for your comments. I found the error accidently in the different part of the code. I was confused as I thought it is a memory related error and it must be related with the dynamic memory and this code is the only part where I use it.

It is dynamic because the function which I want to use uses the dynamic memory/pointers, so I need to adjust my code to that. I am beginner, and I have some sort of troubles when I use the array, I find vectors much more convenient. The whole code will be updated reg things you mentioned, it is just as it is due to my naive trying to adapt it to my other code.
I am beginner, and I have some sort of troubles when I use the array, I find vectors much more convenient.
Why aren't you using vectors, then?

You're probably corrupting the heap at some point in the execution of the main logic by writing outside the bounds of an array, which causes the implementation of malloc() and free() to get confused and crash. The code that causes that corruption could be anywhere in the program; it's not necessarily here just because it was here that it crashed.
nvrtx2;
Another unfortunate case of a keyboard's vowel keys malfunctioning.
yes, anyone using ** as a parameter to a function has created a monster. The ways around avoiding it are not always worth it.
however consider making your pointers static and allocate them once if you call the function a lot of times. That function spends 90% of its time doing memory stuff, so if you get into this kind of problem where performance matters, find a way to avoid the constant allocation / free cycle.

Glad you found it!
Topic archived. No new replies allowed.