implementation of the 'sizeof' operator

A

Aloo

dear fellas,

this is anupam aka aloo and i have small problem

the problem ststes :--

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
(e-mail address removed)

-thanks Aloo
 
R

Richard Heathfield

Aloo said:
the problem ststes :--


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?
 
A

Aloo

Richard said:
Aloo said:


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


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




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 ?
 
D

DevarajA

Aloo ha scritto:
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);
 
R

Richard Heathfield

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.
 
K

Keith Thompson

DevarajA said:
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);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,690
Members
48,796
Latest member
Greg L.

Latest Threads

Top