20191216从标准输入流中输入三个字符串,每输入一段字符串动态分配内存,实时存储在动态内存中

/*编写一个程序,使用 cin 从标准输入输入 3 段文字,保存到一段动态分配的 内存中,
  每一段文字输入后,必须要即时保存到动态内存中。
*/
#define _CRTDBG_MAP_ALLOC 
#include <stdlib.h>
#include <crtdbg.h>///以上三行是内存泄露检测文件头

#include <Windows.h>
#include<iostream>
#include<string>
#include<stdio.h>

#ifdef _DEBUG
#ifndef DBG_NEW 
#define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ ,__LINE__) 
#define new DBG_NEW 
#endif 
#endif //接管new操作符

using namespace std;
int main() {
    string strInput1, strInput2, strInput3;
    cout << "请输入一段文字:\n";
    cin >> strInput1;
    char* p1 = new char[strInput1.length()+1];
    strcpy(p1, (char*)strInput1.c_str());//将string 类型的字符串转换为c语言标准的字符串格式,以C语言的'\0'结尾

    cout << "请输入一段文字:\n";
    cin >> strInput2;
    char* p2 = new char[strInput2.length()+1];
    strcpy(p2, (char*)strInput2.c_str());//将string 类型的字符串转换为c语言标准的字符串格式,以C语言的'\0'结尾

    cout << "请输入一段文字:\n";
    cin >> strInput3;
    char* p3 = new char[strInput3.length()+1];
    strcpy(p3, (char*)strInput3.c_str());//将string 类型的字符串转换为c语言标准的字符串格式,以C语言的'\0'结尾

    printf_s("你输入的三段字符串如下:%s\t%s\t%s\n",p1,p2,p3);
    printf_s("你输入的三段字符串地址如下:0x%p\t0x%p\t0x%p\n", p1, p2, p3);

    delete [] p1;
    delete [] p2;
    delete [] p3;

    _CrtDumpMemoryLeaks();

    system("pause");
    return 0;
}

运行结果如下:
在这里插入图片描述

发布了51 篇原创文章 · 获赞 0 · 访问量 535

猜你喜欢

转载自blog.csdn.net/weixin_40071289/article/details/103566058
今日推荐