A. Kwas
A. Kwas

Reputation: 27

Maximum size of array of structs

I try to make array of structs. Struct contain two 2-dimensional arrays (100x100). If i want to make array of 30 or more structs the error occur - "Segmentation fault". I'm using CodeBlocks with MINGW compiler. Code:

struct gracze
{
    int pole1[100][100];
    int pole2[100][100];
};


int main()
{
    gracze gracz[30];

return 0;
}

Upvotes: 2

Views: 2730

Answers (2)

DevSolar
DevSolar

Reputation: 70263

You are allocating about 2.4 to 4.8 megabytes of stack space.

IIRC, the default stack size of MinGW is about 2 MB, i.e. you are exceeding your stack space, and need to increase it -- if, indeed, you want to go with such a large stack allocation instead of e.g. dynamically allocating the memory:

#include <stdlib.h>

int main()
{
    gracze * gracz;
    if ( ( gracz = malloc( sizeof( gracze ) * 30 ) ) != NULL )
    {
        // stuff
        free( gracze );
    }
    return 0;
}

If that is not what you are looking for, this SO question covers the "how" of increasing stack space (gcc -Wl,--stack,<size>).

On Linux, there is also the global limit set by ulimit -s.

This SuperUser question covers Windows / VisualStudio (editbin /STACK:reserve[,commit] program.exe).

Upvotes: 2

JSF
JSF

Reputation: 5321

You are doing a large allocation on the stack. Either increase the stack size, or allocate the object some other way (static or malloc).

See

DevC++ (Mingw) Stack Limit

Upvotes: 1

Related Questions