473,569 Members | 2,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2033
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**********@x xyyyzzzz.com> wrote in message
news:Id******** *********@news0 1.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.ro c.ny>,
Matt <th**********@x xyyyzzzz.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
8253
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 at all possible?
0
1733
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 file, I give my component and instance of an ofstream. This ofstream opens a file with its method "open", whose
1
2547
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 wrong with the line below, but I don't know how to fix it. I think I need to
16
17253
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
6107
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('.'); //These were for error checking if(length <= bsize) { //Buffer is bigger than remaining File realloc(buffer,length * sizeof(char)); //resize buffer to...
4
6740
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 it caught anything, it's very picky... Anyway, the code below works on Dev, and it compiles fine on Borland, but when I run it from borland, there...
2
2942
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
2728
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 { std::ostringstream oss;
4
2171
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
7609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7921
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6278
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2107
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.