Implementation of sizeof operator

The result of pointer subtraction is in elements and not in bytes. Thus the first expression evaluates to 1 by definition. This aside, you really ought to use parentheses in macros: #define my_sizeof(x) ((&x + 1) – &x) #define my_sizeof(x) ((char *)(&x + 1) – (char *)&x) Otherwise attempting to use my_sizeof() in an expression … Read more

sizeof single struct member in C

Although defining the buffer size with a #define is one idiomatic way to do it, another would be to use a macro like this: #define member_size(type, member) sizeof(((type *)0)->member) and use it like this: typedef struct { float calc; char text[255]; int used; } Parent; typedef struct { char flag; char text[member_size(Parent, text)]; int used; … Read more

Why is −1 > sizeof(int)?

The following is how standard (ISO 14882) explains abort -1 > sizeof(int) Relational operator `>’ is defined in 5.9 (expr.rel/2) The usual arithmetic conversions are performed on operands of arithmetic or enumeration type. … The usual arithmetic conversions is defined in 5 (expr/9) … The pattern is called the usual arithmetic conversions, which are defined … Read more

Using sizeof() on malloc’d memory [duplicate]

Because the size of the “string” pointer is 8 bytes. Here are some examples of using sizeof() with their appropriate “size”. The term size_of() is sometimes deceiving for people not used to using it. In your case, the size of the pointer is 8 bytes.. below is a representation on a typical 32-bit system. sizeof … Read more

What does sizeof do?

sizeof(x) returns the amount of memory (in bytes) that the variable or type x occupies. It has nothing to do with the value of the variable. For example, if you have an array of some arbitrary type T then the distance between elements of that array is exactly sizeof(T). int a[10]; assert(&(a[0]) + sizeof(int) == … Read more