C : malloc seems to allocate more then i'm requesting (array)

前端 未结 4 1951
青春惊慌失措
青春惊慌失措 2021-01-15 22:48

Hi I have this question: Why when I\'m allocating memory with malloc for a float array, it allocate more space that i\'m requesting? For example in this code i\'m trying to

4条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-15 23:24

    You can only get a segmentation fault if a read or write falls in the wrong page. A page is typically 4KiB.

    The malloc() system call may reserve a bit more space than requested, and it reserves a few words of memory before the block for book-keeping, but that your program does not get killed when accessing fk_array[15] does not mean that it did not access outside the block reserved for it. It probably did, the OS just didn't notice it. If you had allocated another array after allocating fk_array, you would have had a chance to notice that it was changing.

提交回复
热议问题