c++ - Pointer memory usage -
i may wrong on these facts correct me!
a pointer points memory address, if went memory address find byte ? pointer points lowest address of memory segment points too, if points 8 byte memory segment, starts @ 0x0 , ends @ 0x7, point 0x0 ?
how pointer know size of memory points at? if pointer points segment of memory 128 bytes in size , pointer cast different type, happens size of memory segment?
how pointer know size of memory points @ ?
it doesn't.
so if pointer points segment of memory 128 bytes in size , pointer cast different type, happens size of memory segment ?
the memory's still there, since cast away information had object residing there, well, y'know. that's it. don't know.
that's why people use sizeof
when aliasing objects through char*
@ underlying bytes:
std::ofstream os("some-binary-file"); const double d = 0.1234; os.write((char*)&d, sizeof(double)); // ^^^^^^^^^^^^^^ // // otherwise os.write has no way of knowing how write
of course, if chuck around double*
, every piece of code using pointer assume points 1 or more "blocks" of memory precisely sizeof(double)
bytes wide. if doesn't, that's fault.
tl;dr: telling program how many pointee bytes has work your responsibility.
Comments
Post a Comment