what's wrong with this strtok_r()

G

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::string> get_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::string> Client_msg::get_arguments()
{
std::auto_ptr<std::string> arguments(new
std::string(strtok_r(data_,"#",last)));
return arguments;
}


any idea ??

thanks in advance,
 
V

Victor Bazarov

g said:
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
 
L

Larry Smith

g said:
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::string> get_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::string> Client_msg::get_arguments()
{
std::auto_ptr<std::string> arguments(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_).
 
G

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
 
L

Larry Smith

g said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top