actually, I was thinking arbitrary that i should make my own sizeof function. till date, i have made many many my own C functions which are also defined in C libraries. but i got strucked in sizeof operator desiging. can anyone give me hint how to start with this ? i am not aksing code as per rules of daniweb.

Keep in mind that you won't be able to perfectly simulate sizeof because it's a compile time operator. But you can get somewhat close for the type syntax with pointer arithmetic:

#define SIZEOF(type) (int)((char*)((type*)0) - (char*)((type*)0 + 1))

It's not strictly portable, but I've yet to see a system where this doesn't work. The trick is understanding that pointer arithmetic uses step sizes that match the size of the type of the pointer. So a pointer to int will step by the size of an int, a pointer to double with step by the size of a double, etc... If you take two of those pointers set one step apart and then cast them to pointers to char (which always have a step size of 1), subtracting will give you the size of the original type.

Edited 4 Years Ago by deceptikon

compile type operator ? So what difference is it creating ? please throw some light on that.

compile type operator ?

Compile time operator. It means that the compiler already has all of the information it needs while compiling the code. The vast majority of this information is discarded by the time the code actually runs, which puts you at a significant disadvantage when trying to simulate compile time operations.

I've been taught in my freshman class that sizeof was a function, or at least it was persistently addressed as such, and I didn't really know any better at the time. I think he is under the same boat.

sizeof is not a function, but is more of an operator. Removing the parenthesis when using it still works, which, I think, is the root of the misconception.(Except for types)

Edited 4 Years Ago by asrockw7

@deceptikon i am sorry to say that i didn't get your second line what you trying to say in that. can you explain it a little bit more ? secondly, the macro which you have defined above, can you explain me how is it working. I am felling difficult to get that. you have used 0 and casting over there. please explain a little bit more. i will be thankful to you.

@asrockw7 Do you mean it is operator and not a function? this is what i know. So did you mean that that's why we can't implement it very well as deceptikon said ? as it is nota function that's why we can't implement it very well ?

Edited 4 Years Ago by nitin1

Consider sizeof to be similar to #define. It is created during compile, not while running.

From http://en.wikipedia.org/wiki/Sizeof#Implementation
"In most cases, sizeof is a compile-time operator, which means that during compilation sizeof expressions get replaced by constant result-values."

So you can't write one. You can only write run-time code.

WaltP but when i have used macro isn't that is replaced at preprocessing time(before compilation even) ? i think i am going a little bit wrong while getting you. please make me correct. i will be thankful to you.

Read the quote again: "during compilation sizeof expressions get replaced by constant". Just like a #define. Just like a macro.

Don't get hung up on preprocessing and compile. It doesn't matter. You just can't use it.

i am sorry to say that i didn't get your second line what you trying to say in that. can you explain it a little bit more ?

It would be helpful if you quoted the line you didn't understand so that I know which one you're talking about. But assuming it's the part where I explained how information is lost during compilation, you should probably read up on how compilers work to get more details. I'm happy to describe it, but when you're not willing to take something at face value (ie. information is lost during compilation), a good book on the subject would probably be more suitable than a much more concise forum post.

secondly, the macro which you have defined above, can you explain me how is it working.

Magic.

you have used 0 and casting over there.

The use of 0 is the part that's not strictly portable, it's a trick when you need a pointer for arithmetic but have no object to point to and no intention of dereferencing the pointer.

I explained the casting already, so I'd recommend your next point of research be pointer arithmetic and how the types of pointers affect the results. You can start by reading my first post in this thread more carefully as I describe the concept of a step size.

If you're still confused and need me to elaborate, please point out what you do understand and exactly which parts confuse you as well as why they confuse you. Simply asking me to explain in toto what I've already explained will probably result in my suggesting that you read the post again.

can you tell what's that your MAGIC part is ? i am excited to know that magic of a magician here. ;)

can you tell what's that your MAGIC part is ? i am excited to know that magic of a magician here. ;)

Find two pointers of type T one step away. Convert those pointers to char* and subtract the larger one from the smaller one. The result will be the number of bytes between the two address, and thus the size of type T.

If you know how pointers work then it makes perfect sense. If you don't know how pointers work then no amount of me repeating that explanation will clarify things.

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