85-sizeof.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* -*-comment-start: "//";comment-end:""-*-
  2. * GNU Mes --- Maxwell Equations of Software
  3. * Copyright © 2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  4. *
  5. * This file is part of GNU Mes.
  6. *
  7. * GNU Mes is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * GNU Mes is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. struct foo
  21. {
  22. int length;
  23. char buf[16];
  24. };
  25. struct bar
  26. {
  27. struct
  28. {
  29. int x;
  30. int y;
  31. int z;
  32. };
  33. };
  34. #if __i386__ || __arm__
  35. #define ptr_size 4
  36. #define foo_size 20
  37. #define bar_size 12
  38. #elif __x86_64__
  39. #define ptr_size 8
  40. #define foo_size 24
  41. #define bar_size 12
  42. #endif
  43. int
  44. main ()
  45. {
  46. char **p;
  47. if (sizeof (*p) != ptr_size)
  48. return 1;
  49. if (sizeof (**p) != 1)
  50. return 2;
  51. oputs ("size: ");
  52. oputs (itoa (sizeof (struct foo)));
  53. oputs ("\n");
  54. if (sizeof (struct foo) != 20)
  55. return 3;
  56. struct foo f;
  57. if (sizeof f != 20)
  58. return 4;
  59. if (sizeof (struct bar) != 12)
  60. return 5;
  61. return 0;
  62. }