For small integer values, memory space is lost

I know that an integer data type takes up 2 or 4 bytes of memory. I want to know that if the value of the int datatype variable is less, then the space is lost?

#include <stdio.h>
int main(void)
{
  int a=1;
  printf("%d\n",a);
}

the binary value is 00000001, which is 1 byte, the int data type allocates 2 bytes of space for the value of a. 1 byte left?

+4
source share
6 answers

In theory, yes, space is wasted. Although on a 32-bit processor, allocating 32 bits of data can mean faster access, since it is suitable for alignment. Thus, using a 32-bit variable only to store a value of 1 can be an optimization of speed compared to memory consumption.

, stdint.h, , . uint8_t, int.

, - , , , uint_fast8_t. , 255.

+4

, 2 4

? C , int -32767 +32767 , a short char.

int. , "" int - 64 .

, signed char. -127 +127. sizeof(char) 1 . CHAR_BIT, 8.

, , , C, int , , int, int C.

+1

, , , , int, .

int 32 , -2,000,000,000 2,000,000,000, 32 , int . , , -30 000 30000, 16- , .

, "" , "" . "" "", .

+1

, , , .

, , 0 1, . , , , .

char s.

0

" " - - . . , char ( ) 8 .

, stdint.h, uint8_t, int8_t, uint16_t, int16_t ..

, , "" - .

, , , , /.

, . , , , , /, , - .

0

char, short, int`` long, long long, float double ( ) , [link] [1 ].

For example, to calculate, 2 bytes for char (usually 1 byte). For example, the ARM architecture has special assembly instructions for managing the location of 16-bit memory; the compiler can select 2 bytes for speed and space. However, the programmer should not be involved in the conversion, because the compiler does them. In this case, extra bytes are not used in your code.

0
source

Source: https://habr.com/ru/post/1692759/


All Articles