Re: size of pointer variables
- From: junky_fellow@xxxxxxxxxxx
- Date: 30 May 2005 22:12:26 -0700
Malcolm wrote:
> <junky_fellow@xxxxxxxxxxx> wrote
> > Can the size of pointer variables of different type may be
> > different on a particular architecture.
> > For eg.
> >
> > Can the sizeof (char *) be different from sizeof(int *) or
> > sizeof (void *) ?
> >
> Let's say we've got a machine that only adresses memory in 32-bit chunks
> (bytes).
> One obvious strategy would be to say that chars are 32 bits. This has some
> problems. For instance an image-processing routine that takes an array of
> unsigned chars as its argument would now gobble four times as much memory as
> necessary, or need to be rewritten.
> So the alternative is to make chars 8 bit, and do a bit of bit twiddling
> behind the scenes to produce the illusion of 8-bit bytes.
> But if our addresses are multiples of 32 bits, we need to tag on an extra
> two bits to tell us where the pointer points to.
> So char pointers become
> {
> address:
> offset:
> }
>
> The whole reason the machine accesses data in 32 bit bytes is for
> efficiency, so we don't want to encumber our int *s and double *s with
> similar tags. That would slow everything down.
>
> so char pointers and int pointers are now a different size.
> >
> >
> > What is the purpose of using a void pointer ? Instead of
> > declaring a pointer variable "void *", can I declare it
> > as "char *" and then later on typcast it to whatever type
> > as needed.
> >
> That was the old-fashioned way of doing things. void * is more for
> documentation, to tell the programmer that "this pointer points to memory of
> unknown type".
> unsigned char * can point to any memory. In practise
> double x;
> (unsigned char *ptr) = &x;
> double *dblptr = (double *) ptr;
>
> will work on pretty much any platform. However it might just be the case
> that the double pointers need to carry around an extra bit for some reason,
> in which case the code will break. void pointers, of course, are guaranteed
> to be able to hold any type.
Thank you to everyone for your help.
What I concluded is that since the size/representation of pointer
variables to
different data types may be different even on the same platform, we
should not
typecast one type pointer to other type except void pointer.
In some of the code pieces, I have seen that there is some pointer to a
structure. To initialize the structure with zero, the structure
pointer is typecasted to (char *) and passed as an argument to bzero().
Is this the right thing to do ?
I will give you the example.
typedef struct node {
long l;
int i ;
short s1;
short s2;
char c;
}node_t;
node_t * pstr_node;
bzero(((char *)pstr_node, sizeof (*pstr_node));
Is this legal ? Can we typecast structure pointer to char pointer ?
Let it be,
.
- Follow-Ups:
- Re: size of pointer variables
- From: Richard Bos
- Re: size of pointer variables
- From: bjrnove
- Re: size of pointer variables
- References:
- size of pointer variables
- From: junky_fellow
- Re: size of pointer variables
- From: Malcolm
- size of pointer variables
- Prev by Date: Re: typeless data [structures]
- Next by Date: multidimensional arrays and pointers
- Previous by thread: Re: size of pointer variables
- Next by thread: Re: size of pointer variables
- Index(es):
Relevant Pages
|