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

前端 未结 4 1918
青春惊慌失措
青春惊慌失措 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:18

    I would not be surprised if allocating heap memory does not fall into specific processor rules that addresses must be aligned on a long, quad, or other boundary.

    So, if you requested 100 bytes of heap space, the OS may return more than you requested to satisfy alignment requirements of the processor. However, accessing more than the 100 bytes is considered undefined and can and probably will result in a lot of strange behavior you do not want.

提交回复
热议问题