lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


int type.
Varies enormously on various platforms, with 32 bits on almost all and 64 bits on some.
See at:
https://hownot2code.com/2016/08/30/how-to-cast-a-pointer-to-int-in-a-64-bit-application-correctly/
https://medium.com/@CPP_Coder/a-collection-of-examples-of-64-bit-errors-in-real-programs-baba1a4a73b4

We have a string with more than 4GB, in pointers (64 bits) "const char * conv" and "char * buff" .
The convlen size is int (32 bits).

int memcmp ( const void * ptr1, const void * ptr2, size_t num )
void * memcpy ( void * destination, const void * source, size_t num );




Both expect size_t, as a length parameter (num).
oplen is declared as int type.

While int addresses all the need for options, correcting convlen,
oplen is compared to convlen and used as the index of a string (size_t),
the appropriate type of which is used is size_t or ptrdiff_t.

      return conv + oplen;  /* pointer arithmetic with int type */

#include <stdio.h>
int main()
{
   printf ("sizeof(size_t)=%llu\n", sizeof(size_t));
   printf ("sizeof(int)=%llu\n", sizeof(int));
   return 0;
}
sizeof(size_t)=8
sizeof(int)=4

Thus, the code follows good programming practices in C, and becomes 64-bit compatible.

regards,
Ranier Vilela