Using malloc is giving me more memory than expected?

前端 未结 3 1771
甜味超标
甜味超标 2021-01-22 23:12

I\'m trying to get to grips with malloc, and so far I\'m mostly getting unexpected results when testing and playing around with it.

int main(int arg         


        
3条回答
  •  逝去的感伤
    2021-01-22 23:27

    The codes runs without an error, but it is still wrong. You just do not notice it. Your loop runs out of the allocated area, but the system remains unaware of that fact until you run out of a much larger area your program can potentially access.

    Picture it this way:

    1234567890
    

    Your 10 ints are in the middle of other data, which you can read and even write - to very unpleasant effects. C is not holding your hand here - only once you go out of the total available memory, the program will crash, not before.

提交回复
热议问题