What is the return type of sizeof operator?

C++11, §5.3.3 ¶6 The result of sizeof and sizeof… is a constant of type std::size_t. [ Note: std::size_t is defined in the standard header (18.2). — end note ] You can also do a quick check: which correctly outputs 1 on my machine. As @Adam D. Ruppe said in the comment, probably the compiler does not complain because, since it already knows the result, … Read more

Allocating char array using malloc

Yes, it’s a matter of style, because you’d expect sizeof(char) to always be one. On the other hand, it’s very much an idiom to use sizeof(foo) when doing a malloc, and most importantly it makes the code self documenting. Also better for maintenance, perhaps. If you were switching from char to wchar, you’d switch to … Read more

Why is the sizeof(int) == sizeof(long)?

A long, as far as I know, is 8 bytes. Is this correct? No, this is not correct. The C and C++ specifications only state that long must be greater than or equal to 32 bits. int can be smaller, but on many platforms, in C and C++, long and int are both 32 bits. This is a very good reason … Read more

Get size of pointer in C

Given an arbitrary type (I’ve chosen char here, but that is for sake of concrete example): You can use either of these expressions: Leading to a malloc() call such as: The last version has some benefits in that if the type of ppc changes, the expression still allocates the correct space.

Difference between sizeof(char) and sizeof(char *)

char is a character and sizeof(char) is defined to be 1. (N1570 6.5.3.4 The sizeof and _Alignof operators, paragraph 4) char* is a pointer to a character and sizeof(char*) depends on the environment. It is typically 4 in 32-bit environment and 8 in 64-bit environment. In typical environment where sizeof(char*) > sizeof(char), malloc(sizeof(char*)*len + 1) will (at least try to) allocate more memory than malloc(sizeof(char)*len + 1) if len is small … Read more

Is the sizeof(some pointer) always equal to four?

he guarantee you get is that sizeof(char) == 1. There are no other guarantees, including no guarantee that sizeof(int *) == sizeof(double *). In practice, pointers will be size 2 on a 16-bit system (if you can find one), 4 on a 32-bit system, and 8 on a 64-bit system, but there’s nothing to be gained in … Read more

sizeof float (3.0) vs (3.0f)

Because 3.0 is a double. See C syntax Floating point types. Floating-point constants may be written in decimal notation, e.g. 1.23. Scientific notation may be used by adding e or E followed by a decimal exponent, e.g. 1.23e2 (which has the value 123). Either a decimal point or an exponent is required (otherwise, the number is … 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: LIVE EXAMPLE Note that if you … Read more