C - malloc allocating too much memory

前端 未结 3 920
小蘑菇
小蘑菇 2021-01-27 00:39

running int a strange scenario where malloc is allocating more memory than I ask for:

void function (int array [], int numberOfElements) {

int *secondArray = ma         


        
3条回答
  •  半阙折子戏
    2021-01-27 00:41

    You are just lucky. Malloc can and sometimes does ask from more memory off the OS - Taking into account paging. Sometimes it does not even need to ask the OS for memory as it has asked for extra earlier. Therefore the malloc could ask for a page of memory - more that enough to satisfy your request and the extra memory happens to be filled with zeros.

    You are in the land of undefined behaviour. So all bets are off.

提交回复
热议问题