C002-CPP-用malloc()存储任意长度的键入字符串
来源:互联网 发布:搬瓦工vps搭建ss优化 编辑:程序博客网 时间:2024/06/05 03:02
使用malloc()申请空间来存储任意长度的键入字符串
代码:
malloc_switch.c:
#include <iostream>using namespace std;#include <malloc.h>#include <stdlib.h>#include "malloc_switch.h"#define DEBUG 1// ==========================================================================================================// 使用malloc()申请空间来存储任意长度的键入字符串// // 返回值:char * p_input 返回保存着输入字符串的空间的地址p_input// // ==========================================================================================================char * store_input_stream(void){ char input; // 用于接收每次读取的缓冲区字符 char *p_input = NULL; // 它指向的空间用于保存输入 char *p_temp = NULL; // 它指向的空间用于交换 int number = 1; // 记录从缓冲区读取的键入字符数量 // 创建保存空间 p_input = (char *)malloc(sizeof(char) * number); if(NULL == p_input) { cout << "没有足够的堆空间用于保存输入" << endl; } // 开始接收缓冲区 cout << "请输入任意长度的字符(以ENTER结束): " << endl; cin.get(input); while((input != '\n') && (input != '\r')) { number++; // 申请更大的空间来保存增加的字符 p_temp = (char *)malloc(sizeof(char) * number); if(NULL == p_temp) { cout << "没有足够的堆空间用于保存输入" << endl; } // 将p_input复制到p_temp for(int i = 0; i < (number - 1); i++) { p_temp[i] = p_input[i]; } // 将最新的输入插入p_temp p_temp[number - 2] = input; // 释放p_input指向的内存 free(p_input); p_input = NULL; // 将p_temp交给p_input p_input = p_temp; p_temp = NULL; // 再次接收缓冲区 cin.get(input); } // 加入字符串结尾符 p_input[number - 1] = '\0';#if DEBUG cout << "之前输入的字符串是:" << p_input << endl;#endif#if 1 // 返回这个保存着输入字符串的空间的地址p_input,返回后就不可以销毁它,只能在外部调用这个函数之后销毁 return p_input;#else // 不返回p_input时、需要销毁p_input指向的内存 free(p_input); p_input = NULL;#endif}
main.c:
// ==========================================================================================================// 主函数// ==========================================================================================================#include <malloc.h>#include <stdlib.h>#include <iostream>using namespace std;#include "malloc_switch.h"// ==========================================================================================================// main函数// // ==========================================================================================================int main(void){ char *p_temp = NULL; char *p_input = NULL; // 拿到输入字符串 p_temp = store_input_stream(); cout << "这段字符的长度 = " << strlen(p_temp) << endl; // 创建一段内存来备份保存这些字符串 p_input = (char *)malloc(sizeof(char) * (strlen(p_temp) + 1)); // 上次忘了+1 if(NULL == p_input) { cout << "没有足够的堆空间用于保存输入" << endl; }// strcpy_s(p_input, strlen(p_temp) + 1, p_temp); // VS专有 strncpy(p_input, p_temp, strlen(p_temp) + 1); cout << "输入 = " << p_input << endl; // 备份完成后,释放store_input_stream()函数涉及到的空间 // store_input_stream()的影响到此结束 free(p_temp); p_temp = NULL; // 程序结束,释放p_input申请的空间 free(p_input); p_input = NULL;// ----------------- system("pause"); return 0;}
测试结果:
1、实现了存储任意长度键入字符串的功能
2、没有内存泄漏用工具检查
0 0
- C002-CPP-用malloc()存储任意长度的键入字符串
- C002-CPP-函数集合
- C002-CPP-Luhn公式
- 任意长度的字符串输入
- 任意长度的字符串输入
- 任意长度的字符串输入
- 任意长度的小数字符串的四舍五入
- PHP 生成任意长度的随机字符串
- 生成任意长度的随机字符串
- 任意长度的字符串反向输出
- 随机生成一个任意长度的字符串
- php生成指定长度的任意字符串
- 得到任意长度的随机字符串
- 任意长度的两个字符串数列相乘的算法实现
- 不使用string而获取任意长度的字符串
- 批处理如何计算任意字符串的字符长度?
- 获取一个字符串任意长度的子序列
- 获取一个字符串任意长度的子序
- 快速排序算法
- NEUOJ 720 头哥的烦恼
- 主元素问题
- 原码、反码、补码知识详细讲解(此作者是我找到的讲的最细最明白的一个)
- java设计模式(十一)--状态模式
- C002-CPP-用malloc()存储任意长度的键入字符串
- Microsoft资源
- 阿里云ECS云主机的选择
- SVN-之Clean up失败
- 【leetCode】Binary Tree Zigzag Level Order Traversal python实现
- Android中关于Dialog滑动弹出的简单动画的设置
- 工作随想
- 转发和重定向的区别
- 28. Implement strStr() [easy] (Python)