When i run this program at dev cpp, task manager says that it's about 79 MB. Codeforces with gnu c++ 4.7 says that it's 79112 kilobytes
#include<stdio.h>
const int N=10010,K=1010;
struct TPos
{
int charge;
bool ex;
TPos()
{
charge=1<<30;
ex=false;
}
};
TPos d[N][K];
int main()
{
while(1);
return 0;
}
But when ex parametr is commented:
#include<stdio.h>
const int N=10010,K=1010;
struct TPos
{
int charge;
//bool ex;
TPos()
{
charge=1<<30;
//ex=false;
}
};
TPos d[N][K];
int main()
{
//while(1);
return 0;
}
it's only 39536 KB. I thought that boolean should use one byte. Why does it double the size?
But there is no point in using an int rather than a boolean , at worst, they would take up the same amount of memory; at best it's just more efficient to use the boolean .
bool The bool type takes one byte and stores a value of true (1) or false(0).
Boolean variables are stored as 16-bit (2-byte) numbers, but they can only be True or False.
A bool takes in real 1 bit, as you need only 2 different values. However, when you do a sizeof(bool), it returns 1, meaning 1 byte. For practical reasons, the 7 bits remaining are stuffed. you can't store a variable of size less than 1 byte.
Unless you pack a structure it will always take memory that is divisable by the word size(because of the memory allignment). You pack a structure by using __attribute__(packed)
in gcc for example. See also here. Packing a structure may reduce the required memory but will almost certainly slow down the execution.
Bool is a byte, but what you're seeing here is structure packing.
Your struct contains an int, so the compiler automatically aligns the structure to the size of an int to ensure that it's guaranteed to be properly aligned in an array. Otherwise, the second element of your array would have the int on an improperly aligned address - that could lead to decreased performance and on some architectures even to a crash.
You can explicitly turn structure packing off using compiler-specific pragmas and attributes, but you don't want to. If memory is a concern, consider using a struct of arrays instead of an array of structs.
Because of the memory alignment. In this case, the bool
is aligned in the struct
to int
. Meaning - there are some extra unused bytes.
This means, sizeof( your_struct )
will give you 2 * sizeof( int )
.
You can do some experiments with this - check the sizeof
the struct
ure with some more bool
s inside it (one after the other), check the size again by reordering the elements, etc. These are useful experiments to understand what happens. Also, research for struct padding and memory alignment would be extremely useful for you.
It has to do with structure alignment. On some platforms it's easier to address memory if it's multiple of 4 bytes. The compiler might pad your structure to make it easier to work with. If I'm correct you can add another bool and you will see no extra space used.
Check compiler options on how to disable it, but note that the performance might suffer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With