Debugging C programs with lldb under Mac

Not long ago, I learned the basics of Linux C language on Muke.com. The teacher used the vim editor and the debugging tool used was gdb, but I found that I could not find it on my Mac. Now I am using the upgraded version of lldb of gdb

write picture description here

Don't talk nonsense, go straight to the dry goods! code show as below:

#include<stdio.h>
int main(){
    int a=4;
    int b=6;
    int array[3];
    array[0]=1;
    array[1]=10;
    array[2]=100;
    int *p;
    p=&a;
    int i=0;
    while(i<6){
        printf("*p=%d\n",*p);
        p++;
        i++;
    }
    return 0;
}

Since the code has been written before, that is, the main.c file, directly enter lldb ./main for debugging
write picture description here

1. Set a breakpoint and start debugging
write picture description here
2. Perform single-step debugging, enter next, or abbreviated n, I keep pressing n here until I enter the while loop
write picture description here
3. Check the value in the memory and use the print variable (or abbreviated as p) For example: pa
write picture description here
has: p = &a in the code; so the value of the pointer p is: 0x00007fff5fbffb74, which is the same as the address of the variable a;
in fact, the essence of the variable is memory, and the essence of the pointer is the address. This command: p &p, in fact, is to display the address of the pointer p, that is, the memory address where the value of the pointer p is stored, and &p is the secondary pointer.
4. Display the value of the memory in batches, use the x command:
write picture description here
here is the address of the variable i, because it has been explored before, it is written directly, and it has been single-step debugging and entered the second loop.
At this time , the value of i Change from 0 to 1
write picture description here
C language is divided into code segment, data segment, heap, stack in memory, all local variables in the code will be stored in the stack, that is, at the high address of the memory , we display the address starting from 0x00007fff5fbffb64 A series of address values, you will find that these values ​​are basically stored together:
write picture description here
where 9 means to display 9 address values, d means to display the value in decimal, w means 4 bytes as a unit, the following shows all variables. Values, including a, b, i, array[0], array[1], array[2], of course, the six values ​​output by the loop in the program do not completely include the above six variables, we can see that they The locations in memory are not contiguous in the order of declaration, there are reasons why the compiler has dealt with them

0x7fff5fbffb64: 0                     (i)

0x7fff5fbffb68: 1606417268

0x7fff5fbffb6c: 32767

0x7fff5fbffb70: 6                      (b)

0x7fff5fbffb74: 4 (a)

0x7fff5fbffb78: 0 0x7fff5fbffb7c: 1 (array[0]) 0x7fff5fbffb80: 10 (array[1]) 0x7fff5fbffb84: 100 (array[2])

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325721308&siteId=291194637