P
Praveen Raj
Sir, Is this emulation of sizeof is valid..??
size_t sizeof_obj = (char*)(&obj + 1) - (char*)(&obj);
Standard says
A pointer to an object or incomplete type may be converted to a
pointer to a different object or incomplete type. If the resulting
pointer is not correctly aligned for the pointed-to type, the behavior
is undefined.
and
When a pointer to an object is converted to a pointer to a character
type, the result points to the lowest addressed byte of the object.
Successive increments of the result, up to the size of the object,
yield pointers to the remaining bytes of the object.
+
If two pointers to objects of the same type are subtracted, the result
is a signed integral value representing the displacement between the
pointed-to objects; pointers to successive objects differ by 1. The
type of the result is defined as ptrdiff_t in the standard header
<stddef.h>. The value is undefined unless the pointers point to
objects within the same array; however, if P points to the last member
of an array, then (P+1)-P has value 1..
size_t sizeof_obj = (char*)(&obj + 1) - (char*)(&obj);
Standard says
A pointer to an object or incomplete type may be converted to a
pointer to a different object or incomplete type. If the resulting
pointer is not correctly aligned for the pointed-to type, the behavior
is undefined.
and
When a pointer to an object is converted to a pointer to a character
type, the result points to the lowest addressed byte of the object.
Successive increments of the result, up to the size of the object,
yield pointers to the remaining bytes of the object.
+
If two pointers to objects of the same type are subtracted, the result
is a signed integral value representing the displacement between the
pointed-to objects; pointers to successive objects differ by 1. The
type of the result is defined as ptrdiff_t in the standard header
<stddef.h>. The value is undefined unless the pointers point to
objects within the same array; however, if P points to the last member
of an array, then (P+1)-P has value 1..