Stringstream data type causing problems

This is a discussion on Stringstream data type causing problems within the C++ Programming forums, part of the General Programming Boards category; I have a C++ shared library that I am calling from an OpenEdge application. Up until today, everything was fine. ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    6

    Stringstream data type causing problems

    I have a C++ shared library that I am calling from an OpenEdge application. Up until today, everything was fine. Over the past few days I have been making changes to my eclipse development environment, but no code changes.

    Today, I compiled my library and called it from OpenEdge and got a run-time memory violation. After a good deal of digging, I found that the problem is that any declaration to the stringstream datatype was causing the problem. If I take all references to stringstream ouf my code, it works fine.

    To test a little further, I created a brand new project and made a smiple function

    Code:
    int msg_send()
    {
        stringstream test;
        return 1;
    }


    And the compiler is giving me an error
    /stringstream.cpp: In function ‘int msg_send()’:
    ../stringstream.cpp:13: error: ‘test’ was not declared in this scope
    ../stringstream.cpp:13: error: expected type-specifier before ‘stringstream’
    ../stringstream.cpp:13: error: expected `;' before ‘stringstream’"
    .... I have to assume that something is screwed up with the compiler, but I don't know what.
    Last edited by Boomn4x4; 01-13-2012 at 09:42 AM.

  2. #2
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    That's quite an assumption you're making.

    To me it looks like `stringstream` is not defined as type, and you've messed up your includes, where that is done.
    Last edited by msh; 01-13-2012 at 10:22 AM. Reason: (Can't spell when drunk)
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    6
    Quote Originally Posted by msh View Post
    That's quite an assumption your making.
    Yes, it was... and a bad one at that. After giving eclipse a few seconds, it picked up the stringstream and it worked fine.

    However, my problem still exists.

    If I take every reference to stringstream out of my library, it works. As soon as I add in just a simple declaration, I get the memory violation.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    Did you:
    • #include <sstream> ?
    • Fully qualify as std::stringstream, or use using std::stringstream, or use using namespace std ?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    6
    Quote Originally Posted by laserlight View Post
    Did you:
    • #include <sstream> ?
    • or use using namespace std ?
    Yes, I have.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    Okay, then compile this program:
    Code:
    #include <sstream>
    
    int main()
    {
        using namespace std;
        stringstream test;
        return 0;
    }
    What errors do you get?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    6
    The errors don't happen on compile. They are run time.

    My library compiles without an issue. However, when I call the library functions, I get run time errors with Memory Violations.

    The memory violations occur when it gets to the part in the code that the stringstream is declared. If i put a print statement immediatly before the declaration, the statement will display to the screen, then the memory violation will happen.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    Quote Originally Posted by Boomn4x4
    The errors don't happen on compile. They are run time.
    Right, so why did you report compile errors in post #1 too?

    Without any more evidence, I daresay that you have a bug in your code, possibly one that results in undefined behaviour, e.g., you access an array out of bounds. This bug then shows up when you use a stringstream.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jan 2012
    Posts
    6
    Quote Originally Posted by laserlight View Post
    Without any more evidence, I daresay that you have a bug in your code, possibly one that results in undefined behaviour, e.g., you access an array out of bounds. This bug then shows up when you use a stringstream.

    I've stripped the code down to almost nothing.... here is the entire project.

    header file
    Code:
    #include <inttypes.h>
    #include <time.h>
    #include <stdlib.h>
    #include <string.h>
    #include <iostream>
    #include <sstream>
    #include <fstream>
    #include <sys/stat.h>   //file statistics
    
    using namespace std;
    
    
    enum MiStatus{
        MIS_OK
        , MIS_CFG_FAILED
        , MIS_CONNECTION_FAILED
        , MIS_OPEN_FAILED
        , MIS_SEND_FAILED
        , MIS_INVALID_ARGS
    
    };
    
    
    extern "C"{
    // send message to default queue manager
    MiStatus msg_send_q(
            const char *destination,
            const char *message,
            uint32_t   messageLength,
            uint32_t   persistence);
    }


    This code runs sucessfuly

    Code:
    #include <unistd.h>
    #include <string.h>
    #include <sstream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <cmqc.h>
    #include <cmqxc.h>
    #include "swmq.h"
    #include <inttypes.h>
    
    
    
    MiStatus msg_send_q(
            const char *destination,
            const char *message,
            uint32_t messageLength,
            uint32_t persistence)
    {
        string destType = "QUEUE";
        MiStatus rv;
    
    
    
        return rv;
    
    
    }
    This code fails

    Code:
    #include <unistd.h>
    #include <string.h>
    #include <sstream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <cmqc.h>
    #include <cmqxc.h>
    #include "swmq.h"
    #include <inttypes.h>
    
    
    
    MiStatus msg_send_q(
            const char *destination,
            const char *message,
            uint32_t messageLength,
            uint32_t persistence)
    {
        string destType = "QUEUE";
        MiStatus rv;
        stringstream test;
    
    
        return rv;
    
    
    }

  10. #10
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    In what sense does it fail?
    What does it do if you remove the extern "C"?

  11. #11
    Registered User
    Join Date
    Jan 2012
    Posts
    6
    Quote Originally Posted by oogabooga View Post
    In what sense does it fail?
    What does it do if you remove the extern "C"?
    The OpenEdge application that calls It core dumps with a memory violation.

    OpenEdge only recognizes C external libraries, if i don't extern the functions and compile them as C programs, OpenEdge won't load the libraries because it cannot find an entry point.

  12. #12
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    But then aren't you trying to use C++ inside what is supposed to be a pure C function?

  13. #13
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    >> But then aren't you trying to use C++ inside what is supposed to be a pure C function?
    No. 'extern "C"' just specifies the language linkage of the function. The function can still be written in C++.

    How is your library compiled? Shared object or static lib? Show the full command line for compiling/linking.

    What part of "OpenEdge" is calling your functions? Where are the manuals for that?

    gg

  14. #14
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Codeplug View Post
    No. 'extern "C"' just specifies the language linkage of the function. The function can still be written in C++.
    Oh, I see. So it just specifies how parameters are passed and return values returned. Thanks codeplug.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data type problems, float, int, etc with averages
    By Jomoka in forum C Programming
    Replies: 4
    Last Post: 06-18-2011, 08:18 AM
  2. Replies: 2
    Last Post: 05-14-2011, 09:26 PM
  3. MinGW causing problems with sprintf()
    By sedavidw in forum C Programming
    Replies: 6
    Last Post: 01-14-2010, 04:37 AM
  4. sscanf causing problems
    By takamineg240 in forum C++ Programming
    Replies: 10
    Last Post: 09-26-2006, 04:07 AM
  5. Class member using a global variable, causing problems.
    By RealityFusion in forum C++ Programming
    Replies: 1
    Last Post: 09-11-2005, 11:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21