Trying to create multiples small multi-dimensional arrays with malloc

0

I'm trying to allocate two multi-dimensional arrays from a struct with a function. Allocating the first works perfectly but the second fails, even though the code for the two arrays is exactly the same.

I thought it would be because I don't have enough memory, but I have 16GB of RAM and this doesn't even take a kilobyte.

If it can help I'm using CLion and C98. I also tried to launch the exported program without CLion in case it's coming from it.

main.c

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

#include <math.h>
#include <string.h>

#include "include/calc.h"

int main(int argc, char **argv)
{
    calc calcObject = InitCalcObject();
}

calc.h

typedef struct CalculationObjectElement {
    int ** braketArray; // <--- First array, correct allocation
    int braketCount;
    int braketHighestPriority;

    int ** squareBraketArray; // <--- Second array, fails
    int squareBraketCount;
} CalculationObjectElement, *calc;

calc InitCalcObject();

calc.c

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

#include <math.h>
#include <string.h>

#include "include/calc.h"

calc InitCalcObject()
{
    calc calcReturn = malloc(sizeof(calcReturn));

    if (calcReturn == NULL) {
        FailureHandler("Malloc - Calc object");
        return NULL;
    }

    int i;

    calcReturn->braketArray = malloc(sizeof(int *) * 256); // Working great
    calcReturn->squareBraketArray = malloc(sizeof(int *) * 256); // Working great

    for (i = 0; i < 256; i++) {
        calcReturn->braketArray[i] = malloc(sizeof(int) * 2); // Working like a charm

        if (calcReturn->braketArray[i] == NULL) {
            FailureHandler(strcat("Malloc - Calc object - braketArray", (char *) i));
            return NULL;
        }

        calcReturn->squareBraketArray[i] = malloc(sizeof(int) * 2); // Not working

        if (calcReturn->squareBraketArray[i] == NULL) { 
            FailureHandler(strcat("Malloc - Calc object - squareBraketArray", (char *) i));
            return NULL;
        } //There wasn't this failure handler at first, added it desperately trying to solve this
    }

    return calcReturn;
}

For sure, I'm trying to debug it and get :

Program received signal SIGSEGV, Segmentation fault.
InitCalcObject () at F:\Codage\C\Projects\Calculator\src\calc.c:33
33          calcReturn->squareBraketArray[i] = malloc(sizeof(int) * 2);

Two times before it just crashed (0xC0000005).

c
arrays
multidimensional-array
segmentation-fault
malloc
asked on Stack Overflow Nov 18, 2019 by Jules RC • edited Nov 18, 2019 by John Kugelman supports Monica

1 Answer

0

Thanks to @Yano, I finally found the solution, and man it's ridiculous... I'm posting this answer so everyone that fall on this and has the same problem can fix it.

As Yano said in the comments under the answer, I haven't allocated all the space for the struct, but only a pointer to it...

I don't know how I made this mistake, maybe I should go to sleep xD

So if you did like me, a typedef of a struct with a pointer (This mean that your typedef replace structElement by *structElement) you need to be sure to allocate not the pointer for the struct, but all the pointers and variables inside the struct, this mean the struct itself. So if you do a malloc() with a sizeof(), do not do it with the pointer of it (Si the typedef you did).

Here's an example :

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

// Creating the struct //
typedef struct structElement { 
    int Var1;
    int ** TwoDimensionsArray;
    float Var2;
} structElement, *pointerStructElement; 
// Doing a typedef so pointerStructElement means a pointer of the structElement itself

int main(int argc, char **argv)
{
    pointerStructElement structTest; // Declaring a POINTER of the struct

    structTest = malloc(sizeof(structElement)); 
    /* 
       Allocating memory for the size of the struct and NOT the pointer of the 
       struct, it's where I got it wrong at first
    */

    printf("structTest correctly allocated !!!");

    free(structTest);

    return 0;
}

I really hope I helped some people with that, pointers are hards things to understand and you can always makes small mistakes like that.

Thanks you again Yano :)

Have a great day, Jules.

PS : Also thanks everyone else for your help :)

answered on Stack Overflow Nov 19, 2019 by Jules RC • edited Nov 19, 2019 by Jules RC

User contributions licensed under CC BY-SA 3.0