By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,849 Members | 2,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,849 IT Pros & Developers. It's quick & easy.

segfault strlen

P: n/a
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?

Sep 28 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a

<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?
Sep 28 '06 #2

P: n/a

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

Sep 28 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.