Does not evaluating the expression to which sizeof is applied make it legal to dereference a null or invalid pointer inside sizeof in C++?

I believe this is currently underspecified in the standard, like many issues such as What is the value category of the operands of C++ operators when unspecified?. I don’t think it was intentional, like hvd points outs it is probably obvious to the committee. In this specific case I think we have the evidence to … Read more

sizeof() a vector

You want vector::size() and set::size(). Assuming v is your vector, do this: size_t size = 0; for (vector<set<char> >::const_iterator cit = v.begin(); cit != v.end(); ++cit) { size += cit->size(); } sizeof() is giving you the in-memory size of the object/type it is applied to, in multiples of sizeof(char) (usually one byte). If you want … Read more

What is guaranteed about the size of a function pointer?

From C99 spec, section 6.2.5, paragraph 27: A pointer to void shall have the same representation and alignment requirements as a pointer to a character type. Similarly, pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements. All pointers to structure types shall have the same representation and … Read more

Checking the size of an object in Objective-C

All the compiler knows about is the pointer, which is why you’re getting the size of the pointer back. To see the size of the allocated object, use one of the following code snippets: With ARC: #import <malloc/malloc.h> // … NSLog(@”Size of %@: %zd”, NSStringFromClass([myObject class]), malloc_size((__bridge const void *) myObject)); Without ARC: #import <malloc/malloc.h> … Read more

How do sizeof(arr) / sizeof(arr[0]) work?

If you have an array then sizeof(array) returns the number of bytes the array occupies. Since each element can take more than 1 byte of space, you have to divide the result with the size of one element (sizeof(array[0])). This gives you number of elements in the array. Example: std::uint32_t array[10]; auto sizeOfInt = sizeof(std::uint32_t); … Read more