sizeof(<Insert_Type_Here>*) = constant? 
Author Message
 sizeof(<Insert_Type_Here>*) = constant?

I'm writing some code that uses unions to handle multiple data types,
and I've had to make the assumption that:

sizeof(char*) = sizeof(long*) = sizeof(float*) = sizeof(double*), etc.

In other words, I'm relying on sizeof(pointer-to-X) to be the same,
regardless of what X is.  This happens to be true for all the platforms
I code for, but I've never thought about whether this might not be an
altogether safe assumption.  Does assuming this severely limit
portability, or are architectures for which the assumption is *not* true
so rare that it's a non-issue?

-Dave

--
*-*-*-*-*-*-*-*-*-*-*-*-*
Dave Wolfe
gForce Technologies Inc.

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Fri, 23 Aug 2002 03:00:00 GMT  
 sizeof(<Insert_Type_Here>*) = constant?

comp.lang.c:

Quote:
> I'm writing some code that uses unions to handle multiple data types,
> and I've had to make the assumption that:

> sizeof(char*) = sizeof(long*) = sizeof(float*) = sizeof(double*), etc.

> In other words, I'm relying on sizeof(pointer-to-X) to be the same,
> regardless of what X is.  This happens to be true for all the platforms
> I code for, but I've never thought about whether this might not be an
> altogether safe assumption.  Does assuming this severely limit
> portability, or are architectures for which the assumption is *not* true
> so rare that it's a non-issue?

> -Dave

Not portable.  The only guarantees the standard gives you are:

1.  The size of all pointers to any structure types are the same.

2.  A pointer to any type of object can be converted to a pointer to
void and converted back to a pointer to the original object type.  It
will still point to the same object.

3.  Pointer to char has exactly the same size and representation as
pointer to void.

The safest thing to do is use pointer to void if you want maximum
portability.

Jack Klein
--
Home: http://jackklein.home.att.net



Fri, 23 Aug 2002 03:00:00 GMT  
 sizeof(<Insert_Type_Here>*) = constant?

Quote:

>I'm writing some code that uses unions to handle multiple data types,
>and I've had to make the assumption that:

>sizeof(char*) = sizeof(long*) = sizeof(float*) = sizeof(double*), etc.

>In other words, I'm relying on sizeof(pointer-to-X) to be the same,
>regardless of what X is.  This happens to be true for all the platforms
>I code for, but I've never thought about whether this might not be an
>altogether safe assumption.  Does assuming this severely limit
>portability, or are architectures for which the assumption is *not* true
>so rare that it's a non-issue?

It's not clear why you need to make this assumption at all. Just make sure that
you access the value of the union through the same member that was used to
store it.


Fri, 23 Aug 2002 03:00:00 GMT  
 sizeof(<Insert_Type_Here>*) = constant?

Quote:

>> I'm writing some code that uses unions to handle multiple data types,
>> and I've had to make the assumption that:

>> sizeof(char*) = sizeof(long*) = sizeof(float*) = sizeof(double*)...

<-SNIP->



Quote:
> It's not clear why you need to make this assumption at all. Just make
> sure that you access the value of the union through the same member
> that was used to store it.

Yes!  Thank you.  I've never coded something quite like this before, so
I wasn't thinking through it clearly.  I haven't worked out the syntax
yet, but I suppose I'll end up with something like the following:

int update_import( IO_t* dest, IO_t* src ) {

     switch( dest->Type ) {

          case CHAR:
               switch( src->Type ) {
                    case CHAR:    *dest->charptr = (char)*src->charptr;
                                   break;
                    case UCHAR:   *dest->charptr = (char)*src->ucharptr;
                                   break;
                    case SHORT:   *dest->charptr = (char)*src->shortptr;
                                   break;
                                  .
                                  .
               }
               break;

          case UCHAR:
               switch( src->Type ) {
                    case CHAR:   *dest->ucharptr = (uchar)*src->charptr;
                                   break;
                                  .
                                  .
               }
               break;

               .
               .
               .

          default:
                Exec->Message( WARNING, "DATA MISMATCH, src = %d,
                    dest = %d", (short)src->Type, (short)(dest->Type));
               break;
     }

Quote:
}

As you point out, there's no assumption about pointer sizes there.  I'm
not sure what I was thinking.  Thanks for the heads-up...

- Dave

Sent via Deja.com http://www.deja.com/
Before you buy.



Fri, 23 Aug 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. <<<<<<<Parsing help, please>>>>>>>>

2. File Format conversion, ascii freeform -->.csv <-->.wk1<-->dbf<-->?HELP

3. <<<>>>Need C code advice with functions and sorting.<<<>>>

4. <><><>HELP<><><> PCMCIA Motorola Montana 33.6

5. >>>Windows Service<<<

6. Usage of sizeof(<dynamic array>)

7. proposal: <basic.h>, <pascal.h>, <fortran.h>, <cobol.h>

8. <<<< C Grammar for yacc needed >>>>

9. - - - = = =<><> W e b - U S A <><>= = = - - -

10. >>>> * Get keypress * <<<<

11. sizeof() question >>> :)

12. <Ctrl>+<Key>

 

 
Powered by phpBB® Forum Software