Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does a bool appear to take up as much memory as an int? C++

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?

like image 266
user2136963 Avatar asked Nov 21 '13 09:11

user2136963


People also ask

What takes more memory bool or int?

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 .

How much memory does a bool take in C?

bool The bool type takes one byte and stores a value of true (1) or false(0).

How much memory does a boolean take up?

Boolean variables are stored as 16-bit (2-byte) numbers, but they can only be True or False.

Why do Booleans take up a byte?

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.


4 Answers

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.

like image 124
Ivaylo Strandjev Avatar answered Oct 08 '22 18:10

Ivaylo Strandjev


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.

like image 28
EboMike Avatar answered Oct 08 '22 18:10

EboMike


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 structure with some more bools 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.

like image 25
Kiril Kirov Avatar answered Oct 08 '22 19:10

Kiril Kirov


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.

like image 24
Sorin Avatar answered Oct 08 '22 19:10

Sorin