字符串统计-很奇怪的segmentation fault(core dumped)的问题。c++

问题描述

很奇怪的segmentation fault(core dumped)的问题。c++

源代码如下,统计文件中字符串出现个数

#include
#include
#include
#include
using namespace std;
int Num;
int main(int argc,char *argv[]){ //参数分别是字符串和文件
int length=strlen(argv[1]); //字符串长度
ifstream ifile;
ifile.open(argv[2],ios::binary);
ifile.seekg(0,ios::beg);
char *temp, *ptr,*p; //temp一行行读入文件内容,*ptr *索引指针
while(1){
if(ifile.tellg()==EOF)break; //判断文件是否结束
//cout<<ifile.tellg()<<endl;
ifile.getline(temp,1024);
ptr=temp;

           (注意!!!!!) //cout<<temp<<strlen(temp)<<endl;

//cout<<Num<<endl;
while(1){

  if(p=strstr(ptr,argv[1])){
   Num++;
//cout<<p<<endl;
   ptr=p+length;
  }else break;
}
 memset(temp,0,1024);

}

cout<<Num<<endl;

}

测试文件text.txt/dat(其中有空格论坛不能显示,所以第一次运行统计数字和看起来不一样)

helloworldhelloworld
helloworld

world

world

helloworld

运行./a.out world text.txt
两次运行结果
第一次("注意"那行没有被注释时结果)
helloworldhelloworld20
helloworld12
world 7
world 8
helloworld10//temp&strlen(temp)
0//最后一次读入空,长度为0
6//Num

第二次注释那一行,重新编译运行,显示错误
segmentation fault(core dumped)

按理那一行没什么作用,只是看读入是否正确用的,为什么会对结果产生影响???
环境是fedora
另,如果换成windows 不报错,但结果恒为0
ubuntu即使有那一行也报错,错误一样segmentation fault(core dumped)

解决方案

你需要给temp分配内存,有或没有那行代码,你的程序都可能crash.

最简单的办法:

char *temp ==》 char temp[1025];

解决方案二:

temp空间不够,只是一个指针
char temp[2048];

时间: 2024-07-30 23:44:52

字符串统计-很奇怪的segmentation fault(core dumped)的问题。c++的相关文章

c语言-求字符数目,输入字符串时显示Segmentation fault (core dumped)

问题描述 求字符数目,输入字符串时显示Segmentation fault (core dumped) int main(void){ char a[100]; printf(""please input char:n""); scanf(""%s"" &a); f(a); return 0;}void f(char *a){ int i = 0; int space = 0 line = 0 others = 0;

c++-Segmentation fault (core dumped)!!

问题描述 Segmentation fault (core dumped)!! C++ 三个c++文件都include了同一个头文件,那个头文件里有 static int global1 = 0; static int global2 = 0; extern someClass A; 我把这三个注释掉,代码中用到这三个变量的地方也注释掉就没问题了.. 是因为static不能同时被三个cpp文件使用吗?求大神带飞orz 解决方案 是不是访问A时出错了.要你的具体出错的位置. 解决方案二: 不是的

printf-linux 下 segmentation fault (core dumped) 错误出在哪里

问题描述 linux 下 segmentation fault (core dumped) 错误出在哪里 #include #include int sum; void * runner(void * param); int main(int argc,char * argv[]) { pthread_t tid; pthread_attr_t attr; if(argc<2) { printf("the number of paraments shuold more than 1&quo

启动时celery worker出错”Segmentation fault” (core dumped)解决方法

在我的Ubuntu 14.04 上, 使用"celery worker"命令启动celery worker时,遇到"Segmentation fault" (core dumped),如下: jay@jay-linux:~/workspace/aew$ celery -A aew worker  代码如下 复制代码  -------------- celery@jay-linux v3.1.13 (Cipater) ---- **** ----- --- * ***

linux c-Linux C 编程中segmentation fault (core dump)错误

问题描述 Linux C 编程中segmentation fault (core dump)错误 Linux C 小白,求大神指导,不胜感激.原代码在Windows下用VC++6.0编写,能正常运行,后复制到Ubuntu 12.04下,改了一部分命令,编译能通过,但在运行时报错segmentation fault (core dump).下面为代码: #include #include #include #include #include double num; int WriteDat(){

调试-segment fault:core dumped

问题描述 segment fault:core dumped 程序里有下面两个函数: 小文件没有问题,但是当文件达到1k行左右,会出现段错误.错误在第二个wlhile 语句的fgetc处. void next(char c,int *row,int *col) { if(c == ' ') *col = (*col + TABLEN - 1) / TABLEN * TABLEN + 1; else if(c == ' ') { cur_state.line_endpos[*row] = *col

Linux下调试段错误的方法[Segmentation Fault]--GDB

原文 1.段错误是什么? 段错误是指访问的内存超出了系统给这个程序所设定的内存空间,例如访问了不存在的内存地址.访问了系统保护的内存地址.访问了只读的内存地址等等情况. A segmentation fault (often shortened to segfault) is a particular error condition that can occur during the operation of computer software. In short, a segmentation

c-C语言输出数字团,代码为什么出现Segmentation fault,求解释并纠错??

问题描述 C语言输出数字团,代码为什么出现Segmentation fault,求解释并纠错?? //题目:输出字符串里数字团,以及个数.比如89cy129 //就输出89,129,一共2个数. 代码在这个百度知道的问题上:well,看来百度知道上面没有人会做? 跪谢!!! 源代码: include int main() { int a[80],i=0,*pa,an=0; char str[80],*pstr=str; void getnum(char pstr); gets(str); get

指针-奇怪的segmetation fault

问题描述 奇怪的segmetation fault 我动态申请了一个很大的数组,大约有十几万个元素.... 数组的赋值和输出(指的是把数组printf出来)都没有问题,但是用数组指针用作其他操作时会报 segmentation fault,为了检测,我修改了指针应用方式,改成挨个读写操作,也就是array[i] 这样挨个读,发现在大约14980时候报segmentation fault,到底是为什么?我该怎么解决?