[英]Segmentation fault (core dumped)
I have 3 files namely concat.cpp, concat.h and test4concat.cpp, when i compile and execute i get the following error. 我有3个文件,即concat.cpp,concat.h和test4concat.cpp,在编译和执行时出现以下错误。
Enter the number of splits: 1 Segmentation fault (core dumped) 输入分割数:1分段故障(核心已转储)
It asks for the first split and then stops, since i am fairly new to cpp i would need some help on this. 它要求先拆分然后停止,因为我对cpp还是很陌生,因此我需要一些帮助。 Thanks 谢谢
Following are the 3 file 以下是3档
concat.cpp concat.cpp
#include <iostream>
#include <cstring>
#include <fstream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "concat.h"
using namespace std;
char concat(char* si,char* w,char* fid)
{
strcat (si,w);
strcat (si,fid);
return 0;
}
concat.h concat.h
#ifndef TRY_H_INCLUDED
#define TRY_H_INCLUDED
char concat(char* si,char* w,char* fid);
#endif
test4concat.cpp test4concat.cpp
#include <iostream>
#include <cstring>
#include <fstream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <concat.h>
using namespace std;
int main ()
{
char* si;
char* w;
char* fid;
cout << "Enter the number of splits: ";
cin >> si;
cout << "Enter the number of watchdogs: ";
cin >> w;
cout << "Enter the Fid: ";
cin >> fid;
concat(si, w, fid);
cout<<"\nThe string is "<< si <<endl;
}
Problem which i am encountering: 我遇到的问题:
Enter the number of splits: 1 Segmentation fault (core dumped) 输入分割数:1分段故障(核心已转储)
You need to allocate memory using malloc (or new as you are writing in C++) before reading data into si, w and fid. 在将数据读入si,w和fid之前,您需要使用malloc(或用C ++编写的新方法)分配内存。
si = new char[10];
w = new char[10];
fid = new char[10];
Of course you need to modify the sizes of the character arrays for your own requirements. 当然,您需要根据自己的需要修改字符数组的大小。
This would be a way to do it in C++, avoiding all manual memory allocation pitfalls: 这将是在C ++中做到这一点的一种方法,避免了所有手动内存分配陷阱:
#include <iostream>
#include <string>
int main ()
{
std::string si, w, fid;
std::cout << "Enter the number of splits: ";
std::cin >> si;
std::cout << "Enter the number of watchdogs: ";
std::cin >> w;
std::cout << "Enter the Fid: ";
std::cin >> fid;
si += w;
si += fid;
std::cout<<"\nThe string is "<< si << std::endl;
}
"Enter the number of splits: "; “输入分割数:”;
So you want a number, int
. 所以你想要一个数字, int
。
int si;
cout << "Enter the number of splits: ";
cin >> si;
If you really want to read through pointer, first allocate memory to it with operator new
: 如果您真的想通读指针,请先使用operator new
为它分配内存:
int main() {
char* pt = new char;
cin >> pt;
cout << (*pt);
delete pt;
return 0;
}
There is a std::string
class being a C++
string of characters, a specialization of std::basic_string
with char
as a template parameter. 有一个std::string
类,它是一个C++
字符串,是std::basic_string
其中char
作为模板参数。 It is flexible and may be used in this case as well: 它很灵活,在这种情况下也可以使用:
#include <iostream>
#include <string>
int main ()
{
std::string si, w, fid;
std::cout << "Enter the number of splits: ";
std::cin >> si;
std::cout << "Enter the number of watchdogs: ";
std::cin >> w;
std::cout << "Enter the Fid: ";
std::cin >> fid;
si += w;
si += fid;
std::cout<<"\nThe string is "<< si << std::endl;
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.