7

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?

3
  • 3
    Padding.
    – BoBTFish
    Nov 21, 2013 at 9:24
  • 1
    You could try __attribute__((packed)) to see how it looks (but it's usually not a good practice for performance)
    – Leeor
    Nov 21, 2013 at 9:26
  • 1
    You can even reduce the memory requirement to sizeof(int) if you do not need all int-bits for charge. See my answer below. (Maybe one more or less power of 2 in charge does not make a big difference.)
    – Tobias
    Nov 21, 2013 at 11:34

6 Answers 6

10

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.

1
6

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.

3

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.

1

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.

1

It's true that bool usually uses one byte (it doesn't have to, though).

However, C and C++ are also allowed to add padding -- that is, empty space -- into structs to allow for alignment. It's a bit complicated, but basically when variables are aligned on particular boundaries, then memory access becomes much quicker.

In this case, adding the extra bool has probably increased the struct size from 4 bytes (for just in the int) to 8 bytes, so allow the structs to be nicely aligned inside your array. This all depends on your compiler of course, but you can use sizeof(TPos) to check.

Most compilers have extensions to request that a particular struct not be padded, even if this makes them slower to use -- you may want to look into this.

1

You can use the size specifier to reduce the size of your objects (See the : in the following code). If you do not really need the full int size for charge this could look like TPosB:

#include <iostream>

#include<stdio.h>
struct TPosB
{
    int charge : sizeof(int)-1;
    bool ex : 1;
    TPosB()
    {
        charge=1 << sizeof(int)-2;
        ex=false;
    }
};
TPosB b;

struct TPos
{
    int charge;
    bool ex;
    TPos()
    {
        charge=1<<30;
        ex=false;    
    }  
};
TPos a;

int main()
{
    std::cout << "sizeof(int)" << sizeof(int);
    std::cout << "\nsizeof(a)=" << sizeof(a);
    std::cout << "\nsizeof(b)=" << sizeof(b);

    return 0;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.