Which issues have you encountered due to sequence points in C and C++?

后端 未结 6 492
栀梦
栀梦 2021-02-04 20:39

Below are two common issues resulting in undefined behavior due to the sequence point rules:

a[i] = i++; //has a read and write between sequence points
i = i++;          


        
6条回答
  •  走了就别回头了
    2021-02-04 21:20

    An example similar to Dario's, which I've also seen people fall into:

    printf("%s %s\n", inet_ntoa(&addr1), inet_ntoa(&addr2));
    

    Not only will this either print "addr1 addr1" or "addr2 addr2" (because inet_ntoa returns a pointer to a static buffer overwritten by further calls), but also it is not defined which of these will be the case (because C does not specify order of evaluation in argument lists).

提交回复
热议问题