Difference between data size and sizeof (data type)

I studied C ++ and came across the following question. I'm just a novice, and I'm embarrassed. Is the sizeof () function supposed to return the size of a data type? Why does the data object have a different size from its sizeof ()? I do not understand the explanation of the answer.

Assume that in a hypothetical machine, the size of char is 32 bits. What will return sizeof (char)?

a) 4

b) 1

c) Implementation dependent

d) Machine dependent

Answer: b

Explanation: The standard does NOT require char to be 8 bits, but requires sizeof (char) to return 1.

+4
source share
6 answers

sizeof , char. , sizeof(char) , , char .

C, ++.


C11 6.5.3.4

  1. sizeof ( ) , . ....

  1. sizeof char, unsigned char signed char ( ), 1.

++ 11 5.3.3

  • sizeof . - , ( 5), ....... sizeof(char), sizeof(signed char) sizeof(unsigned char) 1. >

( )

+12

Per 5.3.3 [expr.sizeof]

sizeof . , ( 5), . sizeof , , , , , gl, -. sizeof (char), sizeof ( char) sizeof ( char): 1. [...]

, , a char , 1

+3

.

- . true sizeof(char) == 1, sizeof

8 .

. , , .

+3

sizeof(x) x, char.

+2

, sizeof (char) 4. 1 . 32 , , C, .

"8 " - . C "byte" , char. "byte" -, , "", C ( ++ Objective-C) " char". A char 8 , .

0

: , word size(size of registers) - 32 . sizeof (char)?

1 byte.

, . . ( , ) . - https://en.wikipedia.org/wiki/Word_%28computer_architecture%29

word-size 32 .

, , . - https://en.wikipedia.org/wiki/Byte

1 byte is the smallest addressable unit of memory, it should be 8 bits, 9 bits or 16 bits, which is chosen by the hardware specification.

As for sizeof, it first determines the type of argument, ultimately calculates the size in bytes. So after two C ++ statements you will get the same result.

  int n;
  std::cout<<sizeof(int);
  std::cout<<sizeof(n);
-1
source

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


All Articles