Reputation: 1
How can I determine the exact amount of memory in megabytes that a "char" data type uses, beyond just knowing its size in bytes, nibbles, kilobytes, and bits?
I’ve tried using sizeof and float conversions but need a deeper understanding. I´ve also tried math operations but i cant get to real number
Upvotes: 0
Views: 92
Reputation: 25326
On most common platforms, a byte is defined as 8
bits, and sizeof(char)
is always a single byte. You can check the macro constant CHAR_BIT
to determine whether your platform defines one byte as something different than 8
bits. However, such platforms are very rare. In the remainder of my answer, I will therefore assume that one byte is 8
bits.
So in most situations, adding a char
to your program will increase memory usage by a single byte. If you have an array of 1000
elements of type char
, then this array will use 1000
bytes of memory.
However, the smallest amount of memory that an operating system handles is a memory page, and these pages commonly have a size of 4096
bytes. Therefore, adding a single char
to your program will probably not increase your program's actual memory usage. Only if adding the char
causes the program to require an additional memory page will the memory usage actually increase, and in this case it will increase by an entire memory page (e.g. 4096
bytes) at once. If you are using dynamic memory allocation (e.g. malloc
), then the allocator will likely require a small amount of additional memory for bookkeeping.
Upvotes: 1