8 bit int vs 32 bit int on a 32 bit architechture (GCC)

ananio

While coding, I try not to use more variable memory than needed and that leads me to write code like this:

for (uint8 i = 0; i < 32; i++) {
   ...
}

instead of:

for (int i = 0; i < 32; i++) {
   ...
}

(uint8 instead of int because i only need to go up to 32)

This would make sense when coding on an 8bit microprocessor. However, am I saving any resources if running this code on a 32bit microprocessor (where int might be 16 or 32 bit)? Does a compiler like GCC do any magic/juju underneath when I explicitly use an 8bit int on a 32bit architecture?

ElderBug

In most cases, there won't be any memory usage difference because i will never be in memory. i will be stored in a CPU register, and you can't really use one register to store two variables. So i will take one register, uint8 or uint32 doesn't matter.

In some rare cases, i will actually be stored in memory, because the loop is so big that all the CPU registers are taken. In this case, there is still a good chance you won't gain any memory either, because others multi-bytes variables will be aligned, and i will be followed by some useless padding bytes to align the next variable.

Now, if i is actually stored in memory, and there are other 8-bit variables to fill the padding, you may save some memory, but it's so little and so unlikely that it probably isn't worth it. Performance-wise, the difference between 8-bit and 32-bit is very architecture dependent, but usually it will be the same.

Also, if you are working in a 64-bit environment, this memory saving is probably non-existent, because the 64-bit call convention impose a huge 16-bytes alignment on the stack (where i will be stored if it is in memory).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

GCC fprintf: should that be an int (32bit) or char (8bit)?

From Dev

Bit twiddling to get sign bit of 32 bit int

From Dev

32 bit vs 64 bit

From Dev

How to split an unsigned long int (32 bit) into 8 nibbles?

From Dev

Converting 16 bit unsigned int array to 32 bit float array

From Dev

Unsigned int from 32 bit to 64bit OS

From Dev

Convert 11 bit hex value to a signed 32 bit int

From Dev

Int overflow on 32-bit systems

From Dev

constructing 32 bit unsigned int in c

From Dev

Python - Generate a 32 bit random int with arguments

From Dev

sizeof(int*) in 32-bit compatibility mode

From Dev

Combine four 8-bit unsigned ints into one 32-bit unsigned int

From Dev

Combine four 8-bit unsigned ints into one 32-bit unsigned int

From Dev

Will an int be 32bit and a long 64bit regardless of whether the system is 32 or 64 bit?

From Dev

Will an int be 32bit and a long 64bit regardless of whether the system is 32 or 64 bit?

From Dev

Byte conversion from 32 bit to 8 bit

From Dev

32 bit registers act as 8 bit ones

From Dev

How to shift 32 bit int by 32 (yet again)

From Dev

Why is the default alignment for `int64_t` 8 byte on 32 bit x86 architecture?

From Dev

how to extract the 4 bytes of a 32bit int in lua

From Java

A fast method to round a double to a 32-bit int explained

From Dev

libjpeg/libjpeg-turbo RGBA/32-bit int decompression

From Dev

How to get the first 11 bits of a 32 bit int with ctypes

From Dev

Reading a certain bit from a u32int

From Dev

Conversion from 32 bit binary string to int Java

From Dev

libjpeg/libjpeg-turbo RGBA/32-bit int decompression

From Dev

C# change the first 32bit Int of a GUID

From Dev

Display a 32 bit unsigned int image with Tkinter and Python 3

From Dev

Bit manipulation and > 32 bit numbers?

Related Related

  1. 1

    GCC fprintf: should that be an int (32bit) or char (8bit)?

  2. 2

    Bit twiddling to get sign bit of 32 bit int

  3. 3

    32 bit vs 64 bit

  4. 4

    How to split an unsigned long int (32 bit) into 8 nibbles?

  5. 5

    Converting 16 bit unsigned int array to 32 bit float array

  6. 6

    Unsigned int from 32 bit to 64bit OS

  7. 7

    Convert 11 bit hex value to a signed 32 bit int

  8. 8

    Int overflow on 32-bit systems

  9. 9

    constructing 32 bit unsigned int in c

  10. 10

    Python - Generate a 32 bit random int with arguments

  11. 11

    sizeof(int*) in 32-bit compatibility mode

  12. 12

    Combine four 8-bit unsigned ints into one 32-bit unsigned int

  13. 13

    Combine four 8-bit unsigned ints into one 32-bit unsigned int

  14. 14

    Will an int be 32bit and a long 64bit regardless of whether the system is 32 or 64 bit?

  15. 15

    Will an int be 32bit and a long 64bit regardless of whether the system is 32 or 64 bit?

  16. 16

    Byte conversion from 32 bit to 8 bit

  17. 17

    32 bit registers act as 8 bit ones

  18. 18

    How to shift 32 bit int by 32 (yet again)

  19. 19

    Why is the default alignment for `int64_t` 8 byte on 32 bit x86 architecture?

  20. 20

    how to extract the 4 bytes of a 32bit int in lua

  21. 21

    A fast method to round a double to a 32-bit int explained

  22. 22

    libjpeg/libjpeg-turbo RGBA/32-bit int decompression

  23. 23

    How to get the first 11 bits of a 32 bit int with ctypes

  24. 24

    Reading a certain bit from a u32int

  25. 25

    Conversion from 32 bit binary string to int Java

  26. 26

    libjpeg/libjpeg-turbo RGBA/32-bit int decompression

  27. 27

    C# change the first 32bit Int of a GUID

  28. 28

    Display a 32 bit unsigned int image with Tkinter and Python 3

  29. 29

    Bit manipulation and > 32 bit numbers?

HotTag

Archive