Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old September 28th, 2006, 11:15 AM
danielesalatti@gmail.com
Guest
 
Posts: n/a
Default segfault strlen

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?

  #2  
Old September 28th, 2006, 12:35 PM
Duane Hebert
Guest
 
Posts: n/a
Default Re: segfault strlen


<danielesalatti@gmail.comwrote in message
news:1159439347.682009.46570@m7g2000cwm.googlegrou ps.com...
Quote:
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?


  #3  
Old September 28th, 2006, 03:45 PM
peter koch
Guest
 
Posts: n/a
Default Re: segfault strlen


danielesalatti@gmail.com wrote:
Quote:
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).
Quote:
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

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles