Reputation: 564
What's the meaning of cast of pointer in C++?
For example, what does the write do?
Why does it cast uint32_t pointer to (const char*)??
class A{
uint32_t a;// initialized by some value
void write(ostream& os){
os.write((const char*)&a, sizeof(a));
}
};
Upvotes: 4
Views: 4754
Reputation: 75755
It can help if you look at the documentation of ostream::write
:
basic_ostream& write( const char_type* s, std::streamsize count );
Behaves as an UnformattedOutputFunction. [...]
So write
performs an unformatted write to the stream. That means no text formatting, conversion etc. (e.g. new lines). For that it requires a buffer of bytes to insert into the stream. Unfortunately, C++ does not have a byte
type as a first class citizen. Instead char
is used. That's why the cast to const char*
.
Upvotes: 2
Reputation: 9062
Pointers are variables which store memory locations. So all pointers have the same value range (integers from 0 to the maximum supportrd memory location).
Now, pointers support an operation called dereferencing: accessing the object stored at that memory address. If you have a memory address and you want to access the object there, you need to know things as the size of the object (in bytes) and a lot of stuff regardting how to actually interpret the values there. All of this is known by the type. So, we have pointers of different types and we can convert between them, since all of them have the same value domain.
This implies that by changing the type of the pointer you can have a different interpretation of the same object.
Let's come back at your question. The write function you showed should write to a stream byte by byte (has to do with many reasons, such as the fact that the byte is the smallest unit of storage so you can write anything by writing its bytes). Now, you have a 4 byte int, which you want to access byte by byte. How you do this? Simply by getting a pointer to it, convert it to a byte sized type (char) and accessing 4 bytes using pointer arithmetic (basically access 4 consecutive memory locations) - this is why you also need to send the size of the type. As a matter of precaution, the requested type of the first argument is const char*
, to guarantee that it's not going to change the data.
Now, in your case, the cast (the pointer conversion) was made explicit.
Upvotes: 3
Reputation: 311038
The member function write
is declared the following way
basic_ostream<charT,traits>& write(const char_type* s, streamsize n);
As you see its first parameter has type const char_type *
In this call
os.write((const char*)&a, sizeof(a)
there is used a pointer of type uint32_t *
. However there is no implicit conversion from the type uint32_t *
to the type const char_type *
in C++. So there is used the explicit conversion that is there is an attempt to write an object of type uint32_t
as a sequence of bytes.
Upvotes: 0
Reputation: 49986
This is a serialization of some data structure.
What's the meaning of cast of pointer in C++?
ofstream::write
expects const char* s
as its first parameter
Why does it cast uint32_t pointer to (const char*)??
it writes four bytes to some stream (an unsigned integer), maybe to std::ofstream. This way it writes value stored in a
to for example a file.
Upvotes: 0