473,659 Members | 3,082 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<s td::stringget_a rguments();

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

#endif /*CLIENT_MSG_H_*/



#include "Client_msg .h"

Client_msg::Cli ent_msg()
{
}

Client_msg::~Cl ient_msg()
{
}

std::string Client_msg::get _user()
{
std::string user(strtok_r(d ata_,"#",last)) ;
return user;
}

std::string Client_msg::get _service()
{
std::string service(strtok_ r(data_,"#",las t));
return service;
}

std::auto_ptr<s td::stringClien t_msg::get_argu ments()
{
std::auto_ptr<s td::stringargum ents(new
std::string(str tok_r(data_,"#" ,last)));
return arguments;
}
any idea ??

thanks in advance,

Oct 29 '06 #1
4 2893
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<s td::stringget_a rguments();

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

#endif /*CLIENT_MSG_H_*/



#include "Client_msg .h"

Client_msg::Cli ent_msg()
{
}

Client_msg::~Cl ient_msg()
{
}

std::string Client_msg::get _user()
{
std::string user(strtok_r(d ata_,"#",last)) ;
return user;
}

std::string Client_msg::get _service()
{
std::string service(strtok_ r(data_,"#",las t));
return service;
}

std::auto_ptr<s td::stringClien t_msg::get_argu ments()
{
std::auto_ptr<s td::stringargum ents(new
std::string(str tok_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::s ting&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::s ting&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.d evelopment.syst em
comp.os.linux.d evelopment.apps
Oct 30 '06 #5

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

Similar topics

3
2372
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 help but say i was totally impressed. I needed an open source wikiblog/wikilog, whatever you wanna call it, basically a hybrid of a blog and a wiki. I checked out snipsnap, which uses java, it was said on their site to be a clone of vanilla, a...
72
5833
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 example, between a C/C++ programmers with a few weeks of experience and a C/C++ programmer with years of experience. You don't really need to understand the subtle details or use the obscure features of either language
121
10025
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 support IDEs are DreamWeaver 8 and Zend PHP Studio. DreamWeaver provides full support for Unicode. However, DreamWeaver is a web editor rather than a PHP IDE. It only supports basic IntelliSense (or code completion) and doesn't have anything...
28
3262
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(); fopen("c:\\autoexec.bat","r")&&printf("success") || printf("error opeing
23
3758
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 the input string itself, and as long as a variable string was passed to the function, everything was hunky dory. Then one day somebody decided to pass a constant string to my code ---- and everything came collapsing down like a domino. I...
9
2117
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
2140
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 Record (i) is shown and modified, the change will come to Record (i+1). Can anyone provide suggestion? thanks.
75
25092
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 a null pointer for the first parameter. My confusion is that this behavior is same as strtok. So I assume strtok_r must also be using any function static variable to keep the information about s1. If this is the case then how strtok_r is re-...
24
3087
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 Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8747
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8627
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2752
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1976
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.