Microsoft Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
Good solution.. Can you explain how this is working for an array? I thought for int a[]={4,5,6}; would give me 4 instead of 12. but it does correctly give me 12;
Can you explain why are you casting yo char* ? I know it works perfectly, but not getting the reason for typecast.
@neha
we can implement sizeof( type ) something like this.
#define sizeof_type( type, var ) { type temp; var = mysizeof( var ); }
but you will have to call it in a non conventional way.
size_t bytes;
sizeof_type( int, bytes);
sizeof_type( int *, bytes ); etc....
@jack, typecasting is done to convert to char pointer because if it is ,lets say,a float pointer,the statement
(&data+1)-&data,
would give the correct size/4 (4 i.e the size of float)
whereas
(char *)(&data+1)-(char *)(&data)
gives us the correct size/1 (1 i.e. the size of char)
In case of C++ this can be implemented with a template function, and it can work for object and type as well..
Good solution.. Can you explain how this is working for an array? I thought for int a[]={4,5,6}; would give me 4 instead of 12. but it does correctly give me 12;
suppose memory location for a[] starts with 2000, then a[]+1 will point to 2012 and not 2004.
This is because a[] is referring to the complete array and hence a[]+1 will refer to a memory location just next to the last element of the array.
So, the output with mysizeof(a) gives you 12 and not 4.
Hi,
I think they have stated the reason in the answer itself. When we talk about sizeof() operator, we are interested in getting the size of the data type/ variable in terms of no. of "BYTES". And since a char type would typically be a BYTE long, they casted it to char* (so as to measure the datalength in bytes).
Someone has asked sizeof(type). I have the answer but with a little bit modification to signature. - sizeof(type, size) - here size if the output parameter name. Refer this example-
#include<iostream>
#include<string>
using namespace std;
#define mysize(x,y) { \
x *p; \
y = (char*)(p+1) - (char*)p; \
}
int main()
{
int size = 0;
mysize(int, size);
cout << "size = " << size << endl;
}
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)
It doesn't work for types though.
For example a function that takes an array of int and the array len:
myFunc(data, mysizeof(data) / mysizeof(int));
You should also add '(' and ')' around the expanded macro because of the '-' in the middle, to allow for its use in calculations like:
size_t foo = mysizeof(var1) / mysizeof(var2);
So:
#define mysizeof(data) ((char *)(&data+1)-(char *)(&data))
Assuming size of char is one byte this can be implemented as following macro.
- techieDeep January 29, 2013#define mysizeof(data) (char *)(&data+1)-(char *)(&data)
U can also make it function , but C doesnt support fn overloading