动态内存函数笔试题

news/2025/2/15 21:33:04/

题目四:

修改后

#include<string.h>
void test()
{char* str = (char*)malloc(100);strcpy(str, "hello");if (str != NULL){strcpy(str, "world");printf(str);}free(str);str = NULL;
}
int main()
{test();return 0;
}

源代码

#include<string.h>
void test()
{char* str = (char*)malloc(100);strcpy(str, "hello");free(str);if (str != NULL){strcpy(str, "world");printf(str);}
}
int main()
{test();return 0;
}

问题:

1.顺序颠倒——先释放后判断

2.释放时并没有将str置为空指针

3.判断后没有进行释放,导致内存泄漏

题目三:

源代码

#include<string.h>
char* getmemory(char**p,int num)
{*p = (char*)malloc(num);
}
void test()
{char* str = NULL;getmemory(&str, 100);strcpy(str, "hello");printf(str);
}
int main()
{test();return 0;
}

问题:

对*p无判断无释放

题目二:

修改:

改为:char(*p)[] = "hello world"

源代码

char* getmemory()
{char p[] = "hello world";return p;
}
void test()
{char* str = NULL;str = getmemory();printf(str);
}
int main()
{test();return 0;
}

问题:

1.输出和“hello world”相同字符数的乱码

2.str是字符指针,p是字符数组

3.p出了作用域就销毁(栈区)

题目一:

更改如下

#include<string.h>
void getmemory(char** p)
{*p = (char*)malloc(100);if (*p == NULL){perror("getmemory");}
}
void test(void)
{char* str = NULL;getmemory(&str);strcpy(str, "hello world");printf(str);free(str);str = NULL;
}
int main()
{test();return 0;
}

源代码

#include<string.h>
void getmemory(char* p)
{p = (char*)malloc(100);
}
void test(void)
{char* str = NULL;getmemory(str);strcpy(str, "hello world");printf(str);
}
int main()
{test();return 0;
}

问题:

1.结果报错无输出

2.test应传str的地址

3.getmemory应该用二级指针接收一级指针,改为char**p

4.getmemory中没有对malloc返回值进行判断,也没有释放p,子函数执行完p的内容会销毁


http://www.ppmy.cn/news/1137889.html

相关文章

代码随想录 Day - 59|#647 回文字串|#516 最长回文子序列

清单 ● 647. 回文字串 ● 516. 最长回文子序列 LeetCode #647 回文字串 1. 题目 给你一个字符串 s &#xff0c;请你统计并返回这个字符串中回文子串的数目。 回文字符串是正着读和倒过来读一样的字符串。 子字符串是字符串中的由连续字符组成的一个序列。 具有不同开始位…

typora + picgo + 对象存储 OSS

文章目录 一、安装软件二、使用阿里云 oss 存储图片三、picgo 设置四、typora 设置自动上传 一、安装软件 Typora1.3.8 &#xff08;安装即破解&#xff09; picgo 2.3.0 安装 阿里云盘&#xff08;软件安装包&#xff09;&#xff1a; https://www.aliyundrive.com/s/saQoS…

Python与正则表达式

我们在做机器学习项目的时候&#xff0c;很大部分的精力都在做数据的整理&#xff0c;不管是用爬虫在网上爬取数据还是对已有的数据进行整理&#xff0c;往往需要对一些特定的字符串进行处理&#xff0c;正则表达式则是进行数据处理的利器。 一、什么是正则表达式 正则表达式…

GPT系列论文解读:GPT-2

GPT系列 GPT&#xff08;Generative Pre-trained Transformer&#xff09;是一系列基于Transformer架构的预训练语言模型&#xff0c;由OpenAI开发。以下是GPT系列的主要模型&#xff1a; GPT&#xff1a;GPT-1是于2018年发布的第一个版本&#xff0c;它使用了12个Transformer…

Java常见API---split()

package daysreplace;public class SplitTest {public static void main(String[] args) {String str"武汉市|孝感市|长沙市|北京市|上海市";String[] array str.split("\\|");System.out.println(array[0]);System.out.println(array[1]);System.out.pri…

【数据结构】排序算法(二)—>冒泡排序、快速排序、归并排序、计数排序

&#x1f440;樊梓慕&#xff1a;个人主页 &#x1f3a5;个人专栏&#xff1a;《C语言》《数据结构》《蓝桥杯试题》《LeetCode刷题笔记》《实训项目》 &#x1f31d;每一个不曾起舞的日子&#xff0c;都是对生命的辜负 目录 前言 1.冒泡排序 2.快速排序 2.1Hoare版 2.2占…

C++笔记之信号量、互斥量与PV操作

C笔记之信号量、互斥量与PV操作 文章目录 C笔记之信号量、互斥量与PV操作1.信号量概念2.信号量例程一3.信号量例程二4.信号量例程三5.互斥量6.PV操作概念7.PV操作详解——抄自&#xff1a;https://mp.weixin.qq.com/s/vvjhbzsWQNRkU7-b_dURlQ8.PV操作的英文全称 1.信号量概念 …

python 打包可执行文件-pyinstaller详解

python 打包可执行文件-pyinstaller详解 引言一、参数详解二、优化代码三、体积压缩 引言 pyinstaller是一个将python程序打包成独立可执行文件&#xff08;exe&#xff0c;app等&#xff09;的工具&#xff0c;它具有跨平台兼容性&#xff0c;可以在windows&#xff0c;mac和…