Hi,

I am trying to implement the sizeof() programe

main()
{
int a,res;
a = sizeOf(a);
printf("size=5d\n",res);
}

int sizeOf( void var)
{
void *ptr;
ptr = &var;
return((char*)&ptr[1] - (char*)&ptr[0]);
}


But this is giving error ,
so plz check and correct it

what are the errors ? Your sizeOf function doesn't work because it will always return 1. The standard C/C++ sizeof() is an operator not a function and evaluates the object at compile time, not runtime. To my knowledge there is no way to duplicate it in C. In C++ I suppose you could write a template that would return the size of the object.

>main()
int main ( void ), and you need to return an integer.

>printf("size=5d\n",res);
Format modifiers always start with a %, so you need to say %5d, not 5d.

>int sizeOf( void var)
You can't have variables of type void.

>return((char*)&ptr[1] - (char*)&ptr[0]);
This is undefined behavior. You're accessing a non-array object as if it were an array and exceeding the bounds of the memory owned by that object.

Write a Macro instead of function like this.

#define sizeof(x)  ((char *)((&x)+1) - (char *)&x)

It should work.

Edited 3 Years Ago by pyTony: fixed formatting (removed unnecessary quote)

Write a Macro instead of function like this.
#define sizeof(x) ((char *)((&x)+1) - (char *)&x)
It should work .

That macro doesn't work either for the same reason Narue and I explained.

I tried it is working fine.U can try and confirm it.

That macro doesn't work either for the same reason Narue and I explained.

>I tried it is working fine.U can try and confirm it.
"It works for me" isn't a good excuse to recommend constructs with undefined behavior. Just because it works for you doesn't mean it works for everyone else, or even that it will always work for you.

Can you tell me corner case of that macro where it may fail.
I appreciate any response.

>I tried it is working fine.U can try and confirm it.
"It works for me" isn't a good excuse to recommend constructs with undefined behavior. Just because it works for you doesn't mean it works for everyone else, or even that it will always work for you.

>Can you tell me corner case of that macro where it may fail.
Unless you're taking the size of an array with at least two elements, you're trying to access memory beyond the bounds of the variable. This is undefined behavior in all cases. There is no corner case, it's never guaranteed to work the way you expect it to.

I am not accessing the memory location in that macro , I am deriving memory address alone.If you access that it will give problem .But i am not accessing that memory location.

But anyway thanks for your inputs , I found a case where it can fail.
Below is an example for that.
I am getting error duing compilation step itself.

#include <stdio.h>
#define sizeof(x) ((char *)((&x)+1) - (char *)&x)
int main(int argc, char *argv[])
{
int arr[5];
printf("size : %d \n",sizeof(&arr[0]));
return 0;
}

>Can you tell me corner case of that macro where it may fail.
Unless you're taking the size of an array with at least two elements, you're trying to access memory beyond the bounds of the variable. This is undefined behavior in all cases. There is no corner case, it's never guaranteed to work the way you expect it to.

>I am deriving memory address alone.
You're calculating an address beyond the valid boundaries of the object. Sorry, but that's undefined whether you dereference the address or not. If you use that macro, the C standard makes no guarantees about the behavior of your program.

This article has been dead for over six months. Start a new discussion instead.