Why am I getting segfault when changing the signature of main?

后端 未结 4 1608
青春惊慌失措
青春惊慌失措 2021-01-21 06:12

I am trying to get my feet into C, and wrote this program that displays a kb of my RAM in a random location. Here is the code, and it works fine:

#include 

        
4条回答
  •  悲哀的现实
    2021-01-21 07:01

    The value of mem is undefined (not initialized), but not random. If before main is called, other C runtime functions, are called, then the slot of stack used by mem may have a valid pointer within it. Adding parameters to main changes which slot is used and changes behaviour. This can mean the code doesn't crash, although it is not correct.

提交回复
热议问题