tags:

views:

142

answers:

1

here is code which returns size of struct without using sizeof keyword

#include <iostream>
using namespace std;
struct  point{
    int x;
    int y;
    };
struct point pt={0,0};

int main(){
    point *ppt=&pt;
    unsigned char *p1,*p2;
    p1=(unsigned char *)ppt;
    p2=(unsigned char *)++ppt;
    printf("%d",p2-p1);


     return 0;
}

it returns 8 as i understand because sizeof char is 1 byte and this struct contains integer types first it convers it to char using char pointers and returns sizeof char? or?i dont understand exactly how it works thanks

+4  A: 

The cast here happens after the ++

p2 = (unsigned char *)++ppt;

It works because ++ on a pointer increases the pointer the number of bytes equal to the size of the type pointed to. Then you cast to char, because minus divides the difference in pointers by the size of the type (so divide by 1 because it's now char*).

One caveat if you plan to use this -- sizeof is definitely done at compile time where this code may or may not be recognized by the optimizer as being a constant expression.

Also, as pointed out by the commenter and other question, it won't match sizeof if the type needs alignment (some systems require that types start on memory boundaries divisible by 2, 4, etc).

Finally, (from the comments), once the pointer has been incremented, it is invalid and cannot be used (even for subtracting, comparison -- i.e. even in ways that don't dereference it)

From the C Rationale Document: http://www.open-std.org/jtc1/sc22/wg14/www/C99RationaleV5.10.pdf

Implicit in the Standard is the notion of invalid pointers. In discussing pointers, the Standard typically refers to “a pointer to an object” or “a pointer to a function” or “a null pointer.” A special case in address arithmetic allows for a pointer to just past the end of an array. Any other pointer is invalid.

An invalid pointer might be created in several ways. An arbitrary value can be assigned (via a cast) to a pointer variable. (This could even create a valid pointer, depending on the value.) A pointer to an object becomes invalid if the memory containing the object is deallocated or moved by realloc. Pointer arithmetic can produce pointers outside the range of an array.

Regardless how an invalid pointer is created, any use of it yields undefined behavior. Even assignment, comparison with a null pointer constant, or comparison with itself, might on some systems result in an exception.

Consider a hypothetical segmented architecture on which pointers comprise a segment descriptor and an offset. Suppose that segments are relatively small so that large arrays are allocated in multiple segments. While the segments are valid (allocated, mapped to real memory), the hardware, operating system, or C implementation can make these multiple segments behave like a single object: pointer arithmetic and relational operators use the defined mapping to impose the proper order on the elements of the array. Once the memory is deallocated, the mapping is no longer guaranteed to exist. Use of the segment descriptor might now cause an exception, or the hardware addressing logic might return meaningless data.

Lou Franco
After incrementing ppt, it no longer points to a valid object. Any use of ppt afterwards results in undefined behaviour. This is probably the fifth time this type of question has been discussed today. :/
Michael Foukarakis
"fifth time this type of question has been discussed today" - do I smell like a stupid homework assignment? ;)
msw
@msw: either that, or an interview. :-)
Michael Foukarakis
@mfukar You can't dereference it, but you can use the value of the pointer. If not, things like .end() in the STL wouldn't work.
Lou Franco
@Lou end() works with arrays, where you are allowed one past the end - there is no array in the OP's code.
anon
Arrays aren't actually needed: `int a; (` is technically valid too. It's also a pointer immediately past an object. ISTR this has been clarified after 1998 (C++ standard) and wasn't explicit in C89.
MSalters
Ahh -- correct. Just updated with the appropriate part from the standard
Lou Franco
ok -- I'm not sure what the right answer is -- if someone has a definitive source, please update the answer. In any case, with things this subtle, where experts debate the standard -- BEST TO NOT DEPEND ON IT.
Lou Franco