RLHaides 2017-02-23 13:24 采纳率: 75%
浏览 1277

这个是什么情况,有人帮帮我吗

This time, you need to create tow function:

void Assign(char * & str, int & length); According to the length, allocate memory to str.
void AddTwo(string & res, char & str1, char & str2); Add str1 and str2, then assign the result to the res. More importantly, you need to handle str1 and str2 to guarantee that there would not happen memory problem.
Sample input:

4
abcd
5
efghi
Sample ouput:

abcd
efghi
abcdefghi

my code
#ifndef EasyStr
#define EasyStr
#include
using std::cin;
using std::string;
void Assign(char * &, int &);
void AddTwo(string &, char * &, char * &);
#endif

#include"EasyString.hpp"
#include
#include
#include
using namespace std;

void Assign(char *& str,int& length){
str = new char[length];

}

void AddTwo(string & res , char & str1,char& str2){

res.assign(str1);
res += str2;

}

#include
#include "EasyString.hpp"

using namespace std;
int main() {
int length;
char* str0 = NULL, * str1= NULL;
cin >> length;
Assign(str0, length);
cin >> length;
Assign(str1, length);
cin >> str0 >> str1;
cout << str0 << endl << str1 << endl;
string res = "";
AddTwo(res, str0, str1);
cout << res << endl;
}

结果,评测
[1] Error : InvalidWrite
obj: /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
std::basic_istream >& std::operator>> >(std::basic_istream >&, char*)
[2] Error : InvalidWrite
obj: /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
std::basic_istream >& std::operator>> >(std::basic_istream >&, char*)
[3] Error : InvalidRead
obj: /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so
strlen
[4] Error : InvalidRead
obj: /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so
strlen
[5] Error : InvalidRead
obj: /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so
strlen
[6] Error : InvalidRead
obj: /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so
strlen
[7] Error : Leak_DefinitelyLost
obj: /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so
operator new
[8] Error : Leak_DefinitelyLost
obj: /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so
operator new

  • 写回答

2条回答 默认 最新

  • threenewbee 2017-02-23 15:40
    关注

    你输入的length是多少,是否没有读取到

    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀