473,382 Members | 1,107 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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?

Sep 28 '06 #1
2 3175

<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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Juho Saarikko | last post by:
The program attached to this message makes the Python interpreter segfault randomly. I have tried both Python 2.2 which came with Debian Stable, and self-compiled Python 2.3.3 (newest I could find...
6
by: deejaybags | last post by:
could someone please have a look at this and tell me why it segfaults. i am confused as all hell! stringman.h void load_string(char *newString); char * remove_string(); char *...
2
by: vlindos | last post by:
I have this code <code> #include <stdio.h> #include <stdlib.h> #include <mysql/mysql.h> char locale_loaded; int i18n_num;
40
by: Fatih Gey | last post by:
Hi, .. following causes a segfault. .. didn't know why ?! int main() { char name; strcpy (name, "ab8bc8cd8ed"); char cur;
10
by: name | last post by:
When I started testing the algorithms for my wrap program, I threw together this snippet of code, which works quite well. Except that it (predictably) segfaults at the end when it tries to go...
165
by: Dieter | last post by:
Hi. In the snippet of code below, I'm trying to understand why when the struct dirent ** namelist is declared with "file" scope, I don't have a problem freeing the allocated memory. But...
162
by: Richard Heathfield | last post by:
I found something interesting on the Web today, purely by chance. It would be funny if it weren't so sad. Or sad if it weren't so funny. I'm not sure which. ...
10
by: danielesalatti | last post by:
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 =...
5
by: Zach | last post by:
Wrote simple program to setup a char array of pointers and print each one. It runs with the expected output however it then causes a segmentation fault. Could someone take a lot at the code? My...
2
by: aviraldg | last post by:
I'm having a SEGFAULT on the following program. Weird thing is that it happens randomly with different "str"s (ie. user input; check main() for more info) and my debugger shows me that the getToken...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.