Skip to content

Instantly share code, notes, and snippets.

@RichardBarrell
Last active February 4, 2017 04:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichardBarrell/d46de8b527002233445ce43a0821b4a2 to your computer and use it in GitHub Desktop.
Save RichardBarrell/d46de8b527002233445ce43a0821b4a2 to your computer and use it in GitHub Desktop.
Demonstrating that converting an int containing -1 to size_t does the right thing even where sizeof(int) != sizeof(size_t)
#include <stdint.h>
#include <stdio.h>
#include <limits.h>
typedef struct rtunion {
char whatever[8];
} rtunion;
int main (int argc, char **argv) {
(void)argc;
(void)argv;
int z = 0;
int n = -1;
size_t sz = n;
printf("sizeof(n) = %zu\n", sizeof(n));
printf("n = %d\n", n);
printf("(size_t)n = %zu\n", (size_t)n);
printf("sizeof(sz) = %zu\n", sizeof(sz));
printf("sz = %zu\n", sz);
printf("n * sizeof(rtunion) = %zu\n", n * sizeof(rtunion));
printf("(z - 1) * sizeof(rtunion) = %zu\n", (z - 1) * sizeof(rtunion));
printf("(size_t)(uint32_t)n = %zu\n", (size_t)(uint32_t)n);
return 0;
}
pi@richardpi:~ $ gcc size_t_minus_one.c -o size_t_minus_one_gcc_armv7 -Wall -Wextra
pi@richardpi:~ $ ./size_t_minus_one_gcc_armv7
sizeof(n) = 4
n = -1
(size_t)n = 4294967295
sizeof(sz) = 4
sz = 4294967295
n * sizeof(rtunion) = 4294967288
(z - 1) * sizeof(rtunion) = 4294967288
(size_t)(uint32_t)n = 4294967295
pi@richardpi:~ $ clang size_t_minus_one.c -o size_t_minus_one_clang_armv7 -Wall -Wextra
pi@richardpi:~ $ ./size_t_minus_one_clang_armv7
sizeof(n) = 4
n = -1
(size_t)n = 4294967295
sizeof(sz) = 4
sz = 4294967295
n * sizeof(rtunion) = 4294967288
(z - 1) * sizeof(rtunion) = 4294967288
(size_t)(uint32_t)n = 4294967295
pi@richardpi:~ $ gcc --version
gcc (Raspbian 4.9.2-10) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
pi@richardpi:~ $ clang --version
Raspbian clang version 3.5.0-10+rpi1 (tags/RELEASE_350/final) (based on LLVM 3.5.0)
Target: arm-unknown-linux-gnueabihf
Thread model: posix
RichardB@callisto ~$ gcc size_t_minus_one.c -Wall -Wextra -o size_t_minus_one_gcc_64
RichardB@callisto ~$ clang size_t_minus_one.c -Wall -Wextra -o size_t_minus_one_clang_64 -fsanitize=undefined
RichardB@callisto ~$ ./size_t_minus_one_gcc_64
sizeof(n) = 4
n = -1
(size_t)n = 18446744073709551615
sizeof(sz) = 8
sz = 18446744073709551615
n * sizeof(rtunion) = 18446744073709551608
(z - 1) * sizeof(rtunion) = 18446744073709551608
(size_t)(uint32_t)n = 4294967295
RichardB@callisto ~$ ./size_t_minus_one_clang_64
sizeof(n) = 4
n = -1
(size_t)n = 18446744073709551615
sizeof(sz) = 8
sz = 18446744073709551615
n * sizeof(rtunion) = 18446744073709551608
(z - 1) * sizeof(rtunion) = 18446744073709551608
(size_t)(uint32_t)n = 4294967295
RichardB@callisto ~$ gcc --version
gcc (GCC) 6.3.1 20161221 (Red Hat 6.3.1-1)
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
RichardB@callisto ~$ clang --version
clang version 3.8.1 (tags/RELEASE_381/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /bin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment