c++ - Convert Address to long - variable results in value? -
can explain behaviour me pls?
static short ndosomething(const char* pcmsg, ...) { va_list pvargument; long ltest; void* pvtest = null; va_start(pvargument, pcmsg); pvtest = va_arg(pvargument, void*); ltest = (long) pvtest; va_end(pvargument); return 0; }
if call function in main this:
int main(int argc, char* argv[]) { char actest1[20]; ndosomething("testmessage", 1234567l, actest1); return 0; }
i thought address of pvtest in ltest, in fact contains 1234567 ...
how possible?
your code contains undefined behavior; standard requires type extracted using va_arg
correspond type passed (modulo cv-qualifiers, perhaps): passed long
, , read void*
, compiler correct.
in practice, compilers generate code no type checking. if on machine, long
, void*
have same size (and machine has linear addressing), end whatever passed long
. if sizes of 2 different, machine little endian, , pass small enough value, might end same value well. not @ guaranteed.
Comments
Post a Comment