2020年信奥赛C++提高组csp-s初赛真题及答案解析(选择题6-10)

核心内容摘要

PCL2:轻量级可定制启动器的技术实践与场景价值
GDPR 3.0:个人数据“被遗忘权”代码实现——软件测试从业者指南

系统思考与组织效率

对于STM32F103C8T6集成版进行小升级加入了光敏电阻热敏电阻8个小灯两个按键macial口改成了type-c然后改成了自动下载然后为了省钱又全部改成直插式的了#include stdio.h #include stdlib.h #include string.h //定义结构体数据类型 typedef struct Student { int id; char gender; char name[20]; char *adress;//未分配内存空间之前是野指针 }student; void deepcopy(student *to_stu, student *from_stu) { to_stu-id from_stu-id; to_stu-gender from_stu-gender; //错的 //to_stu-name from_stu-name; strcpy(to_stu-name,from_stu-name); to_stu-adress (char *)malloc(

; memset(to_stu-adress, 0, sizeof(to_stu-adress)); memcpy(to_stu-adress, from_stu-adress, strlen(from_stu-adress)); } int main() { //定义结构体数据变量 // struct Student stu1 {.id 1, .gender M, .name Lisi}; // student stu2 {2, F, xiaohong}; // student *stu stu2; // printf(stu

id %d, stu

gender %c, stu

name %s\n, (stu

-id, stu

gender, stu

name); // printf(stu

id %d, stu

gender %c, stu

name %s\n, (*stu).id, stu-gender, stu-name); struct Student stu1 {1, M}; // printf(please input your message:\n); // scanf(%d %c,stu

id, stu

gender); printf(please input your address:\n); stu

adress (char *)malloc(sizeof(char) *

; scanf(%s,stu

adress); //结构体成员是数组的不能用“”来赋值 //stu

name lisi; strcpy(stu

name, lisi); printf(stu

id %d, stu

gender %c, stu

name %s\n, (stu

-id, stu

gender, stu

name); printf(address:%s\n,stu

adress); //结构体变量之间的赋值,浅拷贝问题 student stu2; // stu1; deepcopy(stu2, stu

; free(stu

adress); stu

adress NULL; printf(stu

id %d, stu

gender %c, stu

name %s\n, stu

id, stu

gender, stu

name); printf(address:%s\n,stu

adress); free(stu

adress); stu

adress NULL; return 0; }#include stdio.h struct Student { int id; char gender; char name[20]; }; //结构体字节对齐是什么 //结构体的内存对齐 //要在最大的数据类型上根据你代码中的注释我来详细讲解结构体对齐规则 // 结构体对齐规则 // 核心规则 // 对齐值结构体中最大成员的类型大小如int为4double为8 // 成员偏移每个成员的偏移量必须是其类型大小的整数倍 // 结构体总大小必须是最大成员大小的整数倍 // 以你的struct Student为例 // struct Student { // int id; // 4字节偏移0 ✓ // char gender; // 1字节偏移4 ✓ // char name[20]; // 20字节偏移5 (5%41不满足) // }; // 问题name偏移量为5但应该是4的倍数因为最大成员是int4字节 // 内存布局 // 偏移

: id (4字节) // 偏移 4: gender (1字节) // 偏移 5: 填充3字节 (对齐到偏移

// 偏移

: name (20字节) // 总大小28字节 // 实际输出28 // 优化版本按类型大小从大到小排列 // struct Student { // int id; // 偏移04字节 // char name[20]; // 偏移420字节 // char gender; // 偏移241字节 // char padding[3]; // 填充3字节总大小28字节 // }; // 注意这个例子中两种写法大小相同但通常按大小排列能减少浪费。

int main() { printf(%d\n,sizeof(struct Student)); return 0; }、#include stdio.h #include stdlib.h #include string.h #include time.h int main() { //共用体union union Data { int i; float f; char str[20]; } data; //共用体的特点所有成员共享同一块内存空间只能同时存储一个成员的数据 return 0; }#include stdio.h int main() { //关键字volatile的作用 //

防止编译器优化 //

防止编译器将变量保存到寄存器中 volatile int i 0 ; int *p i; //取地址为什么会报错 //int *p i; //不能去取i的地址因为i是volatile的编译器会认为i的值随时可能发生变化所以编译器会禁止对i的取地址操作 while(

{ printf(i %d\n,i); } return 0; }#include stdio.h void func(void) { static int i 0; i; printf(i %d\n,i); } int main() { //关键字static的作用 //

修饰局部变量静态局部变量生命周期长作用域局部 //

修饰全局变量静态全局变量生命周期长作用域全局 //

修饰函数静态函数只能在定义它的文件中使用 //

修饰类静态类只能在定义它的文件中使用 func(); func(); func(); return 0; }

桃花tv-桃花应用

百度百家号客服电话人工服务

123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123