Wednesday, April 16, 2014

Object Size Tweak

Someone asked me a few days ago about an interview question. Make a function that returns the integer containing the size allocated to that object passed in that function as a parameter. In simple language, put same functionality as sizeof operator present in C++.

So I answered him to use character pointer to look through the number of bytes allocated.
This function simply does pointer arithmetic, and then simply subtracting the starting address of object from one-block-ahead pointer gives the exact number of bytes.

Happy learning!

No comments :

Post a Comment