Diffrerent output of the same program on HP aCC and Linux (Intel/g++)

Discussion in 'C++' started by Alex Vinokur, Dec 19, 2012.

  1. Alex Vinokur

    Alex Vinokur Guest

    Hi,

    While migratining from deprecated std::strstream to std::stringstream we are faced with some problem related to std::stringstream.
    The same program produces different output on HP aCC and on Intel/g++ compilers on Linux.

    Here is some fragment of different outputs.

    HP aCC
    Compilation:
    [ Line-135, doStringStream] stream.str() = <xyz>
    [ Line-136, doStringStream] stream.str().c_str() = <xyz>
    [ Line-137, doStringStream] stream.str().data() = <xyz>
    [ Line-139, doStringStream] stream.tellp() = <3>
    [ Line-140, doStringStream] stream.str().size() = <3>
    [ Line-141, doStringStream] stream.rdbuf()->str().size() = <3>
    [ Line-142, doStringStream] stream.rdbuf()->in_avail() = <3>


    Linux g++ compiler
    Compilation:

    [ Line-135, doStringStream] stream.str() = <xyzdefQQQQQQQQQQQQQQQQQQQ>
    [ Line-136, doStringStream] stream.str().c_str() = <xyzdefQQQQQQQQQQQQQQQQQQQ>
    [ Line-137, doStringStream] stream.str().data() = <xyzdefQQQQQQQQQQQQQQQQQQQ>
    [ Line-139, doStringStream] stream.tellp() = <3>
    [ Line-140, doStringStream] stream.str().size() = <25>
    [ Line-141, doStringStream] stream.rdbuf()->str().size() = <25>
    [ Line-142, doStringStream] stream.rdbuf()->in_avail() = <25>



    The program and logs below.

    Any suggestions?

    Thanks.

    Alex

    // ========== File aaa21.cpp: BEGIN ==========
    #include <cstring>
    #include <cstdlib>
    #include <climits>
    #include <iostream>
    #include <string>
    #include <strstream> // deprecated
    #include <sstream>




    // ========================
    #define TRACE std::cout << "\nLine-" << __LINE__ << ": --- " << __FUNCTION__ << " ---" << std::endl
    #define START std::cout << "\nLine-" << __LINE__ << ": >>> START " << __FUNCTION__ << " <<<" << std::endl
    #define FINISH std::cout << "Line-" << __LINE__ << ": <<< FINISH " << __FUNCTION__ << " >>>" << std::endl << std::endl
    #define SHOW(x) std::cout << "[ Line-" << __LINE__ << ", " << __FUNCTION__ << "] " << #x << " = <" << x << ">" << std::endl

    // ---------------------------
    void showCompilerInfo(std::eek:stream& o_stream)
    {
    o_stream << "Compiler: ";
    #if (defined __hpux && defined __HP_aCC)
    o_stream << "HP C/aC++ "
    << __HP_aCC;

    #elif (defined __INTEL_COMPILER)
    o_stream << "Intel(R) C++ Intel(R) 64 Compiler, Version "
    << __INTEL_COMPILER;

    #elif (defined __GNUC__ && !defined __INTEL_COMPILER)
    o_stream << "GNU g++, Version "
    << __GNUC__;
    #ifdef __GNUC_MINOR__
    o_stream << "." << __GNUC_MINOR__;

    #ifdef __GNUC_PATCHLEVEL__
    #if __GNUC_PATCHLEVEL__
    o_stream << "." << __GNUC_PATCHLEVEL__;

    #endif // #ifdef __GNUC_PATCHLEVEL__
    #endif // #if __GNUC_PATCHLEVEL__
    #endif // #ifdef __GNUC_MINOR__


    #else
    #error Unknown compiler
    #endif

    o_stream << std::endl << std::flush;
    }

    // -------------
    char* pStrStream = 0;
    char* pStringStream = 0;

    // -------------
    static void putToStrStream(std::eek:stream& i_stream, const char* i_pText)
    {
    START;

    SHOW(pStrStream);
    SHOW(strlen(pStrStream));
    SHOW(i_pText);
    SHOW(strlen(i_pText));

    i_stream << i_pText;

    SHOW(pStrStream);
    SHOW(strlen(pStrStream));

    FINISH;

    }

    // -------------
    static void putToStringStream(std::eek:stream& i_stream, const char* i_pText)
    {
    START;

    SHOW(pStringStream);
    SHOW(strlen(pStringStream));
    SHOW(i_pText);
    SHOW(strlen(i_pText));

    // i_stream.clear();
    // i_stream.seekp(0, std::ios::beg);
    i_stream << i_pText;
    // i_stream << std::ends;

    SHOW(pStringStream);
    SHOW(strlen(pStringStream));

    FINISH;
    }

    // ----------------
    void doStrStream(const char* i_pText, std::size_t i_size)
    {

    START;

    SHOW(i_size);

    std::strstream stream(pStrStream, i_size);
    SHOW(stream.rdbuf()->pcount());
    SHOW(stream.str());

    putToStrStream(stream, i_pText);
    SHOW(stream.rdbuf()->pcount());

    FINISH;
    }

    // ----------------
    void doStringStream(const char* i_pText, std::size_t i_size)
    {

    START;

    SHOW(i_size);

    std::stringstream stream;
    stream.rdbuf()->pubsetbuf(pStringStream, i_size);
    // stream.rdbuf()->str("");

    SHOW(stream.tellp());
    SHOW(stream.str().size());
    SHOW(stream.rdbuf()->str().size());
    SHOW(stream.rdbuf()->in_avail());

    SHOW(stream.str());
    SHOW(stream.str().c_str());
    SHOW(stream.str().data());
    putToStringStream(stream, i_pText);
    SHOW(stream.str());
    SHOW(stream.str().c_str());
    SHOW(stream.str().data());

    SHOW(stream.tellp());
    SHOW(stream.str().size());
    SHOW(stream.rdbuf()->str().size());
    SHOW(stream.rdbuf()->in_avail());

    FINISH;
    }



    // ------------
    int main()
    {

    system ("uname -a");
    showCompilerInfo(std::cout);
    std::cout << "Data Model: " << (sizeof(void*) * CHAR_BIT) << " bits" << std::endl;

    pStrStream = new char [100];
    pStringStream = new char [100];

    memset (pStrStream, 'P', 100);
    pStrStream[70] = 0;
    memset (pStringStream, 'Q', 100);
    pStringStream[90] = 0;


    TRACE;
    doStrStream("ABCDEF", 45);
    doStrStream("XYZ", 25);


    TRACE;
    doStringStream("abcdef", 45);
    doStringStream("xyz", 25);


    // ------------------
    delete[] pStrStream;
    delete[] pStringStream;

    return 0;
    }
    // ========== File aaa21.cpp: END ==========



    // ========== Log HP aCC: BEGIN ==========

    HP-UX hpx418 B.11.23 U ia64 1139467043 unlimited-user license
    Compiler: HP C/aC++ 62501
    Data Model: 64 bits

    Line-166: --- main ---

    Line-100: >>> START doStrStream <<<
    [ Line-102, doStrStream] i_size = <45>
    [ Line-105, doStrStream] stream.rdbuf()->pcount() = <0>
    [ Line-106, doStrStream] stream.str() = <PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP>

    Line-59: >>> START putToStrStream <<<
    [ Line-61, putToStrStream] pStrStream = <PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP>
    [ Line-62, putToStrStream] strlen(pStrStream) = <70>
    [ Line-63, putToStrStream] i_pText = <ABCDEF>
    [ Line-64, putToStrStream] strlen(i_pText) = <6>
    [ Line-68, putToStrStream] pStrStream = <ABCDEFPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP>
    [ Line-69, putToStrStream] strlen(pStrStream) = <70>
    Line-71: <<< FINISH putToStrStream >>>

    [ Line-109, doStrStream] stream.rdbuf()->pcount() = <6>
    Line-111: <<< FINISH doStrStream >>>


    Line-100: >>> START doStrStream <<<
    [ Line-102, doStrStream] i_size = <25>
    [ Line-105, doStrStream] stream.rdbuf()->pcount() = <0>
    [ Line-106, doStrStream] stream.str() = <ABCDEFPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP>

    Line-59: >>> START putToStrStream <<<
    [ Line-61, putToStrStream] pStrStream = <ABCDEFPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP>
    [ Line-62, putToStrStream] strlen(pStrStream) = <70>
    [ Line-63, putToStrStream] i_pText = <XYZ>
    [ Line-64, putToStrStream] strlen(i_pText) = <3>
    [ Line-68, putToStrStream] pStrStream = <XYZDEFPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP>
    [ Line-69, putToStrStream] strlen(pStrStream) = <70>
    Line-71: <<< FINISH putToStrStream >>>

    [ Line-109, doStrStream] stream.rdbuf()->pcount() = <3>
    Line-111: <<< FINISH doStrStream >>>


    Line-171: --- main ---

    Line-118: >>> START doStringStream <<<
    [ Line-120, doStringStream] i_size = <45>
    [ Line-126, doStringStream] stream.tellp() = <0>
    [ Line-127, doStringStream] stream.str().size() = <0>
    [ Line-128, doStringStream] stream.rdbuf()->str().size() = <0>
    [ Line-129, doStringStream] stream.rdbuf()->in_avail() = <0>
    [ Line-131, doStringStream] stream.str() = <>
    [ Line-132, doStringStream] stream.str().c_str() = <>
    [ Line-133, doStringStream] stream.str().data() = <>

    Line-78: >>> START putToStringStream <<<
    [ Line-80, putToStringStream] pStringStream = <QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ>
    [ Line-81, putToStringStream] strlen(pStringStream) = <90>
    [ Line-82, putToStringStream] i_pText = <abcdef>
    [ Line-83, putToStringStream] strlen(i_pText) = <6>
    [ Line-90, putToStringStream] pStringStream = <abcdefQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ>
    [ Line-91, putToStringStream] strlen(pStringStream) = <90>
    Line-93: <<< FINISH putToStringStream >>>

    [ Line-135, doStringStream] stream.str() = <abcdef>
    [ Line-136, doStringStream] stream.str().c_str() = <abcdef>
    [ Line-137, doStringStream] stream.str().data() = <abcdef>
    [ Line-139, doStringStream] stream.tellp() = <6>
    [ Line-140, doStringStream] stream.str().size() = <6>
    [ Line-141, doStringStream] stream.rdbuf()->str().size() = <6>
    [ Line-142, doStringStream] stream.rdbuf()->in_avail() = <6>
    Line-144: <<< FINISH doStringStream >>>


    Line-118: >>> START doStringStream <<<
    [ Line-120, doStringStream] i_size = <25>
    [ Line-126, doStringStream] stream.tellp() = <0>
    [ Line-127, doStringStream] stream.str().size() = <0>
    [ Line-128, doStringStream] stream.rdbuf()->str().size() = <0>
    [ Line-129, doStringStream] stream.rdbuf()->in_avail() = <0>
    [ Line-131, doStringStream] stream.str() = <>
    [ Line-132, doStringStream] stream.str().c_str() = <>
    [ Line-133, doStringStream] stream.str().data() = <>

    Line-78: >>> START putToStringStream <<<
    [ Line-80, putToStringStream] pStringStream = <abcdefQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ>
    [ Line-81, putToStringStream] strlen(pStringStream) = <90>
    [ Line-82, putToStringStream] i_pText = <xyz>
    [ Line-83, putToStringStream] strlen(i_pText) = <3>
    [ Line-90, putToStringStream] pStringStream = <xyzdefQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ>
    [ Line-91, putToStringStream] strlen(pStringStream) = <90>
    Line-93: <<< FINISH putToStringStream >>>

    [ Line-135, doStringStream] stream.str() = <xyz>
    [ Line-136, doStringStream] stream.str().c_str() = <xyz>
    [ Line-137, doStringStream] stream.str().data() = <xyz>
    [ Line-139, doStringStream] stream.tellp() = <3>
    [ Line-140, doStringStream] stream.str().size() = <3>
    [ Line-141, doStringStream] stream.rdbuf()->str().size() = <3>
    [ Line-142, doStringStream] stream.rdbuf()->in_avail() = <3>
    Line-144: <<< FINISH doStringStream >>>

    // ========== Log HP aCC: END ==========



    // ========== Log Linux g++: BEGIN ==========

    Linux illin793 2.6.32-279.5.2.el6.x86_64 #1 SMP Tue Aug 14 11:36:39 EDT 2012 x86_64 x86_64 x86_64 GNU/Linux
    Compiler: GNU g++, Version 4.4.6
    Data Model: 64 bits

    Line-166: --- main ---

    Line-100: >>> START doStrStream <<<
    [ Line-102, doStrStream] i_size = <45>
    [ Line-105, doStrStream] stream.rdbuf()->pcount() = <0>
    [ Line-106, doStrStream] stream.str() = <PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP>

    Line-59: >>> START putToStrStream <<<
    [ Line-61, putToStrStream] pStrStream = <PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP>
    [ Line-62, putToStrStream] strlen(pStrStream) = <70>
    [ Line-63, putToStrStream] i_pText = <ABCDEF>
    [ Line-64, putToStrStream] strlen(i_pText) = <6>
    [ Line-68, putToStrStream] pStrStream = <ABCDEFPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP>
    [ Line-69, putToStrStream] strlen(pStrStream) = <70>
    Line-71: <<< FINISH putToStrStream >>>

    [ Line-109, doStrStream] stream.rdbuf()->pcount() = <6>
    Line-111: <<< FINISH doStrStream >>>


    Line-100: >>> START doStrStream <<<
    [ Line-102, doStrStream] i_size = <25>
    [ Line-105, doStrStream] stream.rdbuf()->pcount() = <0>
    [ Line-106, doStrStream] stream.str() = <ABCDEFPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP>

    Line-59: >>> START putToStrStream <<<
    [ Line-61, putToStrStream] pStrStream = <ABCDEFPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP>
    [ Line-62, putToStrStream] strlen(pStrStream) = <70>
    [ Line-63, putToStrStream] i_pText = <XYZ>
    [ Line-64, putToStrStream] strlen(i_pText) = <3>
    [ Line-68, putToStrStream] pStrStream = <XYZDEFPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP>
    [ Line-69, putToStrStream] strlen(pStrStream) = <70>
    Line-71: <<< FINISH putToStrStream >>>

    [ Line-109, doStrStream] stream.rdbuf()->pcount() = <3>
    Line-111: <<< FINISH doStrStream >>>


    Line-171: --- main ---

    Line-118: >>> START doStringStream <<<
    [ Line-120, doStringStream] i_size = <45>
    [ Line-126, doStringStream] stream.tellp() = <0>
    [ Line-127, doStringStream] stream.str().size() = <45>
    [ Line-128, doStringStream] stream.rdbuf()->str().size() = <45>
    [ Line-129, doStringStream] stream.rdbuf()->in_avail() = <45>
    [ Line-131, doStringStream] stream.str() = <QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ>
    [ Line-132, doStringStream] stream.str().c_str() = <QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ>
    [ Line-133, doStringStream] stream.str().data() = <QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ>

    Line-78: >>> START putToStringStream <<<
    [ Line-80, putToStringStream] pStringStream = <QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ>
    [ Line-81, putToStringStream] strlen(pStringStream) = <90>
    [ Line-82, putToStringStream] i_pText = <abcdef>
    [ Line-83, putToStringStream] strlen(i_pText) = <6>
    [ Line-90, putToStringStream] pStringStream = <abcdefQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ>
    [ Line-91, putToStringStream] strlen(pStringStream) = <90>
    Line-93: <<< FINISH putToStringStream >>>

    [ Line-135, doStringStream] stream.str() = <abcdefQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ>
    [ Line-136, doStringStream] stream.str().c_str() = <abcdefQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ>
    [ Line-137, doStringStream] stream.str().data() = <abcdefQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ>
    [ Line-139, doStringStream] stream.tellp() = <6>
    [ Line-140, doStringStream] stream.str().size() = <45>
    [ Line-141, doStringStream] stream.rdbuf()->str().size() = <45>
    [ Line-142, doStringStream] stream.rdbuf()->in_avail() = <45>
    Line-144: <<< FINISH doStringStream >>>


    Line-118: >>> START doStringStream <<<
    [ Line-120, doStringStream] i_size = <25>
    [ Line-126, doStringStream] stream.tellp() = <0>
    [ Line-127, doStringStream] stream.str().size() = <25>
    [ Line-128, doStringStream] stream.rdbuf()->str().size() = <25>
    [ Line-129, doStringStream] stream.rdbuf()->in_avail() = <25>
    [ Line-131, doStringStream] stream.str() = <abcdefQQQQQQQQQQQQQQQQQQQ>
    [ Line-132, doStringStream] stream.str().c_str() = <abcdefQQQQQQQQQQQQQQQQQQQ>
    [ Line-133, doStringStream] stream.str().data() = <abcdefQQQQQQQQQQQQQQQQQQQ>

    Line-78: >>> START putToStringStream <<<
    [ Line-80, putToStringStream] pStringStream = <abcdefQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ>
    [ Line-81, putToStringStream] strlen(pStringStream) = <90>
    [ Line-82, putToStringStream] i_pText = <xyz>
    [ Line-83, putToStringStream] strlen(i_pText) = <3>
    [ Line-90, putToStringStream] pStringStream = <xyzdefQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ>
    [ Line-91, putToStringStream] strlen(pStringStream) = <90>
    Line-93: <<< FINISH putToStringStream >>>

    [ Line-135, doStringStream] stream.str() = <xyzdefQQQQQQQQQQQQQQQQQQQ>
    [ Line-136, doStringStream] stream.str().c_str() = <xyzdefQQQQQQQQQQQQQQQQQQQ>
    [ Line-137, doStringStream] stream.str().data() = <xyzdefQQQQQQQQQQQQQQQQQQQ>
    [ Line-139, doStringStream] stream.tellp() = <3>
    [ Line-140, doStringStream] stream.str().size() = <25>
    [ Line-141, doStringStream] stream.rdbuf()->str().size() = <25>
    [ Line-142, doStringStream] stream.rdbuf()->in_avail() = <25>
    Line-144: <<< FINISH doStringStream >>>

    // ========== Log Linux g++: END ==========
     
    Alex Vinokur, Dec 19, 2012
    #1
    1. Advertisements

  2. In the program you use
    to try to change the underlying buffer used by the stringstream.
    According to the C++03 standard, pubsetbuf(s, n) calls setbuf(s, n).
    The effects of calling setbuf of a stringbuf are implementation defined,
    so each compiler can choose whatever behaviour they think is sane (as
    long as it is consistent and documented).
    You will have to check the compiler documentation, but from the obtained
    results it seems that GCC regards the original contents of the buffer to
    be present in the stream (ready for being read), while HP aCC regards the
    buffer as being empty.
    Both are equally valid.
    Bart v Ingen Schenau
     
    Bart van Ingen Schenau, Dec 19, 2012
    #2
    1. Advertisements

  3. Alex Vinokur

    Alex Vinokur Guest

    "Bart van Ingen Schenau" wrote in message

    [snip]
    [snip]

    Thank you.
     
    Alex Vinokur, Dec 19, 2012
    #3
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.