473,327 Members | 2,081 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,327 software developers and data experts.

what's wrong with this strtok_r()

g
hello!

I get a segm fault when I use strtok_r function

the code :

#ifndef CLIENT_MSG_H_
#define CLIENT_MSG_H_
#include <string>
class Client_msg
{
public:
Client_msg();

~Client_msg();

const char* data() const
{
return data_;
}

char* data()
{
return data_;
}

std::string get_user();
std::string get_service();
std::auto_ptr<std::stringget_arguments();

private:
char data_[2048];
char **last;
};

#endif /*CLIENT_MSG_H_*/



#include "Client_msg.h"

Client_msg::Client_msg()
{
}

Client_msg::~Client_msg()
{
}

std::string Client_msg::get_user()
{
std::string user(strtok_r(data_,"#",last));
return user;
}

std::string Client_msg::get_service()
{
std::string service(strtok_r(data_,"#",last));
return service;
}

std::auto_ptr<std::stringClient_msg::get_arguments ()
{
std::auto_ptr<std::stringarguments(new
std::string(strtok_r(data_,"#",last)));
return arguments;
}
any idea ??

thanks in advance,

Oct 29 '06 #1
4 2870
g wrote:
hello!

I get a segm fault when I use strtok_r function

the code :
[...]
>

any idea ??
Well, a couple... First off, there is no standard 'strtok_r'
function. Second, there are three instance of calling 'strtok_r'
in your code, which one is failing? Third, your code does not
have the 'main' function so I can't compile it and run it to see
the alleged behaviour (even if my compiler did provide the
'strtok_r' function).

Now, could it be that the arguments (like the first one) that
you try to provide to 'strtok_r' is somehow wrong? Like, not
initialised, maybe...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 29 '06 #2
g wrote:
hello!

I get a segm fault when I use strtok_r function

the code :

#ifndef CLIENT_MSG_H_
#define CLIENT_MSG_H_
#include <string>
class Client_msg
{
public:
Client_msg();

~Client_msg();

const char* data() const
{
return data_;
}

char* data()
{
return data_;
}

std::string get_user();
std::string get_service();
std::auto_ptr<std::stringget_arguments();

private:
char data_[2048];
char **last;
};

#endif /*CLIENT_MSG_H_*/



#include "Client_msg.h"

Client_msg::Client_msg()
{
}

Client_msg::~Client_msg()
{
}

std::string Client_msg::get_user()
{
std::string user(strtok_r(data_,"#",last));
return user;
}

std::string Client_msg::get_service()
{
std::string service(strtok_r(data_,"#",last));
return service;
}

std::auto_ptr<std::stringClient_msg::get_arguments ()
{
std::auto_ptr<std::stringarguments(new
std::string(strtok_r(data_,"#",last)));
return arguments;
}
any idea ??

thanks in advance,
Where is data_ initialized?

See 'man strtok_r'.

I assume you are trying to extract a series
of tokens, seperated by the '#' char, from
data_??

strtok_r is a Posix enhancement to strtok.
Note that 'data_' should be passed only on the first
call, pass NULL on subsequent calls. strtok modifies
it input (data_ in this case). As the man pages says:

"Never use these functions."

Read the strtok/strtok_r man page carefully.

Try using a C++ std::string instead of the
C style array (data_).
Oct 30 '06 #3
g
I assume you are trying to extract a series
of tokens, seperated by the '#' char, from
data_??
yes...
strtok_r is a Posix enhancement to strtok.
Note that 'data_' should be passed only on the first
call, pass NULL on subsequent calls. strtok modifies
it input (data_ in this case). As the man pages says:

"Never use these functions."
why????
Try using a C++ std::string instead of the
C style array (data_).
I wish but I have to use char[]. Client_msg use is for getting incoming
data from clients request's and my network lib (Boost.Asio) dont accept
an std::string for read operations! :-(((

so I have to play with C style char []/* to avoid some copies:

std::string request(network_lib.get_data);// where network_lib.get_data
returns a char[]

std::string get_user(std::sting&request);
std::string get_service(std::sting&request);
....

thats why I prefer to play with char and C functions instead of just
copy the char[] to a string.

thanks in advance

Oct 30 '06 #4
g wrote:
>I assume you are trying to extract a series
of tokens, seperated by the '#' char, from
data_??

yes...
>strtok_r is a Posix enhancement to strtok.
Note that 'data_' should be passed only on the first
call, pass NULL on subsequent calls. strtok modifies
it input (data_ in this case). As the man pages says:

"Never use these functions."

why????
>Try using a C++ std::string instead of the
C style array (data_).

I wish but I have to use char[]. Client_msg use is for getting incoming
data from clients request's and my network lib (Boost.Asio) dont accept
an std::string for read operations! :-(((

so I have to play with C style char []/* to avoid some copies:

std::string request(network_lib.get_data);// where network_lib.get_data
returns a char[]

std::string get_user(std::sting&request);
std::string get_service(std::sting&request);
....

thats why I prefer to play with char and C functions instead of just
copy the char[] to a string.

thanks in advance
Read the strtok 'man' pages carefully, and
ask further questions in one of these newsgroups:

comp.os.linux.development.system
comp.os.linux.development.apps
Oct 30 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mike Henley | last post by:
I first came across rebol a while ago; it seemed interesting but then i was put off by its proprietary nature, although the core of the language is a free download. Recently however, i can't...
72
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
28
by: Madhur | last post by:
Hello what about this nice way to open a file in single line rather than using if and else. #include<stdio.h> void main() { FILE *nd; clrscr();...
23
by: kbhat | last post by:
I had written some code using strtok_r for parsing strings with two "levels" of delimiters. The code was working like a charm, until it suddenly broke one day. You see, I was applying strtok on...
9
by: Pyenos | last post by:
import cPickle, shelve could someone tell me what things are wrong with my code? class progress: PROGRESS_TABLE_ACTIONS= DEFAULT_PROGRESS_DATA_FILE="progress_data" PROGRESS_OUTCOMES=
3
by: Siong.Ong | last post by:
Dear all, my PHP aims to update a MySQL database by selecting record one by one and modify then save. Here are my PHP, but I found that it doesnt work as it supposed to be, for example, when...
75
by: siddhu | last post by:
Dear experts, As I know strtok_r is re-entrant version of strtok. strtok_r() is called with s1(lets say) as its first parameter. Remaining tokens from s1 are obtained by calling strtok_r() with...
24
by: MU | last post by:
Hello I have some code that sets a dropdownlist control with a parameter from the querystring. However, when the querystring is empty, I get an error. Here is my code: Protected Sub...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.