By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 839 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Why does the write method take a character pointer to the object one wishes to write?

P: n/a
Would it not be more efficient to take in a void pointer?

int number = 30;
fout.write((char *)(&number), sizeof(number));

Dec 10 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 10 Dec 2005 10:40:14 -0800, "AMT2K5" <Aa*********@gmail.com> wrote:
Would it not be more efficient to take in a void pointer?

int number = 30;
fout.write((char *)(&number), sizeof(number));


No. Why do you think it would be more efficient?

--
Bob Hairgrove
No**********@Home.com
Dec 10 '05 #2

P: n/a
What purpose does it serve to cast as a character pointer?

Dec 10 '05 #3

P: n/a
On 10 Dec 2005 10:48:36 -0800, "AMT2K5" <Aa*********@gmail.com> wrote:
What purpose does it serve to cast as a character pointer?


You are telling fout to write an int as binary data, so you must cast
the address of your number to a char* and give the size so that the
stream knows how many bytes (i.e. char's) to write.

The problem with void* is that void is not a type. With char*, you
know that you are dealing with a type which is exactly one byte large.
And a pointer to an object can be implicitly converted to void*, so
using char* is more type-safe. That is one of the major advantages of
C++ over C, for example.

But you still didn't say why you think void* would be more efficient?
The cast doesn't cost any processor cycles, nor does it take up any
extra memory.

--
Bob Hairgrove
No**********@Home.com
Dec 10 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.