By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,187 Members | 1,659 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,187 IT Pros & Developers. It's quick & easy.

implementation of the 'sizeof' operator

P: n/a
dear fellas,

this is anupam aka aloo and i have small problem

the problem ststes :--
IMPLEMENT THE 'sizeof' OPERATOR IN C. i.e find the size of any data without using 'sizeof' operator. One soluton is that if the pointer to the data is given then even if we don't know the type we cn just increment the pointer and see by how much it is incremented as the c compiler itself increments it according to its size.


But what if the the variable is gathered in a void pointer or the
element itself is given and not the data.

If you have any answers please mail it to my id
an************@rediffmail.com

-thanks Aloo

Nov 15 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Aloo said:
the problem ststes :--
IMPLEMENT THE 'sizeof' OPERATOR IN C. i.e find the size of any data
without using 'sizeof' operator. One soluton is that if the pointer to the data is given then even if we
don't know the type we cn just increment the pointer and see by how much
it is incremented as the c compiler itself increments it according to
its size.

But what if the the variable is gathered in a void pointer


Then you're stuffed - but then, so is sizeof, so that's no hardship.
or the element itself is given and not the data.


I'm not sure exactly what you mean here. Could you please clarify?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/2005
http://www.cpax.org.uk
email: rjh at above domain
Nov 15 '05 #2

P: n/a

Richard Heathfield wrote:
Aloo said:
the problem ststes :--
IMPLEMENT THE 'sizeof' OPERATOR IN C. i.e find the size of any data
without using 'sizeof' operator.
One soluton is that if the pointer to the data is given then even if we
don't know the type we cn just increment the pointer and see by how much
it is incremented as the c compiler itself increments it according to
its size.


But what if the the variable is gathered in a void pointer


Then you're stuffed - but then, so is sizeof, so that's no hardship.
or the element itself is given and not the data.


I'm not sure exactly what you mean here. Could you please clarify?

--
Richard Heathfield



well Richard,

first of all this problem was put up to me during an interview.

it stated :

let us say

that we need to implement the 'sizeof' operator without using sizeof
opearator of-course.

the data element is declared and the data inputed earlier. e.g.

....
....
int a;
scanf("%d",&a);
....
....
now at a later stage we need to know the size of the object 'a' with
the code above not visible to us. then what do we do ?

"Usenet is a strange place" - dmr 29/7/2005
http://www.cpax.org.uk
email: rjh at above domain


Nov 15 '05 #3

P: n/a
Aloo ha scritto:
Richard Heathfield wrote:
Aloo said:

the problem ststes :--
>IMPLEMENT THE 'sizeof' OPERATOR IN C. i.e find the size of any data
>without using 'sizeof' operator.

>One soluton is that if the pointer to the data is given then even if we
>don't know the type we cn just increment the pointer and see by how much
>it is incremented as the c compiler itself increments it according to
>its size.

But what if the the variable is gathered in a void pointer


Then you're stuffed - but then, so is sizeof, so that's no hardship.

or the element itself is given and not the data.


I'm not sure exactly what you mean here. Could you please clarify?

--
Richard Heathfield


well Richard,

first of all this problem was put up to me during an interview.

it stated :

let us say

that we need to implement the 'sizeof' operator without using sizeof
opearator of-course.

the data element is declared and the data inputed earlier. e.g.

...
...
int a;
scanf("%d",&a);
...
...
now at a later stage we need to know the size of the object 'a' with
the code above not visible to us. then what do we do ?


You could use two pointers, one to the variable and one to variable+1.
Then cast them to char* and subtract them.

type *pt1,*pt2;
int size;

pt1=pt2=&var;
size=((char*)++pt2-(char*)pt1);
--
Devaraja (Xdevaraja87^gmail^c0mX)
Linux Registerd User #338167
http://counter.li.org
Nov 15 '05 #4

P: n/a
Aloo said:
that we need to implement the 'sizeof' operator without using sizeof
opearator of-course.

the data element is declared and the data inputed earlier. e.g.

...
...
int a;
scanf("%d",&a);
...
...
now at a later stage we need to know the size of the object 'a' with
the code above not visible to us. then what do we do ?


You can use a couple of char * to store (char *)&a and (char *)(&a + 1).

My last reply to you was written when I was very tired, and may contain much
that is apocryphal or even plain old brain-damaged, for which I apologise.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/2005
http://www.cpax.org.uk
email: rjh at above domain
Nov 15 '05 #5

P: n/a
DevarajA <no@spam.com> writes:
Aloo ha scritto:

[...]
first of all this problem was put up to me during an interview.
it stated :
let us say
that we need to implement the 'sizeof' operator without using sizeof
opearator of-course.
the data element is declared and the data inputed earlier. e.g.
...
...
int a;
scanf("%d",&a);
...
...
now at a later stage we need to know the size of the object 'a' with
the code above not visible to us. then what do we do ?


You could use two pointers, one to the variable and one to
variable+1. Then cast them to char* and subtract them.

type *pt1,*pt2;
int size;

pt1=pt2=&var;
size=((char*)++pt2-(char*)pt1);


Gratuitous use of the ++ operator. There's also no need for either
pt1 or pt2.

size_t size = (char*)(&var+1) - (char*)(&var);

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.