473,386 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

cast of ostream& to and from void*

I am trying to cast an ostream reference to void* and back again. The
code below shows the problem isolated from a more complex program. It
compiles quietly but seg faults upon execution.

// ================================================== =================
#include <iostream>
using namespace std;

void myprint(void *s) {
(ostream&)s << "hello, world" << endl;
}

int main(int argc, char *argv[]){
myprint((void *)cerr);
}
// ================================================== =================

Why can't I cast the ostream reference to and from void*?
Feb 12 '06 #1
5 2021
TB
Matt sade:
I am trying to cast an ostream reference to void* and back again. The
code below shows the problem isolated from a more complex program. It
compiles quietly but seg faults upon execution.

// ================================================== =================
#include <iostream>
using namespace std;

void myprint(void *s) {
(ostream&)s << "hello, world" << endl;
}

int main(int argc, char *argv[]){
myprint((void *)cerr);
}
// ================================================== =================

Why can't I cast the ostream reference to and from void*?


You should stop and think if your design requires casting to void*.

#include <iostream>
using namespace std;

void myprint(void *s) {
*((ostream*)s) << "hello, world" << endl;
}

int main(int argc, char *argv[]){
myprint((void *)&cerr);
}

A _much better_ solution is:

void myprint(ostream& s) {
s << "hello, world" << endl;
}

int main(int argc, char *argv[]){
myprint(cerr);
}

--
TB @ SWEDEN
Feb 12 '06 #2

Matt wrote:
I am trying to cast an ostream reference to void* and back again. The
code below shows the problem isolated from a more complex program. It
compiles quietly but seg faults upon execution.

// ================================================== =================
#include <iostream>
using namespace std;

void myprint(void *s) {
(ostream&)s << "hello, world" << endl;
}

int main(int argc, char *argv[]){
myprint((void *)cerr);
}
// ================================================== =================

Why can't I cast the ostream reference to and from void*?


I can't imagine that being a valid cast but I don't know for sure. One
thing that could help you a lot in figuring out what is going on is to
use C++ casts instead of C casts. C casts just do whatever but C++
casts only do the specified type of cast and some things that you
shouldn't be doing just are not possible with them where the C cast
will just silently compile and then explode on runtime.

Feb 12 '06 #3
"Matt" <th**********@xxyyyzzzz.com> wrote in message
news:Id*****************@news01.roc.ny...
I am trying to cast an ostream reference to void* and back again. The code
below shows the problem isolated from a more complex program. It compiles
quietly but seg faults upon execution.

// ================================================== =================
#include <iostream>
using namespace std;

void myprint(void *s) {
(ostream&)s << "hello, world" << endl;
}

int main(int argc, char *argv[]){
myprint((void *)cerr);
}
// ================================================== =================

Why can't I cast the ostream reference to and from void*?


That's because cerr isn't a pointer in the first place. You're taking an
instance, converting it to a pointer, then trying to convert it to a
reference. As soon as you convert it to a pointer you loose everything. So
try taking the pointer to cerr instead.

This works for me (although I wouldn't do this, this just shows you how to
do what you're trying to do)

#include <iostream>
void myprint(void *s)
{
*((std::ostream*) s) << "hello, world" << std::endl;
}

int main(int argc, char *argv[])
{
myprint((void *)&std::cerr);
}
Feb 12 '06 #4
In article <Id*****************@news01.roc.ny>,
Matt <th**********@xxyyyzzzz.com> wrote:
I am trying to cast an ostream reference to void* and back again. The
code below shows the problem isolated from a more complex program. It
compiles quietly but seg faults upon execution.

// ================================================== =================
#include <iostream>
using namespace std;

void myprint(void *s) {
(ostream&)s << "hello, world" << endl;
}

int main(int argc, char *argv[]){
myprint((void *)cerr);
}
// ================================================== =================

Why can't I cast the ostream reference to and from void*?


The problem is that casting a basic_ios to a void* calls the operator
void* member-function who's only guaranteed to return 0 if the stream is
not good. There is no guarantee that it's return value is actually a
pointer to the cerr object.

If you want a pointer to the cerr object, then you have to do as others
have said and take the address of it.

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 12 '06 #5
TB wrote:
Matt sade:
I am trying to cast an ostream reference to void* and back again. The
code below shows the problem isolated from a more complex program. It
compiles quietly but seg faults upon execution.

// ================================================== =================
#include <iostream>
using namespace std;

void myprint(void *s) {
(ostream&)s << "hello, world" << endl;
}

int main(int argc, char *argv[]){
myprint((void *)cerr);
}
// ================================================== =================

Why can't I cast the ostream reference to and from void*?

You should stop and think if your design requires casting to void*.

#include <iostream>
using namespace std;

void myprint(void *s) {
*((ostream*)s) << "hello, world" << endl;
}

int main(int argc, char *argv[]){
myprint((void *)&cerr);
}


Thanks for that fix, TB. That gets me going for now. Yes, I plan to
redesign so as to obviate that usage.

Thanks to all for the useful comments.
Feb 14 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Victor Irzak | last post by:
Hello, I have an ABC. it supports: ostream & operator << I also have a derived class that supports this operator. How can I call operator << of the base class for derived object??? Is it...
0
by: Philippe Mesmeur | last post by:
hello everybody, I would like my code accepts UNICODE streams. I've got a component that writes data in an ostream void MyComponent::write(ostream mystream); Thus, if I want to write in a...
1
by: Tim Partridge | last post by:
I want operator<< to be a friend function of a class inside a namespace, but I don't know how. For example: #include <iostream> namespace ns { class foo { public: // there is something...
16
by: Burne C | last post by:
I have something confused in my mind void *search_address = 0; ++search_address; /* The compiler complain that "void *" is unknown size. OK, I changed the code
17
by: SW1 | last post by:
I wrote a small program which does something like tftp - transfering files and some chat, anyway i got a problem with fwrite, here is a snippet of my code: while(length > 0) { putchar('.');...
4
by: Rock | last post by:
I'm in the process of writing this program for complex numbers and I use DevC++. My professor on the other hand compiles on Borland 5.5. So I ocasionally save and run my work on Borland to see if...
2
by: waitan | last post by:
#include <algorithm> #include <iostream> #include <fstream> #include <string> #include <vector> #include <sstream> #include <iterator> #include <iomanip> using namespace std;
2
by: Henning Hasemann | last post by:
My class looks very exactly like this (left out boring things): class Situation { public: // ... virtual void toStream(std::ostream& ostr) const; }; std::string Situation::toString() const...
4
by: developereo | last post by:
Hi folks, Can anybody shed some light on this problem? class Interface { public: Interface() { ...} virtual ~Interface() { ...} virtual method() = 0; };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.