Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

###Unaligned memory accesses###

Unaligned memory accesses

Suppose sizeof(size_t) is 4 and you wanted to use this code to allocate an array of doubles. What will happen is that the length will get stored in the first four bytes and the array of doubles will be shifted by 4 bytes. This means that each double will now be improperly aligned because instead of being on an 8 byte boundary, each double will instead be on a 4 byte boundary. Depending on your machine's architecture, this could cause problems.

The same thing will happen for any type with an alignment larger than the alignment of size_t. It will also affect fields within structs.

To fix the problem, I would suggest that instead of reserving sizeof(size_t) bytes for the length, you reserve MAX_ALIGNMENT bytes instead. You can define MAX_ALIGNMENT to be whatever it needs to be for your system, such as 8 or 16. Note that malloc itself has a built-in alignment which is typically 8 for 32-bit systems and 16 for 64-bit systems, so if you can determine what your malloc's alignment is, you can use the same value.

###Unaligned memory accesses###

Suppose sizeof(size_t) is 4 and you wanted to use this code to allocate an array of doubles. What will happen is that the length will get stored in the first four bytes and the array of doubles will be shifted by 4 bytes. This means that each double will now be improperly aligned because instead of being on an 8 byte boundary, each double will instead be on a 4 byte boundary. Depending on your machine's architecture, this could cause problems.

The same thing will happen for any type with an alignment larger than the alignment of size_t. It will also affect fields within structs.

To fix the problem, I would suggest that instead of reserving sizeof(size_t) bytes for the length, you reserve MAX_ALIGNMENT bytes instead. You can define MAX_ALIGNMENT to be whatever it needs to be for your system, such as 8 or 16. Note that malloc itself has a built-in alignment which is typically 8 for 32-bit systems and 16 for 64-bit systems, so if you can determine what your malloc's alignment is, you can use the same value.

Unaligned memory accesses

Suppose sizeof(size_t) is 4 and you wanted to use this code to allocate an array of doubles. What will happen is that the length will get stored in the first four bytes and the array of doubles will be shifted by 4 bytes. This means that each double will now be improperly aligned because instead of being on an 8 byte boundary, each double will instead be on a 4 byte boundary. Depending on your machine's architecture, this could cause problems.

The same thing will happen for any type with an alignment larger than the alignment of size_t. It will also affect fields within structs.

To fix the problem, I would suggest that instead of reserving sizeof(size_t) bytes for the length, you reserve MAX_ALIGNMENT bytes instead. You can define MAX_ALIGNMENT to be whatever it needs to be for your system, such as 8 or 16. Note that malloc itself has a built-in alignment which is typically 8 for 32-bit systems and 16 for 64-bit systems, so if you can determine what your malloc's alignment is, you can use the same value.

Source Link
JS1
  • 28.9k
  • 3
  • 41
  • 83

###Unaligned memory accesses###

Suppose sizeof(size_t) is 4 and you wanted to use this code to allocate an array of doubles. What will happen is that the length will get stored in the first four bytes and the array of doubles will be shifted by 4 bytes. This means that each double will now be improperly aligned because instead of being on an 8 byte boundary, each double will instead be on a 4 byte boundary. Depending on your machine's architecture, this could cause problems.

The same thing will happen for any type with an alignment larger than the alignment of size_t. It will also affect fields within structs.

To fix the problem, I would suggest that instead of reserving sizeof(size_t) bytes for the length, you reserve MAX_ALIGNMENT bytes instead. You can define MAX_ALIGNMENT to be whatever it needs to be for your system, such as 8 or 16. Note that malloc itself has a built-in alignment which is typically 8 for 32-bit systems and 16 for 64-bit systems, so if you can determine what your malloc's alignment is, you can use the same value.

lang-c

AltStyle によって変換されたページ (->オリジナル) /