Skip to content

Instantly share code, notes, and snippets.

@EvanMu96
Created August 18, 2020 05:54
Show Gist options
  • Save EvanMu96/f1f56036f969417301a7020aa06cc386 to your computer and use it in GitHub Desktop.
Save EvanMu96/f1f56036f969417301a7020aa06cc386 to your computer and use it in GitHub Desktop.
implement a sizeof function
// reference
// https://cs-fundamentals.com/tech-interview/c/implement-sizeof-operator-in-c.php
#include <cstdio>
#define MY_SIZEOF(object) (char*)(&object + 1) - (char*)(&object)
int main()
{
double x;
int arr[10];
printf("double var size: %d\n", MY_SIZEOF(x));
printf("double type size: %d\n", sizeof(double));
printf("int arr[10] size : %d\n", MY_SIZEOF(arr));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment