当前位置:网站首页>榕树贷款C语言结构体里的成员数组和指针
榕树贷款C语言结构体里的成员数组和指针
2022-07-27 01:51:00 【dongsijia】
榕树贷款执行下面的Test函数,分析会输出上面结果以及代码中存在什么问题。
1、笔试题1
void GetMemory(char* p)
{
p = (char*)malloc(100);
}
void Test(void)
{
char* str = NULL;
GetMemory(str);
strcpy(str, "hello world");
printf(str); //将str的首地址传给printf函数,可行
榕树贷款分析:
上面这段程序有三个问题:
第一:榕树贷款在Test函数中调用GetMemory函数时,传递的是str的值,所以GetMemory函数的参数p只是str的一份临时拷贝,与str没有任何关系,将动态开辟的100个字节交由指针p管理并不会对str产生任何影响;而且榕树贷款就算传递的是str的地址,malloc函数申请空间也有可能失败;
第二:榕树贷款由于GetMemory函数并没有能让str获得空间,所以str仍为NULL,这时调用strcpy函数会导致程序错误;
第三:榕树贷款代码中并没有对动态开辟的100个字节空间进行free,会导致内存泄漏;
边栏推荐
- Deeply understand the underlying data structure and algorithm of MySQL index
- MySQL中文失败问题
- Detailed explanation of const usage in C language
- 带你了解什么是 Web3.0
- Penetration test - post penetration - Trace cleaning
- [从零开始学习FPGA编程-54]:高阶篇 - 基于IP核的FPGA开发-PLL锁相环IP核的原理与配置(Altera)
- spark:地区广告点击量排行统计(小案例)
- 深入理解Mysql索引底层数据结构与算法
- docker 创建mysql 8.x容器,支持mac ,arm架构芯片
- 如何进行 360 评估
猜你喜欢

mysql底层数据结构

Common events of window objects

Activiti5.22.0扩展支持达国产数据库,以GBase据库为例

【学习笔记之菜Dog学C】字符串+内存函数

mysql如何优化

Spark: ranking statistics of regional advertising hits (small case)

spark学习笔记(五)——sparkcore核心编程-RDD转换算子

Plato farm has a new way of playing, and the arbitrage eplato has secured super high returns

Boom 3D new 2022 audio enhancement app

Explain tool actual operation
随机推荐
Best practices of opentelemetry in service grid architecture
【常用搜索问题】111
[flask] the server obtains the request header information of the client
“date: write error: No space left on device”解决
How to uniquely identify a user SQL in Youxuan database cluster
一种分布式深度学习编程新范式:Global Tensor
Acwing 2074. Countdown simulation
Win10/win11 lossless expansion of C disk space, cross disk consolidation of C and e disks
Safe-arc/warner power supply maintenance xenon lamp power supply maintenance analysis
Penetration test - post penetration - Trace cleaning
How to design the red table of database to optimize the performance
Indexing best practices
技术风向标 | 云原生技术架构成熟度模型解读
延时队列的几种实现姿势?日常必备技能!
“满五唯一”和“满二唯一”是什么?有什么不同?
Data Lake (20): Flink is compatible with iceberg, which is currently insufficient, and iceberg is compared with Hudi
Add support for @data add-on in idea
30分钟彻底弄懂 synchronized 锁升级过程
队列达到最大长度代码实战
MySQL:互联网公司常用分库分表方案汇总