iosobjective-ccalassetalassetlibrary

"Correct" way to cast ALAssetRepresentation size (long long) to size_t for 32-bit devices


I've got the following code that pulls data from an ALAssetRepresentation.

ALAssetRepresentation *representation = ...;
size_t size = [representation size]; // 'size' returns a long long
uint8_t *bytes = malloc(size);

When building for 32-bit devices on iOS, I get the following warning.

enter image description here

Blindly casting the long long to a size_t works, but I don't know what the "right" way to handle this situation is. Would love to know how to best handle it.


Solution

  • Since you are assigning to a type of size_t, cast the result as a size_t.

    size_t size = (size_t)[representation size];
    

    The only risk of this is that representation size could be a number larger than what will fit in a size_t. But if that happens, the value is too large to allocate memory for it anyway when running on an iOS device so that is a bigger problem than the loss in precision.