Link to home
Start Free TrialLog in
Avatar of thambuzk
thambuzk

asked on

Implenting sizeOf() operator in c

Can any one help me in implementing the same sizeof() fuction in C.It should take the input like of
Sizeof(2)---should return 2 bytes(Integer)
sizeof(2.5)--should return 8 bytes(Double)
sizeof(2.5f)--should return 4 bytes(float)
and also it should take variable names declared in the program.
like
sizeof(x)
if x is an integer or float or a struct variable it should give me the proper results.
SOLUTION
Avatar of nrohan
nrohan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of thambuzk
thambuzk

ASKER

1)see i am not intrested in the build in sizeof in c,i want my own implementation for that.
2)if condition won't work out with program variables
3)sizeof function accepts constants too.

wht i think is if we can get into the actual memory area  and declare a pointer for the same, increment the pointer if it is integer then it will increment by 2 bytes and if it is float then by 4bytes and so on...But the problem comes i don't know wht the user is gona input integer,or float or struct variable so i have to use a void pointer,which won't work out.
What are you trying to accomplish? Where you write "user input", do you mean input from the command line or stdin? Or do you intend to call your sizeof function at arbitrary points in your program?

If you are trying to determine size of types for user (CLI) input, then my suggestion is valid for that use. You parse the input to determine the type, and then call the built-in sizeof for that type. Note that you are converting an input string to its numeric equivalent. You only need to ensure that the input buffer is sufficiently sized.

--Eric
By constant, do you mean a constant in input text or a binary pattern in memory?  If the user input is

"Sizeof(2.0f)"

just look for the . If there is one, the answer is 8.  If an f follows the answer is 4.  If neither then the answer is 2.

If it is a binary pattern in memory then you'll have a hard time distinguishing between a char and an unsigned long long.  At run-time, all the program sees is just a bunch of binary patterns.  If you take the address of the first global variable, then you can get at all the other global stuff but you can't get to the items on the stack.  You'd probably have to examine the varargs macro closely to figure that one out.  Note that it is different for every compiler.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry...
#define sizeof(x) (&x+1) - &x

This will work only for variables
You can't roll your own sizeof(). sizeof() can take an arbitrary expression of arbitrary type, so it certainly can't be a C function.  The pointer arithmetic version won't even work for all variables; what if the variable has the register storage class? For that matter, a type is a valid operand of sizeof; a macro can't distinguish between the name of a (possibly typedef) type and the name of a variable; it doesn't have access to the required symbol table information.
Avatar of sunnycoder
No comment has been added lately and this question is therefore classified abandoned.

If asker wishes to close the question, then refer to
https://www.experts-exchange.com/help/closing.jsp

Otherwise, I will leave a recommendation in the Cleanup topic area that this question is:
PAQed with A grade and 30:40:30 split between nrohan:ewest:helloprak

Please leave any comments here within the next seven days. It is assumed that any participant not responding to this request is no longer interested in its final disposition.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Sunny
EE Cleanup Volunteer