Implement Your Own sizeof

3.1

Here is an implementation.

#define my_sizeof(type) (char *)(&type+1)-(char*)(&type)
int main()
{
    double x;
    printf("%d", my_sizeof(x));
    getchar();
    return 0;
}

You can also implement using function instead of macro, but function implementation cannot be done in C as C doesn’t support function overloading and sizeof() is supposed to receive parameters of all data types.

Note that above implementation assumes that size of character is one byte.

Time Complexity: O(1)
Space Complexity: O(1)

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



3.1 Average Difficulty : 3.1/5.0
Based on 32 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.