0

While trying to debug a fairly large program, I got a segmentation fault when creating a struct. For this reason I made another c file with a reduced version of the code to see if I could reproduce the crash, and noticed that the crash happens when I create a struct.

Here is the code:

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

#define SIZE 10000

typedef struct {
    char* type;
    char* name;
    int status;
}VARIABLE;

typedef struct {
    char* fun;
    char* t;
    char* args[SIZE];
}FUNCTION;

typedef struct {
    int gf;

    VARIABLE vars[SIZE];
    FUNCTION funs[SIZE];
}COMPILER;

int main(int argc, char** argv) {   
    COMPILER compiler;
    return 0;
}

I solved the issue by substituting

COMPILER compiler;

with

COMPILER* compiler = (COMPILER*)malloc(sizeof(COMPILER));

The only thing that weirds me out is that when I tried to debug the first version of the code with gdb it gave me a segfault at argc and argv, with this message:

0x0000555555555160 in main (
    argc=<error reading variable: Cannot access memory at address 0x7fffd04abc4c>, 
    argv=<error reading variable: Cannot access memory at address 0x7fffd04abc40>)
    at main.c:26
26  int main(int argc, char** argv) {

Why would it give me the segfault here and not at compiler's declaration? I'm not good with C yet and I'm trying to understand how to debug programs better. Thanks in advance

Leon9343
  • 1
  • 1

0 Answers0