当前位置:网站首页>C traps and defects Chapter 3 semantic "traps" 3.2 pointers to non arrays
C traps and defects Chapter 3 semantic "traps" 3.2 pointers to non arrays
2022-07-29 03:03:00 【weixin_ Guest time】
Pointer to a non array
stay C In language , A string constant represents a block that includes all characters in the string and an empty character ('\0') Of memory
The address of the area . because C Language requires string constants to end with empty characters , For other strings ,C Programmers usually
This practice is also followed .
character string s and t, Connect two strings into a single string r. Do that , We can use common library functions strcpy
and strcat. The following method seems to be clear at a glance , But it can't meet our goal :
char *r;
strcpy(r, s);
strcat(r, t);
Why not , Because I'm not sure r Where . We should also see , Not just let r Point to an address , and r The address pointed to should also have memory space to hold the string , This memory space should have been allocated in some way .
to r Allocate a certain amount of memory space :
char r[100];
strcpy(r, s);
strcat(r, t);
as long as s and t The string pointed to is not too large , Then the method we use now can work normally . Unfortunately ,C The language forces us to declare the array size as a constant , So we dare not ensure r Large enough . However , majority C Language implementation provides us with a library function malloc, This function accepts an integer , Then allocate a block of memory that can hold the same number of characters . majority C The language implementation also provides library functions strlen, This function returns the number of characters included in a string .
char *r, *malloc();
r = malloc(strlen(s) + strlen(t));
strcpy(r, s);
strcat(r, t);
This example is wrong , There are three reasons .
The first reason ,malloc Function may not be able to provide the requested memory , In this case malloc The function returns a null pointer as “ memory allocation failed ” Signal of time .
The second reason , to r The allocated memory should be released in time after use , This must be kept in mind . Because in the previous program example r Is declared as a local variable , So when you leave r When scoped ,r Automatically released . The modified program is displayed to r Allocated memory . The revised procedure is shown to r Allocated memory , To do this, you must explicitly free memory .
The third reason , And the most important reason , It is the previous routine that calls malloc Function did not allocate enough memory . Because of the convention that strings use empty characters as terminators . Library function strlen Returns the number of characters contained in the string in the parameter , The empty character as the end sign is not counted .
therefore , If strlen(s) The value of is n, Then the string actually needs n+1 Space of characters . therefore , We have to r Allocate one more character space .
char *r, *malloc();
r = malloc(strlen(s) + strlen(t) + 1);
if (!r) {
complain();
exit();
}
strcpy(r, s);
strcat(r, t);
/* Use it after a period of time */
free(r);
边栏推荐
- C陷阱和缺陷 第3章 语义“陷阱” 3.2 非数组的指针
- Trample --- discretization + tree array + difference
- Inventory of domestic and foreign project collaborative management software: SAAS and customization become a trend
- 多线程实现多用例文件并发读取执行+Selenium Grid4实现测试框架分布式部署
- 万字详解 Google Play 上架应用标准包格式 AAB
- [QNX hypervisor 2.2 user manual]9.11 RAM (under update)
- 4000 多字学懂弄通 js 中 this 指向问题,顺便手写实现 call、apply 和 bind
- 会议OA项目之我的审批功能
- VIM common commands
- Double for loop
猜你喜欢

2022-07-28 顾宇佳 学习笔记

Verilog:阻塞赋值和非阻塞赋值

【打开新世界大门】看测试老鸟如何把API 测试玩弄在鼓掌之间

Jinshan cloud returns to Hong Kong for listing: Hong Kong stock rush of Chinese to B cloud manufacturers

Redis configuration cache expiration listening event trigger

C语言基础知识点汇总

【FreeSwitch开发实践】UniMRCP编译与安装

Multi table (Association) query of SQL query data

瀚高数据库最佳实践配置工具HG_BP日志采集内容

微信为之疯狂的Glide使用——之生命周期学习
随机推荐
sqlilabs less-32~less-33
Mongodb index (3)
SQL查询数据之多表(关联)查询
C陷阱与缺陷 第3章 语义“陷阱” 3.7 求值顺序
Chapter 09_ Use of performance analysis tools
Typescript学习(一)
SOA(面向服务架构)是什么?
Jinshan cloud returns to Hong Kong for listing: Hong Kong stock rush of Chinese to B cloud manufacturers
Thirty years of MPEG audio coding
Seed random seed
Tp5.0 applet users do not need to log in and directly obtain the user's mobile number.
解析Steam教育中的项目式学习创造力
Codeworks 5 questions per day (average 1500) - day 25
服务器运行管理制度
融云实时社区解决方案
Mysql复合查询(重要)
从零开始实现lmax-Disruptor队列(六)Disruptor 解决伪共享、消费者优雅停止实现原理解析
C陷阱与缺陷 第3章 语义“陷阱” 3.1 指针与数组
明明开发薪资高,为什么我还是选了测试?
原理知识用得上