sizeof Operator
The sizeof operator represents a compile-time unary operator that yields the size in bytes occupied by an object or type. The result is a value of type size_t, which is an unsigned integral type capable of representing the size of any object in memory.
Key characteristics of size_t:
- Machine-independent unsigned type
- Large enough to hold the size of any object in memory
The sizeof operaotr accepts either a type name or an expression as its operand:
struct Product {
std::string name;
double price;
int quantity;
};
Product item, *ptr;
sizeof(Product); // Size of the Product struct type
sizeof(Product); // Equivalent to above
sizeof(item); // Size of item object, equals sizeof(Product)
sizeof(ptr); // Size of pointer itself (8 bytes on 64-bit, 4 on 32-bit)
sizeof(*ptr); // Size of object pointed to, equals sizeof(Product)
sizeof(item.price); // Size of the price member (double)
Important behaviors:
- sizeof evaluates only the type of the expression, not the expression itself. The expression is not actually computed.
- For pointers: returns the size of the pointer variable itself. On 64-bit systems, all pointer types occupy 8 bytes regardless of what they point to.
- For dereferenced pointers: returns the size of the pointed-to type. This operation is safe even with null pointers because the expression is not evaluated.
- For references: returns the size of the referenced object, not the reference itself.
- For arrays: returns the total size of all elements combined. The array name does not decay to a pointer in this context.
- For std::string: returns a fixed size (typically 40 bytes on 64-bit systems), independent of string content length.
- For std::vector: returns a fixed size (typically 32 bytes on 64-bit systems), independent of element count.
strlen Function
The strlen function operates on C-style strings and is declared in the header. It counts the number of characters before the first null terminator.
C-style strings can be initialized in two ways:
char str1[] = {'C', '+', '+', '\0'};
char str2[] = "C++";
Modern C++ generally prefers std::string over C-style strings due to automatic memory management and safety guarantees.
Key Differences
| Aspect | sizeof | strlen |
|---|---|---|
| Category | Compile-time operator | Runtime library function |
| Evaluation | Computed at compile time | Computed at runtime |
| Null character | Counts '\0' in size | Stops at first '\0' |
| Parameter | Accepts any type | Requires character pointer |
Demonstration of differences:
#include <iostream>
#include <cstring>
#include <string>
int main() {
char a[] = "test";
std::cout << "a: " << a
<< " | strlen: " << strlen(a)
<< " | sizeof: " << sizeof(a) << std::endl;
char b[] = "test\0";
std::cout << "b: " << b
<< " | strlen: " << strlen(b)
<< " | sizeof: " << sizeof(b) << std::endl;
char c[] = "te\0st";
std::cout << "c: " << c
<< " | strlen: " << strlen(c)
<< " | sizeof: " << sizeof(c) << std::endl;
char d[] = "\\0";
std::cout << "d: " << d
<< " | strlen: " << strlen(d)
<< " | sizeof: " << sizeof(d) << std::endl;
return 0;
}
Output analysis:
- For "test": strlen returns 4, sizeof returns 5 (includes null terminator)
- For "test\0": strlen returns 4 (stops at explicit null), sizeof returns 6
- For "te\0st": strlen returns 2 (stops at null), sizeof returns 6
- For "\0": this represents two characters: backslash and zero, strlen returns 2, sizeof returns 3
The backslash serves as an escape character in C-style strings, combining with subsequent characters to form special sequences.