What was the most dangerous programming mistake you have made in C?

前端 未结 26 1174
南方客
南方客 2021-02-03 12:46

I am an intermediate C programmer. If you have made any coding mistake that you came to know later that it was the most hazardous / harmful to the total application please share

26条回答
  •  北荒
    北荒 (楼主)
    2021-02-03 13:22

    Two things comes to mind. Ths first was a function in embedded C (MCU) i tried to have some restrictions on an timer value as a entered a function. so I wrote

    if(55000 < my_var < 65000)
    

    My ida was to check like this:

    if( (55000

    But this is the equivilant or the result

    if( (55000

    and the result ended up that the if test was always true.

    The secound was a pointer mistake. (simplyfied presented here)

    get_data(BYTE **dataptr)
    { 
      ubyte* data = malloc(10);
      ... code ...
      *dataptr = &data[1];
    }
    
     main()
     {
       BYTE *data
       get_data(&data);
       free(data);
     }
    

    Thus resulting in a loss of 1 byte of memory for each time the get_data() function was called

提交回复
热议问题