C/C++|物联网开发入门+项目实战|指针|嵌入式C语言高级|C语言内存空间的使用-数组-学习笔记(10)


参考: 麦子学院-嵌入式C语言高级-内存空间

2-3 : C语言内存空间的使用-数组

内存分配的一种形式
在这里插入图片描述

数组的定义及初始化

定义一个空间:
1、大小
2、读取方式
数组名[]:升级为连续空间的名称, [m]的作用域只在申请的时候起作用
每个多大?数组名前定义数据类型,按int 还是char进行切割?
数组名是地址常量符号,地址常量标签;一定不要放在等号左边
char buff[100];
buf = “hello world!” //该语法错误,不能赋值给常量。
int a[100]; //空间申请的一种方式
数据会有越界问题
a[10] a是门牌号,是常量,a的属性已定。而指针是内存,可变。

数组空间的初始化

空间的赋值
按照标签逐一处理
int a[10]: [0-9]
a[0]= xx;
a[1]= yy;
程序员这样赋值,工作缗比较大,能不能让编译器进行一些自动处理,帮助程序员写如上的程序

空间的第一次赋值

->空间定义时,就告知编译器的初始化情况,空间的第一次赋值,初始化操作
int a[10] = 空间; //调用标准库或者逐一拷贝,和普通变量初始化有本质不同
c语言本身,CPu内部本身一般不支持大空间和大空间的拷贝
int a[10] = {10,20,30} //3个B,3个字节。
          =====>拷贝的过程,CPU做了,a[0] = 10; a[1] = 20;  a[2] = 0

和逐一赋值是一样的,{}代表空间的限制块

数组空间的初始化和变量的初始化本质不同,尤其在嵌入式的裸机开发中,空间的初始化往往需要库函数的铺助。

char

char buf[10] = {‘a’,‘b’,‘c’} //a是8b,占1个B,共占3个字节
buf当成普通内存来看,没有问题
buf当成一个字符串来看,最后加上一个’\0’或者0
字符串的重要属性是,结尾一定有一个’\0’(内存中0的表示形式)

char buf[10] = {“abc”}; //编译器看到"“自动加0
char buf[10] = “abc”; //执行后,系统中有2个abc,一个是"abc”(常量),一个是buf的变量-字符串"abc",=号完成常量向变量去拷贝。buf可变,可操作。不能改变abc
本质区别于:char p = “abc”; //指向"abc"的指针,buf可变,但p不变
buf[2] = ‘e’ //可行, p[2] = ‘e’ //不行,出现段错误,因为p指向常量,不能修改
示例:

#include <stdio.h>

int main()
{
    printf("the x  is %x\n","abc");  //显示的是内存地址
    return 0;

}
// E:\temp>cd "e:\temp\" && gcc 2.c -o 2 && "e:\temp\"2
// the x  is 409020

char buf[]=“abc”; //4个字节,因为还有0
char buf[10] = “abc”;
buf = “hello world!” //buf是常量标签,不能修改,故编译错误,标签不能变
第二次内存初始化,赋值?
逐一处理 buf[0] = ‘h’ ,…buf[n+1] = 0;

strcpy(工程禁用), strncpy(推荐)

一块空间,当成字符空间,提供了一套字符拷贝函数
在这里插入图片描述
字符拷贝函数的原则:
内存空间和内存空间的逐―赋值的功能的一个封装体,一旦空间中出现了0这个特殊体,则函数拷贝结束。
strcpy()
标准函数代码中会嵌入一些汇编语言,开发过程中效率进一步提高。
嵌入式开发工程师:根据CPU的体系结构不同以及CPU的能力,这段程序是可以改变的,能够更加优化。
char buf[10] = “abc”;
strcpy(buf,“hello world”); //完成复制,并自动加入"\0"结束标志
当"hello world*********"等待复制的字符串会一直copy,典型的内存泄漏,工程不能使用,只能用strncpy,控制复制长度,限制拷贝数量。

非字符串空间

字符空间
ASCII码编码来解码的空间,–》给人看
%s abc ‘a’ ‘b’ ‘c’
'\0’作为结束标志
非字符空间
数据采集 采集0x00-0xff 8bit
开辟一个存储这些数据的盒子,
定义时用
char buf[10]; ----》string
有歧义,怎么能保证完全是数据?
采集过来的只有位的概念,和符号无关,
必须定义为unsigned char buf[10]; ----》一定是最小data单位

unsigned char *p = sensor_ base;
strcpy(buf,p);//此方法这里不适用,看到\0才停止,存在非常不合理的风险,
所有,非字符串空间只管逐一拷贝,结束在哪里?只能是定义个数。
拷贝三要素:
 1、src
 2、dest
 3、个数(sizeof)

memcpy

int buf[10];
int sensor_buf[100];

memcpy(buf,sensor_buf,10sizeof(int)); //非单字节空间是,需sizeof
在这里插入图片描述

unsigned char buf1[10];
unsigned char sensor_buf[100];// 00 00 00 23 45 78
// strncpy(buf,sensor_buf,10) //拷贝不成功,因为数据00开头
memcpy(buf,sensor_buf,10*sizeof(unsigned char)); //10个字节(数据空间),对应10个内存单位,这里正好一一对应,书写规范要求带sizeof
先找对象(有无符号),再确定操作。

指针与数组

指针数组

建立与二维指针的关系,哪种好理解,记哪个
*a[100] //*代表a的属性
char *a[100];
sizeof(a) = 100 * 4; //一个地址4个字节
char **a;

数组名的保存

定义一个指针,指向int a[10]的首地址
定义一个指针,指向int b[5][6]的首地址
int *p1 = a; //这种写法是合理的,p指针定义读内存的方法是int,4个字节一组,读取,与原数据定义一致,2个内存读取的方式一样,就不会报警告或者错误。
int **p2 = b; //错误方法

示例代码

#include <stdio.h>

int main()
{
    int a[10];
    int b[5][6];

    int *p1 = a;

    int **p2 = b; //提示警告,因为二维数组与二维指针没有任何关系

}
// E:\temp>cd "e:\temp\" && gcc 2.c -o 2 && "e:\temp\"2
// 2.c: In function 'main':
// 2.c:10:16: warning: initialization from incompatible pointer type [enabled by default]

二维数组不是按基本的数据类型读内容,因为每行中的列不同,上例中,每移动一行的指针,内存跨度为4*6=24B,是一块一块的内存。
正确写法为:int (*p)[6]; //*p是整体,[6]每行二维数组的列数。
故示例中应改为:
int (*p2)[6] = b; //在内存中指针移动或指代的方式根据之前的int和之后的[]修饰
int b[2][3][4]
int (*p)[3][4];

2-4 : C语言内存空间的使用-结构体

字节对齐

出于CPU效率的考虑,牺牲一点空间换取时间的效率。
这样的解决方案又称为字节对齐,32bit对齐。

示例

#include <stdio.h>

struct  abc
{
    char a;
    int  b;
};

int main()
{
    struct  abc buf;

    printf("the buf is %lu\n",sizeof(buf)); //8个字节

};
// E:\temp>cd "e:\temp\" && gcc 2.c -o 2 && "e:\temp\"2
// the buf is 8

打包顺序的影响

最终结构体的大小一定是4的倍数
结构体成员变量顺序不一致,也会影响他们的大小
可以由低2位对齐,满足效率要求。

示例

#include <stdio.h>

struct  abc
{
    char a;
    short e;
    int  b;
};

struct  my
{
    char a;
    int  b;
    short e;

};

int main()
{
    struct  abc buf1;
    struct  my  buf2;
    printf("the buf1 is %lu\n",sizeof(buf1)); //8个字节
    printf("the buf2 is %lu\n",sizeof(buf2)); //8个字节

};
// E:\temp>cd "e:\temp\" && gcc 2.c -o 2 && "e:\temp\"2
// the buf1 is 8
// the buf2 is 12

内存分布图

内存的属性

1、大小
2、在哪里
int a; //默认方式进行最终的定位,放置的区域叫section(分段)的概念

编译—》汇编―–》链接
*.o build

#include <stdio.h>

int a;   //主函数体外,可看成是全局变量,存放位置离mian的空间较近
int main()
{
   //int a;  //临时的,当函数完成后,会销毁,存放位置也相对较高

   a = 0x10;

   printf("the a is %p\n",&a);

   printf("the main is %p\n",main);

   return 0;
};
// 局部变量:
// E:\temp>cd "e:\temp\" && gcc 2.c -o 2 && "e:\temp\"2
// the a is    000000000064FE2C
// the main is 0000000000401500
// 全局变量:
// E:\temp>cd "e:\temp\" && gcc 2.c -o 2 && "e:\temp\"2
// the a is    000000000040D3C8
// the main is 0000000000401500
内核空间    应用程序不许访问,看也不行

----------------3G
栈空间(局部变量)

运行时的堆空间(malloc申请的空间)

全局数据空间    (初始化的空间,未初始化) data bss
只读数据段  "hello world"                TEXT段
代码段      code(只读)                 TEXT段

0x0;

#include <stdio.h>

int b = 100;   //主函数体外,可看成是全局变量,存放位置离mian的空间较近
int fun()
{
    static int a = 100;

    return a++;
}
int main()
{
    static int a;
    unsigned char *p;
    a = 0x10;

    printf("the a is %p\n",&a);

    printf("the main is %p\n",main);

    p = (unsigned char *)main;

    printf("the p[0] is %x\n",p[0]);
    p[0] = 0x12; //操作这样属性的区域,会报错:segmentation fault
    printf("+++++ the p[0] is %x\n",p[0]);
    return 0;

};
// E:\temp>cd "e:\temp\" && gcc 2.c -o 2 && "e:\temp\"2
// the a is 000000000040C030
// the main is 0000000000401519
// the p[0] is 55
// 无显示

示例代码:

#include <stdio.h>

int b = 100;   //主函数体外,可看成是全局变量,存放位置离mian的空间较近
int fun()
{
    static int a = 100;

    return a++;
}
int main()
{
    static int a;
    unsigned char *p;
    char *p1 = "helao world";
    a = 0x10;

    printf("the p1 is %s\n",p1);
    printf("the string address is %p\n","helao world");
    p1[3] = 'l';
    printf("the p1 is %s\n",p1);

    // printf("the a is %p\n",&a);
    // printf("the main is %p\n",main);

    // p = (unsigned char *)main;

    // printf("the p[0] is %x\n",p[0]);
    // p[0] = 0x12; //操作这样属性的区域,会报错:segmentation fault
    // printf("+++++ the p[0] is %x\n",p[0]);
    return 0;

};
// E:\temp>cd "e:\temp\" && gcc 2.c -o 2 && "e:\temp\"2
// the p1 is helao world
// the string address is 0000000000409020
// 无显示
示例3
```C
#include <stdio.h>

// int b = 100;   //主函数体外,可看成是全局变量,存放位置离mian的空间较近
int fun()
{
    static int a = 100;

    return a++;
};
int main()
{
    static int a;
    unsigned char *p;
    //char *p1 = "helao world";
    const int b = 0x12345678; //b只读变量
    a = 0x10;
    char *p1 = &b;

    printf("the p1 is %x\n",p1);
    printf("the string address is %p\n","helao world");
    p1[3] = '0';
    printf("the p1 is %x\n",p1);
    return 0;
};

// E:\temp>cd "e:\temp\" && gcc 2.c -o 2 && "e:\temp\"2
// 2.c: In function 'main':
// 2.c:17:16: warning: initialization from incompatible pointer type [enabled by default]
// the p1 is 64fe24
// the string address is 000000000040902E
// the p1 is 64fe24

// E:\temp>size 2.exe
//    text    data     bss     dec     hex filename
//   30936    2864    5136   38936    9818 2.exe

示例4:同样的字符串增加字符前后堆空间分布

#include <stdio.h>

// int b = 100;   //主函数体外,可看成是全局变量,存放位置离mian的空间较近
int fun()
{
    static int a = 100;

    return a++;
};
int main()
{
    static int a;
    unsigned char *p;
    char *p1 = "helao world";
    const int b = 0x12345678; //b只读变量
    a = 0x10;

    printf("the p1 is %s\n",p1);
    printf("12345678the string address is %p\n","helao world");  //对独一无二的字符串加上1234
    p1[3] = 'l';
    printf("the p1 is %s\n",p1);
    return 0;
};
//    text(代码)    data(数据)     bss(未初始化信息)     dec     hex filename
// E:\temp>cd "e:\temp\" && gcc 2.c -o 2 && "e:\temp\"2
// the p1 is helao world
// the string address is 0000000000409020

// E:\temp>size 2.exe
//    text    data     bss     dec     hex filename
//   30952    2864    5136   38952    9828 2.exe

// E:\temp>cd "e:\temp\" && gcc 2.c -o 2 && "e:\temp\"2
// the p1 is helao world
// 1234the string address is 0000000000409020

// E:\temp>size 2.exe
//    text    data     bss     dec     hex filename
//   30952    2864    5136   38952    9828 2.exe

// E:\temp>cd "e:\temp\" && gcc 2.c -o 2 && "e:\temp\"2
// the p1 is helao world
// 12345678the string address is 0000000000409020

// E:\temp>size 2.exe
//    text    data     bss     dec     hex filename
//   30984    2864    5136   38984    9848 2.exe

在这里插入图片描述

size命令

# E:\temp>size 2.exe
#    text    data     bss     dec     hex filename
#   30984    2864    5136   38984    9848 2.exe

# E:\temp>strings 2.exe  //显示双引号内的字符
# ffffff.
# ATUWVSH
# T$ E
# t5H9
# [^_]A\
# fff.
# D$ tv
# D$(H
# T$ H
# L$ H
# L$(H
# fffff.
# ATUWVSH
#  [^_]A\
# ......

static int a; 放在bss空间中,而默认值是auto。
在这里插入图片描述
可以认为是:局部访问的全局数据段标识,仅在函数中起作用,但是存放位置还是在全局的数据空间。
查看内存分配命令可以用:nm 2.exe >>2.txt

堆空间

运行时的,函数内部使用的变量,生存周期为,函数一旦返回就释放,生存周期是函数内。
运行时可以自由分配、自我管理或释放的空间。生存周期由程序员来决定。

分配

malloc(),一旦成功,返回分配好的地址,我们只需要接受即可。对于这个新地址的读取方法由程序员指定。
输入参数指定分配的大小,单位是B
char *p;
p = (char *)malloc(100);
if (p == NULL){
error
}

int a[5]; malloc(5*sizeof(int))

存在泄漏风险的代码示例

void fun()
{
    char *p;
    p = (char *)malloc(100);
    return;
}

这个函数中的p当函数执行完毕时,还是指向malloc已申请的空间(未释放),存在泄漏风险。
解决方式:free。

释放(与分配配对执行)

free§;

猜你喜欢

转载自blog.csdn.net/Medlar_CN/article/details/130247777