C creating a struct array beyond certain size causes a crash

1

Hi all I am really new to C (just started this week), and want to make sure that I am not looking down the wrong rabbit home and hoping to perhaps get pointed to the right rabbit hole.

I create a struct:

#define MAX 64
#define ARRAY_SIZE 2048

struct object {
    int q, c, p;
    char name[MAX]; //Stores string up to 63 characters
    char arr[ARRAY_SIZE][MAX]; // Creates an array of 2048 cells with string of length 63 max
};

int main(){
...
...
int variable = 30;
struct object l[variable]; //This is where the crash happens. But only when either variable is too large (for instance works on 15 just fine, but anything over 20 it crashes), or when Array_SIZE is too larger, for instance works fine with 1024 but 2048 crashes.
...
...
}

The error I get on crash is the following: Process returned -1073741571 (0xC00000FD) in the cmd window. And the following in the debugger in the IDE: Program received signal SIGSEGV, Segmentation fault. [Inferior 1 (process 12120) exited with code 030000000375]

Am I doing something obviously wrong with how I declare an array of structs? Why would large numbers not work but lower numebrs work?

Does the above error indicate I am accessing something out of bounds somewhere? Ive been up and down the code and cant seem to find any reason why larger numbers dont work and lower ones do. My memory footprint doesnt seem to be the issue, just a few megs of memory.

I need help with what to look for (I cant find any instances of accessing anything out of bounds, so I get the feeling Im not chasing the right rabbit and need to look for something else)? Or maybe Im doing something illegal for C without knowing it?

c
arrays
struct
asked on Stack Overflow Jan 12, 2018 by Duxa • edited Jan 12, 2018 by Duxa

3 Answers

2

You have a memory size problem, try to increase the memory size for your program.

And if you want to use big array size and so allocate a lot of memory, you shouldn't allocate statically but dynamically. So you should use malloc

typedef struct object {
  int q, c, p;
  char name[MAX]; //Stores string up to 63 characters
  char arr[ARRAY_SIZE][MAX];
} myObject ;

int variable = 30;
myObject *l = malloc(sizeof(myObject) * variable);

I do not advice you to declare an array of 2048 statically, so you should initiate your struct with a function.

typedef struct object {
  int q, c, p;
  char name[MAX]; //Stores string up to 63 characters
  char *arr[MAX];
} myObject ;

myObject    *createNewObject() {
    myObject *toReturn = malloc(sizeof(myObject) * variable);

    if (toReturn == NULL)
       return NULL;
    toReturn->arr = malloc(sizeof(char) * ARRAY_SIZE);
    return (toReturn);
}

void        freeMyObject(myObject *objectToFree)
{
    if (objectToFree && objectToFree->arr =! NULL)
       free(objectToFree->arr)
    if (objectToFree)
       free(objectToFree)
}

void main()
{
    myObject *myNewObj = createNewObject()

    // You can do some stuff with myNewObj but always verify the pointers
    freeMyObject(myNewObj);
}

You should also debug with valgrind when you works with malloc, so you don't have memory loss or problems.

Hope I helped

answered on Stack Overflow Jan 12, 2018 by Sacha.R
2

I think your program crashes because you statically allocate too much memory on the stack. Try using the malloc or calloc function. It dynamically allocates memory on the heap instead e.g. :

struct object *l = malloc(variable*sizeof(struct object));

Don't forget to free it afterwards using the free function.

free(l);

answered on Stack Overflow Jan 12, 2018 by nabil.douss
1

Well the problem you had is - you have used automatic memory allocation. Due to constraint of size of automatic storage your program crashed - you asked for more than you should.

So what is the solution?

  1. Static memory allocation:

Solution being

     static struct object l[variable];
  1. Dynamic memory allocation

    struct object *ptr = malloc( sizeof *ptr * variable);
    

The storage of these allocation is different from automatic variables - so free from the size constraint . In this case you have to free the dynamically allocated memory. That's why you will get around the problem you have.


Statically allocate is not a confusion free term. All these types of variable will have different scope - lifetime. Standard never mentions about stack or heap . It is the implementation that follow these to store automatically allocated memory and dynamically allocated memory.

answered on Stack Overflow Jan 12, 2018 by user2736738

User contributions licensed under CC BY-SA 3.0