vi HelloWorld.c
#include
int main(int argc,char* argv[]){
printf("HelloWorld!\n");
return 0;
}
clang -g -o helloworld HelloWorld.c
ls -alt helloworld
./helloworld
#include
int main(int argc,char* argv[]){
int a = 100;
float b = 1.23;
char c = 'C';
printf("Hello World!\n");
printf("a = %d\n",a);
printf("b = %f\n",b);
printf("c = %c\n",c);
return 0;
}
clang -g -o helloworld1 HelloWorld.c
./helloworld1
打印:
Hello World!
a = 100
b = 1.230000
c = C
指针:
#include
#include
int main(int argc,char* argv[])
{
int *a,*b;
a = (int*)malloc(sizeof(int)); //在栈中开辟内存
b = (int*)malloc(sizeof(int));
*a = 1;
*b = 2;
int c[3] = {0,1,2};
printf("addr of a:%p,%p,%d\n",&a,a,*a);
printf("addr of b:%p,%p,%d\n",&b,b,*b);
printf("addr of c:%p,%p,%d,%d,%d\n",&c,c,c[0],c[1],c[2]);
return 0;
}
gcc -g -o testpoint testpoint.c
./testpoint
打印结果:
addr of a:0x7ffee1cb1830,0x7fa99b402b60,1
addr of b:0x7ffee1cb1828,0x7fa99b402b70,2
addr of c:0x7ffee1cb184c,0x7ffee1cb184c,0,1,2
操作系统时如何管理内存的?
内存的分配与释放
void* mem = malloc(size);
free(mem);
内存泄漏与野指针
函数指针
int func(int x);//声明一个函数
int(*f)(int x);//声明一个函数指针
f = func;//将func函数的首地址赋给指针f
函数指针示例:
vi testfunc.c
#include
int sum(int a,int b)
{
return (a + b);
}
int sub(int a,int b)
{
return (a- b);
}
int main(int argc,char* argv[])
{
int(*f)(int,int);
f = sum;
int result = f(3,5);
f = sub;
int result2 =f(result,3);
printf("3+5 = %d\n",result);
printf("8-3 = %d\n",result2);
return 0;
}
clang -g -o testfunc testfunc.c
./testfunc
打印:
3+5 = 8
8-3 = 5
vi testst.c
#include
struct st{
int a;
int b;
};
int main(int argc,char* argv[])
{
struct st sst;
sst.a = 10;
sst.b = 20;
printf("struct content is :%d,%d\n",sst.a,sst.b);
return 0;
}
clang -g -o testst testst.c
./testst
打印结果:
struct content is :10,20
vi testenum.c
#include
enum e_type{
red = 0,
green,
blue
};
int main(int argc,char* argv[]){
enum e_type et;
et = red;
printf("the color is: %d\n",et);
et = blue;
printf("the color is: %d\n",et);
return 0;
}
clang -g -o testenum testenum.c
./testenum
打印结果:
the color is: 0
the color is: 2
FILE* file;
FILE* fopen(path,mode);
fclose(FILE*)
#include
int main(int argc,char* argv[])
{
FILE* file;
char buf[1024] = {0,};
file = fopen("1.txt","a+");//在末尾添加,游标指向末尾
fwrite("hello world!",1,13,file);
rewind(file);//游标指向最前
fread(buf,1,13,file);
fclose(file);
printf("buf : %s\n",buf);
return 0;
}
clang -g -o testfile testfile.c
./testfile
buf : hello world!
gcc/clang -g -O2 -o test test.c -I... -L... -l
编译过程
vi add.h
int add(int a,int b);
vi add.c
int add(int a,int b){
return (a + b);
}
vi add.c
#include
#include "add.h"
int main(int argc,char* argv[])
{
printf("add = %d\n",add(2,3));
return 0;
}
clang -g -c add.c
libtool -static -o libmylib.a add.o
clang -g -o testlib testlib.c -I. -L. -lmylib
./testlib
打印结果:
add = 5
或者:
clang -g -c testlib.c
clang -o testlib1 testlib.o -L . -lmylib
./testlib1
打印结果:
add = 5
调试器原理
gdb和lldb
命令 | gdb | lldb |
---|---|---|
设置断点 | b | b |
运行程序 | r | r |
单步执行 | n | n |
跳入函数 | s | s |
跳出函数 | finish | finish |
打印内容 | p | p |
lldb testlib
b main
break list
r
s
p a
p b
finish
n
c #continue执行完之后的
quit # 退出
cd testlib.dSYM/Contents/Resources/DWARF
dwarfdump testlib