Not a direct question in C

P

parag_paul

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

Jens Thoms Toerring

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
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top