Why does malloc() fail when there is enough memory?

后端 未结 5 1543
遇见更好的自我
遇见更好的自我 2021-01-08 00:21

I\'m using a server with 128GB memory to do some computation. I need to malloc() a 2D float array of size 56120 * 56120. An example code is as follows:

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-08 00:56

    float ls[3149454400]; is an array with automatic storage type, which is usually allocated on the process stack. A process stack is limited by default by a value much smaller than 12GB you are attempting to push there. So the segmentation fault you are observing is caused by the stack overflow, rather than by the malloc.

提交回复
热议问题