指针
内存类型资源地址 门牌号的代名词
指针变量
存放指针这个概念的盒子
C语言编译器对指针这个概念有两个疑问、
分配一个 盒子多大 在32位系统中 指针就是4个字节、
盒子里存放的地址 内存的读取方法是什么、
指针指向的内存空间 一定要保证合法性
const 越界
#include <stdio.h>
int main()
{
const int a=12345678;
int b =1231231;
int *p=&b;
*(p+1)=0x100;
printf("this is a %x\n",a);
return 0;
}
~
~
~
~
~
~
~
~
~
"007.c" 17L, 150C ???
root@yxcq-desktop:/mnt/hgfs/mnt/src#
root@yxcq-desktop:/mnt/hgfs/mnt/src# gcc -o build 007.c
root@yxcq-desktop:/mnt/hgfs/mnt/src# ./build
this is a 100
多级指针
1 #include <stdio.h>
2
3 int main(int argc,char **argv)
4 {
5
6 int i;
7 for(i=0;i<argc;i++){
8 printf("this is argv[%d] is %s\n",i,argv[i]);
9
10
11 }
12
13 return 0;
14
15 }
~
~
~
~
~
~
~
~
~
~
~
"008.c" 15L, 152C ???
root@yxcq-desktop:/mnt/hgfs/mnt/src#
root@yxcq-desktop:/mnt/hgfs/mnt/src# gcc -o build 008.c
root@yxcq-desktop:/mnt/hgfs/mnt/src# ./build
this is argv[0] is ./build
root@yxcq-desktop:/mnt/hgfs/mnt/src# ./build 123 456
this is argv[0] is ./build
this is argv[1] is 123
this is argv[2] is 456
方法二
#include <stdio.h>
int main(int argc,char **argv)
{
int i=0;
while(argv[i]!=NULL){
printf("this is %s\n",argv[i]);
i++;
}
return 0;
}
~
~
~
~
~
~
~
~
~
~
~
~
"008.c" 14L, 144C ???
root@yxcq-desktop:/mnt/hgfs/mnt/src#
root@yxcq-desktop:/mnt/hgfs/mnt/src# gcc -o build 008.c
root@yxcq-desktop:/mnt/hgfs/mnt/src# ./build 123 456
this is ./build
this is 123
this is 456
如果指针指向常量 是不可修改的
int a[100]
char *a[100]
sizeof(a)=100*4 一个指针4个字节
结构体
为了提高效率 可以舍弃点空间 提高效率
最终结构体的大小是4的倍数
结构体成员顺序的不一致 也会影响他的大小
内存分布图
内存的属性
大小
在哪里
编译 汇编 链接
4g .o build
kernel 内核空间 不允许应用程序访问
-----------------------------------------------------------------3g
栈空间 局部变量
--------------------------------------------
堆空间 malloc分配的空间
-----------------------------------
全局的数据空间(初始化 未初始化)
只读数据段 "hello world"
代码段 code
--------------------------------
ox0
堆空间
运行时 可以自由 分配内存空间和释放内存空间
生存周期由程序员来决定
char *p
p =(char *)malloc(100)'
if(p=NULL){
ERROR
}