C - malloc allocating too much memory

前端 未结 3 807
小蘑菇
小蘑菇 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:42

    malloc is actually allocating exactly the right amount.

    However, you're accessing memory beyond the allocation.

    What exists there is completely undefined and could really be anything.

    In your case, it was one "junk" number and four zeroes.

提交回复
热议问题