size of struct without use sizeof keyword
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;
}
开发者_如何学Goit 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
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.
struct ABC{
int a;
float b;
char c;
};
int main(){
struct ABC *ptr=(struct ABC *)0;
ptr++;
printf("Size of structure is: %d",*ptr);
return 0;
}
This is an alternate way to find size of struct
without using sizeof
.
精彩评论