I noticed that when working with some large arrays (for example, it doubles 1000 per 1000) my program runs out of memory if I declare an array inside main (), but this never happens if I declare an array outside of main () even for large arrays . Can someone explain what the difference is?
When a variable is declared inside a function (in your case main), it is allocated on the stack, and if it is too large (for example, a large array), you will encounter a stack overflow .
main
, , . , .
, . , .
, , , . , main(), . ( ) , - ++, .
main()
, , , automatic, , .
automatic
, , static , , .
static
. .
, malloc new. ( C, ++, - ++).
malloc
new
(main - ), . 1000x1000x8 = 8 . , , . , , - 1 .
( ) , , , .
: , . /, , bss . . .
, ( ) ( , , ), main puts , , ( ).
the stack size is about 2-3 M, and the heap size is associated with virtual memory. It can be very big. you can declare it as follows:static int num[1000][1000];
static int num[1000][1000];
Source: https://habr.com/ru/post/1538680/More articles:How to add class style or id or CSS to execCommand formatBlock 'p' tag? - javascriptAuthentication of Google Glass GDK using PHP - phpОшибка записи в установочный сокет при развертывании с помощью capistrano3 - gitReason for including "-ObjC" in Xcode - iosException from reading and writing text file memory - c #Google API 1.8.1 - GoogleWebAuthorizationBroker.AuthorizeAsync зависает под управлением Windows - google-oauthnodejs connecting to db only once - databaseJasmine testing an AngularJs application using angular translators - angularjsIs it possible to find out the installation and unmounting time of a device in Ubuntu? - linuxConsequences of using paddingLeft on android 4.2+ with RTL - androidAll Articles