[C-prog-lang-l] pointers and debuggers

Vladimír Kotal vlada at kotalovi.cz
Mon Mar 20 22:33:12 CET 2023


Hi all,

as I mentioned during today's lecture, learning how to use a command line debugger (of your choice) is often advantageous and sometimes necessary.

Here's a sample session in lldb, using the code https://github.com/devnull-cz/c-prog-lang/blob/master/src/ptr-basics.c to print various pointer related data. To get source line level debugging, compile the program with -g.

Best regads,


V. Kotal

`(lldb) list main
File: /home/vkotal/MFF/C/c-prog-lang.git/src/ptr-basics.c
   1   	#include <stdio.h>
   2   	
   3   	int
   4   	main(void)
   5   	{
   6   		int i = 5;
   7   		int *p = &i;
   8   	
   9   		printf("pointer lives at %p\n", &p);
   10  		printf("pointer points to %p\n", p);
   11  		printf("value of the pointed address: %d\n", *p);
(lldb) b 6
Breakpoint 2: where = a.out`main + 8 at ptr-basics.c:6:6, address = 0x0000000000001148
(lldb) r
Process 130668 launched: '/home/vkotal/MFF/C/c-prog-lang.git/input/2023/a.out' (x86_64)
Process 130668 stopped
* thread #1, name = 'a.out', stop reason = breakpoint 2.1
    frame #0: 0x0000555555555148 a.out`main at ptr-basics.c:6:6
   3   	int
   4   	main(void)
   5   	{
-> 6   		int i = 5;
   7   		int *p = &i;
   8   	
   9   		printf("pointer lives at %p\n", &p);
(lldb) s
Process 130668 stopped
* thread #1, name = 'a.out', stop reason = step in
    frame #0: 0x000055555555514f a.out`main at ptr-basics.c:7:7
   4   	main(void)
   5   	{
   6   		int i = 5;
-> 7   		int *p = &i;
   8   	
   9   		printf("pointer lives at %p\n", &p);
   10  		printf("pointer points to %p\n", p);
(lldb) x &i
0x7fffffffe3dc: 05 00 00 00 01 00 00 00 00 00 00 00 90 6d da f7  .............m..
0x7fffffffe3ec: ff 7f 00 00 00 00 00 00 00 00 00 00 40 51 55 55  ............ at QUU
(lldb) s
Process 130668 stopped
* thread #1, name = 'a.out', stop reason = step in
    frame #0: 0x0000555555555157 a.out`main at ptr-basics.c:9:2
   6   		int i = 5;
   7   		int *p = &i;
   8   	
-> 9   		printf("pointer lives at %p\n", &p);
   10  		printf("pointer points to %p\n", p);
   11  		printf("value of the pointed address: %d\n", *p);
   12  	}
(lldb) x p
0x7fffffffe3dc: 05 00 00 00 01 00 00 00 00 00 00 00 90 6d da f7  .............m..
0x7fffffffe3ec: ff 7f 00 00 00 00 00 00 00 00 00 00 40 51 55 55  ............ at QUU
(lldb) x &p
0x7fffffffe3d0: dc e3 ff ff ff 7f 00 00 50 50 55 55 05 00 00 00  ........PPUU....
0x7fffffffe3e0: 01 00 00 00 00 00 00 00 90 6d da f7 ff 7f 00 00  .........m......
(lldb) print p
(int *) $6 = 0x00007fffffffe3dc
(lldb) print &p
(int **) $7 = 0x00007fffffffe3d0
(lldb) n
pointer lives at 0x7fffffffe3d0
...`
-------------- next part --------------
HTML attachment scrubbed and removed


More information about the c-prog-lang-l mailing list