C: An array declared inside the can not function exceeds ~ 8 MB of memory.

What am I doing wrong? When using Eclipse on a Mac (2GB RAM), I ran into the following problem:

Whenever I try to create an array that exceeds 8384896 bytes, I get segmentation errors. The following program will be executed:

#include <stdio.h>

main()
{
    double x[1048112];
    printf("sizeof(x) = %li", sizeof(x));
}

and the output will be (as expected):

sizeof (x) = 8384896

But increasing the number of elements in x or creating additional variables in main () will lead to an unexecutable program and segfaults. Looks like I'm pushing some memory limit, and I don't understand why this is happening. I would be very grateful if someone could explain this to me, or maybe offer some solution to my problem.

+3
8

- .

- , , malloc:

double *x = malloc(1048112 * sizeof(double));

, sizeof(x) , double *. , , .

, , free, :

free(x);
+14

OS X 8 ( ulimit -s ).

- , - ulimit -s 65536. , .

:

 double *x = (double*)malloc(9999999)

, , : free(x)

EDIT: , OS X. , . .

+7

:

 static double x[1234567];

. , , .

, , , " " , , , .

+3

Yup, ... , Stack Overflow ing. , ? ... :

double *x=malloc(1048112*sizeof(double));
+2

malloc , :

#include <stdio.h>

static double x[1048112];
main()
{

    printf("sizeof(x) = %li", sizeof(x));
}

, , , , .

+2

, , , .

0

. , ( " " ) . , , "", . , ( "" ), , . , ( , "" ) .

0
source

As you know, this limit in windows is 1 mb

this code works

    void myfunction ()
    {
    static char yes [1100000] // allocated in the heap
    }

this code does not work

    void myfunction ()
    {
    char yes [1100000] // allocated in the stack
    }

0
source

Source: https://habr.com/ru/post/1720393/


All Articles