Large char buffers for use in C

I need to save a “large” file (say, a little less than 2 MB) in memory, and I want to know if it’s correct to use

char buffer[2048000];

Is there a better way to handle this? Any performance issues when using large char arrays?

+4
source share
4 answers

Yes, there may be problems, most C implementations will allocate this buffer on the stack, if it is an automatic storage time variable, and the amount of "stack memory" available for use can be surprisingly small.

Best used mallocfor anything more than a few kilobytes. Remember to call freeat the appropriate point.

static - .

+4

, . , .

, . . , malloc mmap .

+4

:

1) . , . , , .

2) ( malloc). .

3) . , . .

+3

, 2048000 . , 2048000 , . , , , , .

- malloc . , NULL ( ). , , , .

, ( , ), char. , . , .

, 2 , 10 200 , , .

, , ( ), : . , , , , , , , , , , , .

+1

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


All Articles