How much space can occupy an integer or data

5

Hello I understand an integer occupies: 4 bytes. But my question is if the integer is very large how much it can occupy or 4 bytes is for each whole number. That was my doubt. And it is not only for that reason but also to understand if another data type size is considered per unit or per data. that is, an "a" character will have two "aa" characters that will weigh 2 bytes. Then it will happen with integers also "1" will weigh 4 bytes "11" will weigh 8 bytes Is it correct?

And this is my doubt because I made this little program

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    cout<<endl;
    cout<<"Tamaño de int es {0}"<<sizeof(int)<<endl;
    getch();
}

And when I put a long number like 2222222222

cout<<"Tamaño de int es {0}"<<sizeof(2222222222)<<endl;

It gives me 4 bytes so it does not depend on how big the number is. Can someone explain to me better how is this the size of each data in memory?

and with the string the same thing happens as with the characters? since they are strings of text and with that already the answer I would clarify my doubts enough if it is a string, how would it be?

    
asked by jeronimo urtado 15.12.2016 в 15:41
source

3 answers

4

The numbers in are stored in binary format. Thus number 10 becomes 1010 and number 22% is 10110 .

In C ++ the types do not have a fixed size, their size is dependent on the architecture of the system. Of course, for a given architecture the size of a type is fixed.

In the case of int , in an application for pc it usually occupies 32 bits. If we assume that this is your case and given that 1 bit is used to indicate the sign of the number we have that the range of values that the type int admits is 2 ^ 31 a (-2 ^ 31) -1 or, said in another way from -2,147,483,648 to 2,147,483,647. This means that any number within this range will be stored in 4 bits regardless of its value. Thus the value 1 and the value 123456789 will occupy both 32 bits (the digits of greater weight that are not in use are initialized with zeros).

In any case, C ++ has a series of utilities in the limits library that inform you about the valid range for each type of data. So:

#include <limits>
#include <iostream>

int main()
{
  std::cout << "rango de int:   " << std::numeric_limits<int>::min() << " a "
                                  << std::numeric_limits<int>::max() << '\n'
            << "rango de short: " << std::numeric_limits<short>::min() << " a "
                                  << std::numeric_limits<short>::max() << '\n'
            << "rango de char:  " << (int)std::numeric_limits<char>::min() << " a "
                                  << (int)std::numeric_limits<char>::max() << '\n';
}

Your exit:

rango de int:   -2147483648 a 2147483647
rango de short: -32768 a 32767
rango de char:  -128 a 127

Exceeding the capacity of one of the types causes an effect known as overflow ( overflow ). In this case, a specific flag is activated in the processor and the number loses values:

int main()
{
  int valor = std::numeric_limits<int>::max();
  std::cout << valor << '\n' << valor + 1;
}

Your exit:

2147483647
-2147483648

As you can see, the number has suddenly turned around. Go from a positive number to a negative number this is because:

  0111 1111 1111 1111 1111 1111 1111 1111 -> 2147483647
+                                       1
  ---------------------------------------
  1000 0000 0000 0000 0000 0000 0000 0000 -> -2147483648
  ^
  Este es el bit de signo

Another example:

int main()
{
  int valor = std::numeric_limits<int>::max();
  std::cout << valor << '\n' << valor + valor;
}

Exit:

2147483647
-2

Explanation to this second case

  0111 1111 1111 1111 1111 1111 1111 1111 -> 2147483647
+ 0111 1111 1111 1111 1111 1111 1111 1111 -> 2147483647
  ---------------------------------------
1 1111 1111 1111 1111 1111 1111 1111 1110 -> -2
^                                             ^
Este dígito se pierde                         Los números negativos se almacenan en complemento a 2

Thus, any sum of two integers is always going to give a result, even if this is incorrect (that's what the overflow flag is for).

What happens in the case of character strings?

Character strings are not a native type. They do not occupy a regular number of bytes since it depends on the size of the text to be stored.

Here we should distinguish between character arrays and string classes ( std::string , std::wstring , ...):

  • In the case of character arrays, you must bear in mind that at some point you have indicated the maximum size. In this case, if you try to write beyond the defined size (remember that the indexes begin to count at 0), what you will get is to write in memory that does not belong to the array. This usually results in a malfunction of the program or even in a casque if the Operating System realizes.

    char cadena[10];
    cadena[10] = '0'; // Error estás escribiendo fuera del arreglo
    
  • In the case of string classes you should know that it is the classes themselves that are responsible for managing the memory for the string. They alone are able to adapt the size of the buffer so that the text you try to store enters. This type of elements also has a maximum size that can be around 2 ^ 32 or 2 ^ 64 characters (that is if you do not run out of memory before).

answered by 15.12.2016 / 16:09
source
4

Characters are saved one byte per character. With the numbers does not pass them.

The numbers are saved compressed as binary. A binary number is a positional number where each position with a one indicates that that position is "on" and to calculate its value becomes 2 ^ position. (starting from position 0).

As an example:

To save 456. First we convert it to binary.

                           256 + 0 + 64 + 32 + 0 + 0 + 4 + 0 + 0
                            1    0   1     1   0   0   1   0   0

Now you have a 9-bit number, and you have to fill the remaining 23 with 0.

So the value that is saved is:

         00000000 00000000 00000001 01100100

Your doubt is common, when you think of numbers as strings, but processors usually store the numbers in a different format, to save space and take advantage of computational bits (multiply by two is for example a bit shift to left, divide right).

    
answered by 15.12.2016 в 16:04
1

Integers are always weighing 4 bytes (32 bits). When you declare an integer you are assigned a 32-bit memory space, these bits may or may not be used in their entirety. How the numbers are saved in binary if you keep a one you would have: 00000000000000000000000000000001 And if you keep an 11 it would be: 00000000000000000000000000001011.

In case of the characters you will have one byte for each character you have. Since the characters are represented in 8 bits therefore they are also stored in a few zeros. For more information you can consult the ascii table which tells you the number value that corresponds to a letter.

    
answered by 15.12.2016 в 16:24