please explain this runtime error if you can?

closed account (iw0XoG1T)
I wrote a program that had these two functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
std::string line_format_for_total ( const std::string title,
                                    const long long val )
{
    std::stringstream ss;
    std::string rt ;
    ss << title ;
    ss << std::setw(FIELD_WIDTH_SZ) ;
    ss << format_with_commas ( val / 100.0 , 2 );
    ss.flush () ;

    rt = ss.str();
//  return ss.str(); <---this is how it was originally written.
    return rt;
}

std::string line_format_for_col   ( const std::string  title1,
                                    const std::string  title2 )
{
    std::stringstream ss;
    std::string rt ;
    ss << title1 ;
    ss << center_on(title2, FIELD_WIDTH_SZ) ;
    ss.flush () ;

    rt = ss.str();
//  return ss.str(); <---this is how it was originally written.
    return rt;

}


These functions work, but when written the original way they would work if one was called but not if they were both called.

I spent at least five hours trying to figure out what was wrong; because why it broke made no sense to me. If I call only one of the functions in the program or tested them separately there was no problem.
closed account (iw0XoG1T)
@ness555 I'll try to explain with more detail.
chwsks wrote:
These functions work, but when written the original way they would work if one was called but not if they were both called.


* If a comment-out either function call the program will run without error.

*But when I try to call them both the program does not run.

* If you look at the code I wrote you will see I placed a comment that shows how they were originally written. It is a runtime error the program compiles without error, but when written the original way it is stopped by the operating system with a message that says the program has been stopped.

* More confusing is the fact that when I run this same program at home I can run it either way and they both work. I did recompile the program so it is not the same program--but both were compiled with the same compiler (gcc-4.7.0).

* I would also appreciate a little more detailed criticism because I really do not understand why you feel that this question was not asked in the smart way.
You are incurring in undefined behaviour.
Those functions are fine, the problem is somewhere else. format_with_commas() center_on() or the caller code
closed account (iw0XoG1T)
problem is somewhere else. format_with_commas() center_on()


I did not write the format_with_commas I copied from somewhere--I actually do not
understand why it works. If it is the problem I can rewrite that function more simply.

I probably should not use code I don't personally understand.

I posted the code below in hope that maybe someone else can spot the problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
std::string center_on (  std::string in, unsigned col_sz)
{
    unsigned left_pad = ( col_sz - in.size () ) / 2 ;
    unsigned right_pad =  col_sz - in.size () - left_pad; 
    std::string lp; 

    for( unsigned i = 0 ; i != left_pad ; ++ i)
        lp += ' ' ;
    
    lp += in ;

    for( unsigned i = 0 ; i != right_pad ; ++ i)
        lp += ' ' ;
    
    return lp ; 

class comma_numpunct : public std::numpunct<char>
{
protected:
    virtual char do_thousands_sep () const
    {
        return ',' ;
    }

    virtual std::string do_grouping () const
    {
        return "\03" ;
    }
};

template<class T>
std::string format_with_commas (T value, unsigned precision)
{
    std::locale comma_locale (std::locale () ,
                              new comma_numpunct ()) ;
    std::stringstream ss ;
    ss.imbue (comma_locale) ;
    ss << std::setprecision (precision)
       << std::fixed << value ;
    return ss.str () ;
}
}
Last edited on
Topic archived. No new replies allowed.