471,123 Members | 962 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,123 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 2761
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Mike Henley | last post: by
72 posts views Thread by E. Robert Tisdale | last post: by
121 posts views Thread by typingcat | last post: by
28 posts views Thread by Madhur | last post: by
23 posts views Thread by kbhat | last post: by
9 posts views Thread by Pyenos | last post: by
3 posts views Thread by Siong.Ong | last post: by
75 posts views Thread by siddhu | last post: by
24 posts views Thread by MU | last post: by

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.