stringstream: runtime error assistance

I'm not new, but I feel this is a problem that should not be too complicated to solve, which is why I post in the beginner's section of the forum.

Problem: Runtime error due to a variable being inserted into the stream.

Details:

During runtime, it just stopped working, so I had to dig this out. The code is as follows:

1
2
typedef unsigned long ID_T;
//in a header... 


1
2
3
4
5
6
7
8
9
10
11
//in another .cpp file with the previous header included

string conv_id_to_str(const ID_T& id)
{
    stringstream ss;
    ss.str("");

    /*Problem here*/
    ss<< id;
    return ss.str();
}


id is read only, and that's what we do. By my understanding, this should work... I would VERY much appreciate any help! Thank you, in advance, for your time.
Here is a small but complete program that runs for me. I cannot produce a runtime error from the code you have provided.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <vector>
#include <string>
#include <sstream>

typedef unsigned long ID_T;
std::string conv_id_to_str(const ID_T& id)
{
    std::stringstream ss;
    ss.str("");

    /*Problem here?*/
    ss<< id;
    return ss.str();
}

int main(int argc, char *argv[])
{
    
    std::cout << conv_id_to_str(159L) << std::endl;

    return 0;
}
Last edited on
In the following snippet:
1
2
    std::stringstream ss;
    ss.str("");

The second line is totally unnecessary. The constructor without arguments creates an empty stream.

It is also possible that this line is causing the problem. I have seen compilers that don't handle zero length string constants correctly.
@jlb

I do not think that is true, as I have used that 'method' before to clear the stream, but I will try it.

EDIT: no difference...
Last edited on
@booradley60

Actually, i did run it in isolation, and it ran fine, but the problem occurs during the execution of the following line:

string journal_path = (f.current_directory() + "\\" + JOURNAL_FOLDER + "\\" + conv_id_to_str(id) + JOURNAL_FILE_EXTENSION);

There must be somthing about it that makes it s**t on itself...

This should not create an error, or at least the way I understand it, but it does. I know it is in the conv_id_to_str() function because I did some testing in it, and it got to putting the id into the stream, but my program did not get to the execution of cout<< "returning string"<< endl; which was placed directly after the ss<< id; line. So, it gets there, but somthing about that line is making it puke on me for some reason... and I can't figure out why.

Thank you for your assistance.

PS. this is thoroughly irritating... (this problem) -,0'
Last edited on
What does f.current_directory() look like?

Have you tried breaking apart that long line into individual steps to see which part is actually causing the problem?

1
2
3
string journal_path = f.current_directory();
journal_path += "\\";
...


By isolating each part you should be able to run your program thru your debugger and see which item is actually causing the problem..

Also if you're using a C11 compiler you may be able to use the to_string() function instead of the stringstream. http://en.cppreference.com/w/cpp/string/basic_string/to_string

f is of type class fsys, a class I wrote to handle quite a few filesystem operations, including quite a few very complex ones. It eliminates my having to use boost in so many of my files, and allows me to many things.

string fsys::current_directory() will return the current directory as a string.

Here is a hypothetical example:

C:\Users\Username\Desktop

That would be the desktop.

Now, I can assure you that the error is not in that class, as I have thoroughly tested it. Also, I have isolated the issue. The issue is with conv_id_to_str(const ID_T& id = def_id), because during my testing, it made it to that function, which means it passed f.current_directory().

I know it is at conv_id_to_str(), and ive even pinpointed the location in the function where the error is occuring.

I will perform further tests tomorrow, as it is beginning to get late where I am.

Tests:

1
2
cout<< "id = "<< id<< endl;
wait() //wait for user input functio  i wrote 


Mabey it has somthing to do with the number being passed...

***********************************
also, interesting, I might try that some other time.
Last edited on
so i did a test and separated everything, and, like previous results showed, its in the conv_str_to_id() function. This was the results:

C:\Users\JonathanWitlock\Desktop\C++\InClass Note Taker
C:\Users\JonathanWitlock\Desktop\C++\InClass Note Taker\Journals\
ID =

I had to separate the cout<< id part into two pieces:

1
2
cout<< "ID = ";
cout<< id<< endl;


It would appear that putting id into a stream of any kind is causeing this problem.
Alright, i have done some further testing and have found these to be true:

- conv_id_to_str() does not work in the loader function at all
- The point at which conv_id_to_str() crashes happenes whenever id is placed into a stream... ANY stream, even cout<< id crashes it.

- I have no fRaKiNg idea what the hell is going on with this thing.... that's making it throw up on me...

**********************************************************************
So.... there's only one last thing I can do: turn the entire thing over to more experienced set of eyes to help me out.

http://pastebin.com/GUZNp5u1

use my collector to make the project folder.

Note: you will need boost + minGW, i use code::blocks
Last edited on
humpty bumbty....

could not be put back together since the last post......

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

pun... get it?! (^0^)
I believe I have found the root of the issue... it stems from that no id is being passed... I do not understand how this is possible.... but I will investigate further.

It has to do with the loader not loading all of the proper variables.
Alrighty. I FIXED IT!!!

The data structure would load a path of a corrupt file (i'm fool-proofing it, as this will be one of my more porffessional pieces). This could result in falsities, as we assume 3 related vectors' slots correlate with one another. If we find a file that does not conatain data, or the right data, it should not load, and should be eliminated from the list of .jrn files it retrieved.

Thank you for your help. The problem was this: no id was actually ever being passed, but, for some reason, it would execute the function and pass a variabel out of thin-air, and get to the part were we use it, which just so happened to be in the conv_id_to_str() function, which works just fine.
Topic archived. No new replies allowed.