Re: porting problems encountered

From: RC Bryan (rcbryan_at_hotmail.com)
Date: 12/09/03


Date: 8 Dec 2003 15:56:37 -0800

If you have a simple application that does simple stuff, it is simple
to take it from one computer and run it on another.

If you are passing binary structured data over the network between
computers of different types, you can be guaranteed that you will have
trouble of one sort or another.

Programs that only move data between variables of the same types
generally do OK. Enabling all warnings on for your compiler and making
the source code compile clean on both machines, is a step in the right
direction.

When you see casting, it is an indication that something may be up.

More specific:

Tru64 compiles long variables to size 8 bytes while VMS and HP-UX
compiles them to 4 bytes (long long is required for 64 bit variables.)

If you do something underbright like:

struct MYSTRUCT
{
    short sval;
    long val;
    short sval2;
    long val2;
} mystruct, mystruct2;

Some compilers will fill in after sval and s2val with unnamed elements
to force the val and val2 elements to the proper boundaries. The size
of this structure is either 16 or 32 bytes, depending upon the size of
long (32 or 64 bits). It seems like if you run this on a 16 bit
platform, the size of the structure would be 12 bytes.
    
If you were to do something like:

memcpy((void*)&mystruct2, (void*)&mystruct, 16);

you would not get what you want because on Tru64, only the first half
would be initialized. Using:

memcpy((void*)&mystruct2, (void*)&mystruct, sizeof(mystruct2) );

will solve this problem.

Another popular problem is to do pointer manipulation with integer
arithmetic.

I had a case where they wanted a pointer to the beginning of a 16 byte
bounded region. The original code read something like:

char * sub(char *p)
{
   int myP=p;
   myP = (myP+15)& 0xfffffff0;/* add and mask back down */
   p=(char *)myP;
. . .

This will break all kinds of places. On 64 bit platforms, the
pointers are 64 bits while int is 32 bits. It really got interesting
when it got to the AS/400 with 128 bit pointers.

An example of an endian problem:

    long value=0x1234567;
    sub((short*)&value);
...

void sub(short *val)
{
   printf("val=%x", *val);
}

will print out:
val=123 on an HP and Solaris (sparc)
val=4567 on Tru64, VMS and Wintel
val=0 on 64 bit Solaris

We are accessing 16 bits of a 32 or 64 bit value:
0x0000000001234567

An example of a bounding problem:

Using the struct from before:

    p=malloc(sizeof(mystruct)*2);
    p+=sizeof(int);
    intVal=((struct MYSTRUCT*)p)->val; /* val is long*/

This will run with a warning on some platforms, crash on others and
run just fine on others. If your code was sloppy to begin with, you
may be running cases like this without knowing it.

Conclusion:

Basically, if your program only references internal variables by name
and you don't mix types, you will be fine. Another idea is to use one
of those ridiculous languages where you have to stand on your head to
avoid any possibility of having any problems.

This is a lot longer than I had intended.
Pascal said it best, "Forgive the long letter, I didn't have time to
write a short one."

Regards,
/RC Bryan

PS, I think you would be more satisfied with the responses if this
were posted on a C programming type newsgroup rather than VMS.

navin_2013@yahoo.co.in (navin_2016) wrote in message news:<268bf130.0312080455.b31cf9f@posting.google.com>...
> Hi,
> Can somone please describe the problems one would face when we try
> to port a software written in C to another platform some of them being
> endianness ,alignment,data types etc ?
>
> It would be very helpful if you could state the solution along with
> problem .Also can you please give me some pointers and links to
> documents so that porting can be easily accomplished ?
>
> Navin
> -



Relevant Pages

  • Re: [BK PATCH] USB update for 2.6.3
    ... > pointers attached to the struct device inherited from the bus ... platform-specific pointer to whatever data structure that platform needs ... pointers wouldn't have any function pointers there, ... send the line "unsubscribe linux-kernel" in ...
    (Linux-Kernel)
  • Re: Simple question, err... I think
    ... Your nodes contain no other indication of which pointers are valid, ... struct CountedObject ... int is_red; ... bool lament(char const s) ...
    (comp.programming)
  • Re: strict aliasing rules in ISO C, someone understands them ?
    ... I understand now that my interpretation of the standard was totally ... an aggregate is a struct or an array. ... struct s1 {int i; double d;}; ... Pointers are just a means of accessing the objects, ...
    (comp.lang.c)
  • Re: Array of pointers in a struct
    ... >typedef struct trieNode ... This is an array of 27 pointers. ... >And this is the code I make a new trie node, initialize and return it. ...
    (comp.lang.c)
  • Re: How are C++ objects laid out in memory ?
    ... If a C++ class or struct contains no virtual methods, ... There are no function pointers. ... >pointers which I can locate and then invoke the function pointers? ...
    (comp.lang.asm.x86)