本文介绍了c_str()执行SEGSEGV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 嗨 我有一个程序,其中我有以下声明。 sprintf(somevar,"%s /% s。%s",PREFIX.c_str(),MIDDLE.c_str( ),SUFFIX.c_str()); 这里是PREFIX,MIDDLE和SUFFIX都是常量字符串。 这个工作正常,但最近在接近1000次成功运行后,我的 程序被SIGSEGV抛弃了。 我在gdb提示符下对核心文件做了一个bt,发现问题是 c_str() 我正在使用gcc 3.2.3 libstdc ++。so.5 RH linux AS 3上的 是spriontf语句是的,还是有更好的方法。 谢谢 ShyamHiI have a program within which i have the following statement.sprintf(somevar,"%s/%s.%s",PREFIX.c_str( ),MIDDLE.c_str(),SUFFIX.c_str( ) );Here PREFIX, MIDDLE and SUFFIX are all const strings.This works fine but recently after close to 1000 successful runs myprogram dumped with SIGSEGV.I did a bt on the core file at gdb prompt and found that the problem iswith c_str( )I am using gcc 3.2.3libstdc++.so.5on RH linux AS 3.Is the spriontf statement correct , or is there any better way.thanksShyam推荐答案 什么是somevar ?What is "somevar" ? 您是否尝试使用stringstream或正常字符串连接? - SirMike - http://www.sirmike.org C让你轻松拍摄自己的脚; C ++让它变得更难,但是当你这样做时,它会让你的整条腿感到震惊。 - Bjarne StroustrupDid you try to use stringstream or normal string concatenation?--SirMike - http://www.sirmike.orgC makes it easy to shoot yourself in the foot; C++ makes it harder, butwhen you do, it blows away your whole leg. - Bjarne Stroustrup 我会责怪你的一些(未显示)代码,可能 会覆盖字符串的一些内部存储器,因此 c_str()段错误。 < OT> 如果您使用的是32位平台,请尝试valgrind精确定位 错误。 < / OT> HTH, - J. br />I''d blame some of your (not shown) code that probablyoverwrites some internal memory of the strings, hencethe c_str() segfault.<OT>If you are on a 32-bit platform, try valgrind to pinpointthe error.</OT>HTH,- J. 我会责怪你的一些(未显示)代码可能 覆盖字符串的一些内部存储器,因此 c_str()段错误。 < OT> 如果您使用的是32位平台,请尝试valgrind精确定位 错误。 < / OT> HTH, - J. I''d blame some of your (not shown) code that probablyoverwrites some internal memory of the strings, hencethe c_str() segfault.<OT>If you are on a 32-bit platform, try valgrind to pinpointthe error.</OT>HTH,- J. 这篇关于c_str()执行SEGSEGV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!