LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-12-2006, 11:55 AM   #1
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
stringstream as class member variable


hi everybody.

i have a Message class that takes params of all different sorts of types, and it takes those param values and converts them to strings to be passed within the message. then when the message is read the reader has to know what type a param is supposed to be to convert it from string to that type. the current implementation is using sprintf for number to string conversions and aoti/atof for string number conversions. i want to move away from using those functions and use a stringstream to convert. the weird issue is whenever i add a stringstream to the class, it causes a segfault. ?? it seems extremely weird to me, i have read some things about the possiblity of the underlying string implementation not being std::string and this could cause problems. but i dont see that this applies to me, afaik everything is using the std c++ libs. here is a short and simple example of what i am doing..

Code:
class AAMessage
{
    public:
        ....

    private:
        stringstream ss;
};
if i comment out the stringstream dec, it works. if i leave it in it segfaults. the weirdness increases because i reverted back to the original message, and just added the stringstream and did not use it or reference it anywhere else in the app, and it still crashes.. i comment it out and it works fine.. ?

any ideas.. even the slightest of leads would be appreciated..

thanks
-x
 
Old 10-12-2006, 01:34 PM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Have you included sstream?
Why isn't there an std:: infront of stringstream? have you said using std::stringstream or using namespace std?
 
Old 10-12-2006, 01:35 PM   #3
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
can you post the whole code and exact error message with line number and all
 
Old 10-12-2006, 01:36 PM   #4
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Original Poster
Rep: Reputation: 45
sorry guys i must not have been that clear.. this is not a compile time error.. this is a runtime error. upon the creation of the first instance of this class it crashes with a segfualt.
 
Old 10-12-2006, 01:40 PM   #5
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
can you post the full code of the files involved
 
Old 10-12-2006, 02:43 PM   #6
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by xhi
sorry guys i must not have been that clear.. this is not a compile time error.. this is a runtime error. upon the creation of the first instance of this class it crashes with a segfualt.
Nope I thought this was the case. I was just checking because there wasn't much else to comment on . wink wink
 
Old 10-12-2006, 03:23 PM   #7
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Original Poster
Rep: Reputation: 45
Quote:
Originally Posted by nadroj
can you post the full code of the files involved
no i cant.. its not personal code..

but the basic of this is, i have a class
Code:
class foo
{
   stringstream ss;
};
as soon as i do a
Code:
...
foo f;
it segfaults.. if i remove the stringstream from the class, there is no segfault. add it back and it segfaults.. the stringstream is never used anywhere else in the class, so i am assuming there is something with the way the stringstream is being constructed, i tried different things in the initializer list for the class, but nothing makes any difference.. very very strange..
 
Old 10-12-2006, 03:27 PM   #8
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
no i cant.. its not personal code..
Well theres no much I can suggest to you, if you want to email me the code feel free.
 
Old 10-12-2006, 03:51 PM   #9
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
can you post the full code of just the AAMessage class? or at least all of the constructor
 
Old 10-13-2006, 09:53 AM   #10
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Original Poster
Rep: Reputation: 45
Quote:
Originally Posted by nadroj
can you post the full code of just the AAMessage class? or at least all of the constructor
no i cant.. ill post an example that resembles the class in question.. the constructor is trivial, there is nothing in the body, and it takes two ints and assigns them to int members in the init list. example
Code:
Foo::Foo(int a, int b) : m_a(a), m_b(b) {}
not much there.. i tried to init the strinstream in the init list, but it did not help..

good news though the problem has disappeared.. i wonder if i just have run into a strange set of circumstances, or if there is something to this.. anyhow this is what made the problem go away.. so in the Message i have a nested class that is trivial, it is just a tuple with a string and int.. originally i had the stringstream dec before the nested class, and it was not working.. through some stroke of luck i thought to try it after the dec, and it works.. just to make sure i was not going crazy i moved it back before the dec and it does not work.

example..
Code:
class Message
{
   ...
   private:
      .... // some ints and stuff
      // if stringstream is here, it crashes
      struct Param
      { 
         int name;
         string value;
      }
      // if stringstream is here it works fine..
}
this does not make much sense to me, but is there some reason that this would be?
 
Old 10-13-2006, 10:15 AM   #11
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by xhi
this does not make much sense to me, but is there some reason that this would be?
Yes... but I can't tell you
 
Old 10-13-2006, 11:05 AM   #12
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Original Poster
Rep: Reputation: 45
Quote:
Originally Posted by dmail
Yes... but I can't tell you
haha thats about the same response ive been getting here at work..

for some reason lint quit running on this library, i need to get it working again.. there might be an answer or clue there.. im moving on, but i would definately be interested to hear a reason though..

thanks for the interest guys.
 
Old 10-13-2006, 11:30 AM   #13
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
I'm glad you took that comment the way it was intended.

Honestly I do not know of any reason why this should occur and why moving it would resolve the problem, I have been unable to reproduce this behaviour and would suspect that the problem is not the stringstream but something else.
Can you reproduce this with the class isolated?
How are you clearing the stream of characters?

If you could post what compiler version your using and a foo type single class which can reproduce the behavior I would be interested to see and try and figure it out.

Last edited by dmail; 10-13-2006 at 11:31 AM.
 
Old 10-13-2006, 11:36 AM   #14
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Original Poster
Rep: Reputation: 45
Quote:
Originally Posted by dmail
I'm glad you took that comment the way it was intended.

Honestly I do not know of any reason why this should occur and why moving it would resolve the problem, I have been unable to reproduce this behaviour and would suspect that the problem is not the stringstream but something else.
Can you reproduce this with the class isolated?
How are you clearing the stream of characters?

If you could post what compiler version your using and a foo type single class which can reproduce the behavior I would be interested to see and try and figure it out.
yeh. when i get a minute ill do that and see if it duplicates alone.. i would agree that it is something other than stringstream. either something i am doing, or this god-awful build process for this project.. i highly doubt that i found some sort of bug in anything that is standard, especially since this class is pretty trivial..

ill try to pull it out later and test.. and if i can reproduce it ill put something up here for you to test.. i have to make up for horribly slow progress the past two days now.. that means lunch consists of a cigarette and this post..

thanks dmail
 
Old 10-13-2006, 11:49 AM   #15
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Just an after thought which would not explain why moving the declaration would work but.
Are you storing or passing the pointer returned by .str()?, this could cause a seg fault.

[edit]forget the above I mean c_str() function of the str() lol.

Last edited by dmail; 10-13-2006 at 11:52 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Indirection in class member declaration websinger Programming 8 09-08-2006 10:24 AM
passing a class member function to pthread_create. dmail Programming 1 07-29-2006 11:15 AM
C++: difference between static method variable and static class variable? Thinking Programming 3 01-16-2006 10:08 AM
C++ / Passing a variable by reference to a class member function. sepulture Programming 12 11-15-2005 10:23 PM
a class with a ifstream member Hano Programming 2 04-24-2002 11:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration