嵌入式C语言高级_内存空间 笔记

指针

内存类型资源地址 门牌号的代名词

指针变量

存放指针这个概念的盒子

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

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值