Debugging programs c

Programming is in a sense easy. But mistakes are what always cause more problems. Can someone help me with some good debugging tricks and tools in c?

+3
source share
11 answers

Popular debugging tools are listed below.

Some very simple tricks / suggestions

-> Always check that nowhere in your code have you dereferenced a wild / sagging pointer

Example 1)

int main()
{
     int *p;
     *p=10; //Undefined Behaviour (crash on most implementations)
}

Example 2)

int main()
{

     int *p=malloc(sizeof(int));
     //do something with p

     free p;
     printf("%d", *p); ////Undefined Behaviour (crash on most implementations)
}

-> Always initialize variables before use

 int main()
  {

    int k;

    for(int i= k;i<10;++i)
              ^^
            Ouch
      printf("%d",i");
  }
+2
source

From "Elements of a Programming Style," Brian Kernigan, 2nd Edition, Chapter 2:

, . , , , , - ?

, ; " "!

, ; ! . , , , .

, ; . , lint, pclint QA-C, .

+8

, . , , - , . :

  • , , . C 25 - .
  • . , , .
  • . , , .
  • . , , , . , ( ), , .
  • - . , .

, , , , , !

+4

C

  • GDB

:

  • Valgrind
+3

. .

+1
  • gdb - .
  • printf
  • Valgrind
  • . - .
+1

:

:

  • ,
  • .
  • ,

-

  • /
  • .
  • .
+1

(gdb, valgrind, ), .

  • . ( size_t) , , ptrdiff_t , off_t .. enum .
  • int, long, char . , .
  • , char , - . uint8_t int8_t .
  • , : integer, double, , struct. , . , . , . NULL , .
  • , . .
  • , , .
  • assert. , , .
+1

, .

0
0

valgrind , Linux. gdb/ddd linux. , , windbg. , , ​​ gdb; , . assert, , , . , ​​ Check, cunit .. NULL, . , NULL. segfault, . , . , , 10 , . gcc -Wall -Wextra. strn * str *. , .

0
source

Source: https://habr.com/ru/post/1754763/


All Articles