0

I have an unclear question that I need more clarification about it. when I store data type like integer 4 byte. then how the processor with the architecture of 64 bit reads or writes an integer with a size of 4byte as I know the processor reads/writes a word. is there any padding size here. I will be thankful for some clarifications about that because I can not understand how it works or maybe I miss some things that I must read more about it. is it differs from compiler to compiler or language to language? Thanks a lot.

  • What about a `char` on a 32-bit system? or a short? Did that matter to you as well? If not, why does the above matter? You usually should not be interested in how data is stored by a language. Try and elaborate on why you need this, you may receive better help. – kabanus Sep 01 '20 at 05:48
  • yes all matter. there is no problem but that help me to understand my last lecture on uni. – Abdelrahman Elayashy Sep 01 '20 at 05:55

1 Answers1

1

as I know the processor reads/writes a word

Word-oriented CPUs can load/store in 64-bit chunks, but also narrower chunks. (Storing the low part of a register, or loading with zero-extension or sign-extension). Capability to do narrow stores is fairly essential for writing device drivers for most hardware, as well as for implementing efficiently sized integers that don't waste a huge amount of cache footprint, and for some kinds of string processing.

Some CPUs (like x86-64) are not really word-oriented at all, and have about the same efficiency for every operand-size. Although the default operand-size in x86-64 machine code is 32-bit.

All mainstream 64-bit architectures natively support 32-bit operand-size, including even DEC Alpha which was aggressively 64-bit and chose not to provide 8-bit or 16-bit loads/stores. (See Can modern x86 hardware not store a single byte to memory? for more details)

There might be some highly obscure 64-bit architecture where only 64-bit load/store is possible, but that seems unlikely. Also note that most modern 64-bit ISAs evolved out 32-bit ISAs.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • thanks a lot for your clarification. then the char 1 byte (integer 4 byte) will be stored in memory as 1 byte (4 byte) or will be stored as 8 byte(size of word) with padding size? – Abdelrahman Elayashy Sep 01 '20 at 07:47
  • @abdddde: Normally without padding of course, since CPUs can support those sizes efficiently. You can of course check `sizeof(int)` and `CHAR_BIT` in any specific C implementation you care about (e.g. on https://godbolt.org/ which has several 64-bit RISC ISAs as well as x86-64) – Peter Cordes Sep 01 '20 at 08:01
  • > ja it is a littel bit clear now. i have another question if you allowed me to ask you. i could not add a picture in the comment so i have added it up in the question. could you explain why we need for char a padding size and not for uint32_t and need padding size with 6 not only 2 for int16_t. Thanks a lot – Abdelrahman Elayashy Sep 01 '20 at 08:15
  • i will delet the picture after you see it. because it does not relate to the original question – Abdelrahman Elayashy Sep 01 '20 at 08:15
  • @abdddde: struct members get padded to their `alignof(T)` so the `uint32_t` object is correctly aligned for a `uint32_t`. The padding isn't "because of the char" alone, it's because the next member has 4-byte alignment. – Peter Cordes Sep 01 '20 at 08:17