segfault strlen [英] segfault strlen
问题描述
你好!!
我正在学习c ++而我正在努力让一小段代码正常工作,
但是我得到了一个这里有strlen与strlen:
void tabhash :: set(url * U){
uint hash = U-> hashCode();
char * url = U-> giveUrl();
char * chash =(char *)hash;
char * Insert1 =" INSERT INTO sep_hashtable VALUES(" ;;
char * Insert2 =",0,''" ;;
char * Insert3 ="'') ;" ;;
char * Insert = new
char [strlen(Insert1)+ strlen(chash)+ strlen(Insert2)+ strl en(url) )+ strlen(Insert3)];
strcpy(Insert,Insert1);
strcat(Insert,chash);
strcat(Insert, Insert2);
strcat(Insert,url);
strcat(Insert,Insert3);
if(mysql_query(& mysql,Insert) )){
cerr<< 无法插入sep_hashtable:"
<< strerror(errno)<<结束;
退出(1);
}
}
我不能找出为什么我得到这个段错误...你能帮助我吗
a bit?
< da ************ @ gmail.comwrote in message
news:11 ******* **************@m7g2000cwm.googlegrou ps.com ...
你好!!
我正在学习c ++而且我正在努力让一小段代码工作,
但是我在这里得到了strlen的段错误:
void tabhash :: set(url * U){
uint hash = U-> hashCode();
char * url = U-> giveUrl();
char * chash =(char *)hash;
char * Insert1 =" INSERT INTO sep_hashtable VALUES(" ;;
char * Insert2 =",0,''" ;;
char * Insert3 ="'');" ;;
char * Insert = new
char [strlen(Insert1)+ strlen(chash)+ s trlen(Insert2)+ strl en(url)+ strlen(Insert3)];
strcpy(Insert,Insert1);
strcat(Insert,chash);
strcat(Insert,Insert2);
strcat(Insert,url);
strcat(Insert,Insert3);
if(mysql_query(& mysql,Insert)){
cerr<< 无法插入sep_hashtable:"
<< strerror(errno)<<结束;
退出(1);
}
}
我不能找出为什么我得到这个段错误...你能帮帮我
a bit?
除了已经提到的C风格演员
一个未签名的char *,你有新的
你显示但没有删除。你正在创建一个char
数组,其中有足够的空间来容纳你所放的
,但没有空格来终止null。这个
可能是正确的,但这取决于函数
mysql_query()所期望的。例如,如果它'
期望一个空终止的char *并且内部是
尝试strcpy()等等你可以获得
a seqfault。
为什么不避免所有这一切并使用std :: string?
你也叫退出(1 )没有做任何
清理(虽然你没有显示任何
使用此功能。)
这是通常不会做的事情在c ++程序中是
,因为它会绕过dtors和
这样。为什么不让你的功能返回
a bool表示成功并让调用者
决定如果失败怎么办?
< blockquote>
da ************ @ gmail.com 写道:
你好!!
我正在学习c ++
不,你不是。也许这就是你所说的,但是快速浏览一下
你的代码很明显你正在学习C - 而不是C ++(使用新的
而不是malloc不改变这个)。所以在C $ / $
论坛中提出你的问题或者你的基本知识是正确的(下面几乎没有显示可能是/ b $ b描述为正确的C ++ - 特别是在学习时
东西)。
我正试图让一小段代码工作,
但是我在这里得到了strlen的段错误:
void tabhash :: set(url * U){
uint hash = U-> hashCode();
char * url = U-> giveUrl();
char * chash =(char *)hash;
char * Insert1 =" INSERT INTO sep_hashtable VALUES(" ;;
char * Insert2 =",0,''" ;;
char * Insert3 ="'');" ;;
char * Insert = new
char [strlen(Insert1)+ strlen(chash)+ strlen( Insert2)+ strl en(url)+ strlen(Insert3)];
strcpy(Insert,Insert1);
strcat(Insert,chash);
strcat(Insert,Insert2);
strcat(Ins ert,url);
strcat(Insert,Insert3);
if(mysql_query(& mysql,Insert)){
cerr< ;< 无法插入sep_hashtable:"
<< strerror(errno)<<结束;
退出(1);
}
}
我不能找出为什么我得到这个段错误...你能帮帮我
a bit?
当然 - 转换为C ++,我们很乐意这样做。
/ Peter
Hello!!
I''m studying c++ and I''m trying to get a little piece of code working,
but I''m getting a segfault with strlen here:
void tabhash::set (url *U) {
uint hash = U->hashCode();
char* url = U->giveUrl();
char* chash = (char*)hash;
char* Insert1="INSERT INTO sep_hashtable VALUES (";
char* Insert2=",0,''";
char* Insert3="'');";
char* Insert=new
char[strlen(Insert1)+strlen(chash)+strlen(Insert2)+strl en(url)+strlen(Insert3)];
strcpy( Insert, Insert1);
strcat( Insert, chash);
strcat( Insert, Insert2);
strcat( Insert, url);
strcat( Insert, Insert3);
if (mysql_query(&mysql,Insert)) {
cerr << "Cannot insert into sep_hashtable : "
<< strerror(errno) << endl;
exit(1);
}
}
I can''t find out why I''m getting this segfault error...can you help me
a bit?
<da************@gmail.comwrote in message
news:11*********************@m7g2000cwm.googlegrou ps.com...Hello!!
I''m studying c++ and I''m trying to get a little piece of code working,
but I''m getting a segfault with strlen here:
void tabhash::set (url *U) {
uint hash = U->hashCode();
char* url = U->giveUrl();
char* chash = (char*)hash;
char* Insert1="INSERT INTO sep_hashtable VALUES (";
char* Insert2=",0,''";
char* Insert3="'');";
char* Insert=new
char[strlen(Insert1)+strlen(chash)+strlen(Insert2)+strl en(url)+strlen(Insert3)];
strcpy( Insert, Insert1);
strcat( Insert, chash);
strcat( Insert, Insert2);
strcat( Insert, url);
strcat( Insert, Insert3);
if (mysql_query(&mysql,Insert)) {
cerr << "Cannot insert into sep_hashtable : "
<< strerror(errno) << endl;
exit(1);
}
}
I can''t find out why I''m getting this segfault error...can you help me
a bit?Aside from the already mentioned C style cast
of an unsigned to a char*, you have new in what
you show but no delete. You''re creating a char
array with enough space to hold what you put
in it, but no space for a terminating null. This
could be correct but it depends on what the function
mysql_query() is expecting. For example, if it''s
expecting a null terminated char* and internally is
trying to strcpy() something etc. you could get
a seqfault.
Why not avoid all of this and use std::string?
You''re also calling exit(1) without doing any
cleanup (though you aren''t showing any
usage of this function.)
This is not usually something done
in c++ programs as it bypasses dtors and
such. Why not have your function return
a bool indicating success and let the caller
decide what to do if it fails?
da************@gmail.com wrote:Hello!!
I''m studying c++No you are not. Perhaps that is what you''re told, but a quick glance at
your code makes it evident that you are learning C - not C++ (using new
instead of malloc does not change this). So ask your question in a C
forum or get your basics right (almost nothing shown below could be
described as "proper" C++ - especially not when one is learning
something).
and I''m trying to get a little piece of code working,
but I''m getting a segfault with strlen here:
void tabhash::set (url *U) {
uint hash = U->hashCode();
char* url = U->giveUrl();
char* chash = (char*)hash;
char* Insert1="INSERT INTO sep_hashtable VALUES (";
char* Insert2=",0,''";
char* Insert3="'');";
char* Insert=new
char[strlen(Insert1)+strlen(chash)+strlen(Insert2)+strl en(url)+strlen(Insert3)];
strcpy( Insert, Insert1);
strcat( Insert, chash);
strcat( Insert, Insert2);
strcat( Insert, url);
strcat( Insert, Insert3);
if (mysql_query(&mysql,Insert)) {
cerr << "Cannot insert into sep_hashtable : "
<< strerror(errno) << endl;
exit(1);
}
}
I can''t find out why I''m getting this segfault error...can you help me
a bit?Sure - convert to C++ and we''ll be happy to do so.
/Peter
这篇关于segfault strlen的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!