Not a direct question in C

Discussion in 'C Programming' started by parag_paul@hotmail.com, Jun 27, 2008.

  1. Guest

    hi All,
    I happen to have a strccuture which has members in such adn such
    location from the starting byte

    struct A{
    int i1,i2,i3;
    char a;
    char b;
    int i4,i5;
    }

    Now I am dumping inofrmaiton in a file and want to recreate one
    strucutre instance from the information above,

    In the code, I do some thing like this

    int k = (address of A ) + 14 ;

    where I believe that I ma breaking the word boundary constraint.
    Though IA32 is relaxed about the above scenario , I do see that there
    is a crash with Sparc. ( I am using solaris SunOS59 , sparc64 ) .

    Now the problem happens when I do something like

    int * j = *(int*) ((address of A ) + 14 )
    int * k = *(int*)((address of A ) + 18 )

    but the Sparc does not complain when I use memcp and memcpy from the
    bytes 14 or 18 etc.

    Some thing like

    memcp ( addressof A + 18, address of A + 14 , 4 )

    Is that because the internal implementation of memcmp does a character
    by character pick and compare, which does not lead to anykind of
    corruption.
     
    , Jun 27, 2008
    #1
    1. Advertising

  2. <> wrote:
    > I happen to have a strccuture which has members in such adn such
    > location from the starting byte


    > struct A{
    > int i1,i2,i3;
    > char a;
    > char b;
    > int i4,i5;
    > }


    > Now I am dumping inofrmaiton in a file and want to recreate one
    > strucutre instance from the information above,


    > In the code, I do some thing like this


    > int k = (address of A ) + 14 ;


    I guess you mean "int *k".

    > where I believe that I ma breaking the word boundary constraint.


    Not only that, you make your program unportable (it might not
    even work anymore when you just use a different compiler) since
    the sizes of the members could be different on a different
    machine and a different compiler could insert padding byes at
    different positions. Don't you have the offsetof() macro?

    > Though IA32 is relaxed about the above scenario , I do see that there
    > is a crash with Sparc. ( I am using solaris SunOS59 , sparc64 ) .


    > Now the problem happens when I do something like


    > int * j = *(int*) ((address of A ) + 14 )
    > int * k = *(int*)((address of A ) + 18 )


    > but the Sparc does not complain when I use memcp and memcpy from the
    > bytes 14 or 18 etc.


    > Some thing like


    > memcp ( addressof A + 18, address of A + 14 , 4 )


    > Is that because the internal implementation of memcmp does a character
    > by character pick and compare, which does not lead to anykind of
    > corruption.


    All the mem*() functions must written in a way that they don't
    result in alignment problems. A simple implementation would be
    to compare (or copy etc.) char by char, just as you write. But
    since these functions are in the C standard library the imple-
    mentors can use all tricks that ar possible on the machine the
    library is targeted for (could be written in Assembler, using
    e.g some special instructions for comparing memory blocks).
    So the implementation on a Sparc might look quite different
    from the one for a PC.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Jun 27, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. .NET Follower
    Replies:
    1
    Views:
    483
    Andy Mortimer [MS]
    Feb 13, 2004
  2. =?Utf-8?B?Q2xhcw==?=
    Replies:
    0
    Views:
    1,501
    =?Utf-8?B?Q2xhcw==?=
    Nov 10, 2005
  3. shyam

    direct string manuplation not working

    shyam, Nov 29, 2005, in forum: C Programming
    Replies:
    9
    Views:
    351
    Default User
    Nov 29, 2005
  4. wuz
    Replies:
    0
    Views:
    677
  5. JTimoty
    Replies:
    3
    Views:
    305
    JTimoty
    Apr 26, 2010
Loading...

Share This Page