Connecting Tech Pros Worldwide Help | Site Map

Usage of rdbuf

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 04:45 PM
Omid
Guest
 
Posts: n/a
Default Usage of rdbuf

Hi.

I have a piece of code that can be compiled and linked both with
Cygwin g++ and with VC++. The code is:

//WORKS WITH CYGWIN G++
//BUT NOT WITH CL.EXE (VC++) (compiles, but error when executed)
//################################
#include <iostream>
#include <sstream>
using namespace std;

int main ()
{

int val;
string mystr;
stringstream ss (stringstream::in | stringstream::out);

cout.rdbuf(ss.rdbuf()); //Is this ok to do?
cout << "120 42 377 6 5 2000";

//If the two lines above are replaced with
//this line it works fine with both
//g++ and VC++:
//ss << "120 42 377 6 5 2000";

for (int n=0; n<6; n++)
{
ss >> val;
cerr << val*2 << '\n';
}

cerr << "Hello World! " << endl;
return 0;
}
//################################



My problem is that during run time the exe-file that was created with
Cygwin g++ (3.3.1) works fine, but the exe-file that was created with
VC++ 6.0 crashes.

Here is the error message:
The instruction at "0x00402f7f" referenced memory at "0x0000000sd".
The memory could not be "read".


Is there any errors in my code, or do I do something I'm not allowed
to?
Since it works in Cygwin g++, but not in VC++ 6.0 I think this is
really strange, since I believe that my code is 100% ANSI-C++ code. Or
is it?

I compile with:
G++: g++ test.cpp
VC++: cl /GX /TP test.cpp (also compiled with the entire VC++
development GUI)

I'm thankful for any information that might help me with this problem.

/Omid

  #2  
Old July 22nd, 2005, 04:45 PM
John Harrison
Guest
 
Posts: n/a
Default Re: Usage of rdbuf


"Omid" <n_o_sp_a_m@hotmail.com> wrote in message
news:77537485.0407220025.295ab908@posting.google.c om...[color=blue]
> Hi.
>
> I have a piece of code that can be compiled and linked both with
> Cygwin g++ and with VC++. The code is:
>
> //WORKS WITH CYGWIN G++
> //BUT NOT WITH CL.EXE (VC++) (compiles, but error when executed)
> //################################
> #include <iostream>
> #include <sstream>
> using namespace std;
>
> int main ()
> {
>
> int val;
> string mystr;
> stringstream ss (stringstream::in | stringstream::out);
>
> cout.rdbuf(ss.rdbuf()); //Is this ok to do?
> cout << "120 42 377 6 5 2000";
>
> //If the two lines above are replaced with
> //this line it works fine with both
> //g++ and VC++:
> //ss << "120 42 377 6 5 2000";
>
> for (int n=0; n<6; n++)
> {
> ss >> val;
> cerr << val*2 << '\n';
> }
>
> cerr << "Hello World! " << endl;
> return 0;
> }
> //################################
>
>
>
> My problem is that during run time the exe-file that was created with
> Cygwin g++ (3.3.1) works fine, but the exe-file that was created with
> VC++ 6.0 crashes.
>
> Here is the error message:
> The instruction at "0x00402f7f" referenced memory at "0x0000000sd".
> The memory could not be "read".
>
>
> Is there any errors in my code, or do I do something I'm not allowed
> to?
> Since it works in Cygwin g++, but not in VC++ 6.0 I think this is
> really strange, since I believe that my code is 100% ANSI-C++ code. Or
> is it?
>
> I compile with:
> G++: g++ test.cpp
> VC++: cl /GX /TP test.cpp (also compiled with the entire VC++
> development GUI)
>
> I'm thankful for any information that might help me with this problem.
>
> /Omid[/color]

You must restore the old buffer before you exit.

streambuf* save_buffer = cout.rdbuf(ss.rdbuf()); //Is this ok to do?
cout << "120 42 377 6 5 2000";
cout.rdbuf(save_buffer);

john



  #3  
Old July 22nd, 2005, 04:50 PM
DM McGowan II
Guest
 
Posts: n/a
Default Re: Usage of rdbuf

"Omid" <n_o_sp_a_m@hotmail.com> wrote in message
news:77537485.0407220025.295ab908@posting.google.c om...[color=blue]
> Hi.
>
> I have a piece of code that can be compiled and linked both with
> Cygwin g++ and with VC++. The code is:
>
> //WORKS WITH CYGWIN G++
> //BUT NOT WITH CL.EXE (VC++) (compiles, but error when executed)
> //################################
> #include <iostream>
> #include <sstream>
> using namespace std;
>
> int main ()
> {
>
> int val;
> string mystr;
> stringstream ss (stringstream::in | stringstream::out);
>
> cout.rdbuf(ss.rdbuf()); //Is this ok to do?
> cout << "120 42 377 6 5 2000";
>
> //If the two lines above are replaced with
> //this line it works fine with both
> //g++ and VC++:
> //ss << "120 42 377 6 5 2000";
>
> for (int n=0; n<6; n++)
> {
> ss >> val;
> cerr << val*2 << '\n';
> }
>
> cerr << "Hello World! " << endl;
> return 0;
> }
> //################################
>
>
>
> My problem is that during run time the exe-file that was created with
> Cygwin g++ (3.3.1) works fine, but the exe-file that was created with
> VC++ 6.0 crashes.
>
> Here is the error message:
> The instruction at "0x00402f7f" referenced memory at "0x0000000sd".
> The memory could not be "read".
>
>
> Is there any errors in my code, or do I do something I'm not allowed
> to?
> Since it works in Cygwin g++, but not in VC++ 6.0 I think this is
> really strange, since I believe that my code is 100% ANSI-C++ code. Or
> is it?
>
> I compile with:
> G++: g++ test.cpp
> VC++: cl /GX /TP test.cpp (also compiled with the entire VC++
> development GUI)
>
> I'm thankful for any information that might help me with this problem.
>
> /Omid[/color]

I just tried with VS.NET and it compiles and runs cleanly. You can download
the compiler for free http://msdn.microsoft.com/visualc/vctoolkit2003/.

  #4  
Old July 22nd, 2005, 04:52 PM
Omid
Guest
 
Posts: n/a
Default Re: Usage of rdbuf

"John Harrison" <john_andronicus@hotmail.com> wrote in message news:<2m9cv5Fk0kjtU1@uni-berlin.de>...[color=blue]
> "Omid" <n_o_sp_a_m@hotmail.com> wrote in message
> news:77537485.0407220025.295ab908@posting.google.c om...[color=green]
> > Hi.
> >
> > I have a piece of code that can be compiled and linked both with
> > Cygwin g++ and with VC++. The code is:
> >
> > //WORKS WITH CYGWIN G++
> > //BUT NOT WITH CL.EXE (VC++) (compiles, but error when executed)
> > //################################
> > #include <iostream>
> > #include <sstream>
> > using namespace std;
> >
> > int main ()
> > {
> >
> > int val;
> > string mystr;
> > stringstream ss (stringstream::in | stringstream::out);
> >
> > cout.rdbuf(ss.rdbuf()); //Is this ok to do?
> > cout << "120 42 377 6 5 2000";
> >
> > //If the two lines above are replaced with
> > //this line it works fine with both
> > //g++ and VC++:
> > //ss << "120 42 377 6 5 2000";
> >
> > for (int n=0; n<6; n++)
> > {
> > ss >> val;
> > cerr << val*2 << '\n';
> > }
> >
> > cerr << "Hello World! " << endl;
> > return 0;
> > }
> > //################################
> >
> >
> >
> > My problem is that during run time the exe-file that was created with
> > Cygwin g++ (3.3.1) works fine, but the exe-file that was created with
> > VC++ 6.0 crashes.
> >
> > Here is the error message:
> > The instruction at "0x00402f7f" referenced memory at "0x0000000sd".
> > The memory could not be "read".
> >
> >
> > Is there any errors in my code, or do I do something I'm not allowed
> > to?
> > Since it works in Cygwin g++, but not in VC++ 6.0 I think this is
> > really strange, since I believe that my code is 100% ANSI-C++ code. Or
> > is it?
> >
> > I compile with:
> > G++: g++ test.cpp
> > VC++: cl /GX /TP test.cpp (also compiled with the entire VC++
> > development GUI)
> >
> > I'm thankful for any information that might help me with this problem.
> >
> > /Omid[/color]
>
> You must restore the old buffer before you exit.
>
> streambuf* save_buffer = cout.rdbuf(ss.rdbuf()); //Is this ok to do?
> cout << "120 42 377 6 5 2000";
> cout.rdbuf(save_buffer);
>
> john[/color]

Thanks John!

That solved my problem.


/Omid
 

Bookmarks

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.