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
原创粉丝点击