byte writing

C

curious_one

All,
I have a struct
struct {
char a;
char b;
}some_struct;

I have a shared memory that can contain 16bit wide data, I find that
when writing an 8bit value in to char "a" the same value is
over-written onto "b".
*pSome_struct->a = 5; both a & b are 5. But if I write it
as *(unsigned short*)&pSome_struct->a = 5 << 8; the value is not
over-written into b Any clue as to why this happens ?

regards
Kurious One.
 
G

Giorgos Keramidas

I have a struct
struct {
char a;
char b;
}some_struct;

I have a shared memory that can contain 16bit wide data, I find that
when writing an 8bit value in to char "a" the same value is
over-written onto "b".

A complete but minimal program that exhibits the behavior you describe
would be very useful in tracking the cause of weirdness.
 
D

Dan Pop

In said:
I have a struct
struct {
char a;
char b;
}some_struct;

I have a shared memory that can contain 16bit wide data, I find that
when writing an 8bit value in to char "a" the same value is
over-written onto "b".
*pSome_struct->a = 5; both a & b are 5. But if I write it
as *(unsigned short*)&pSome_struct->a = 5 << 8; the value is not
over-written into b Any clue as to why this happens ?

Show us the code instead of attempting to describe what it does.
A complete but minimal program illustrating your problem.

Dan
 
E

Eric Sosman

curious_one said:
All,
I have a struct
struct {
char a;
char b;
}some_struct;

I have a shared memory that can contain 16bit wide data, I find that
when writing an 8bit value in to char "a" the same value is
over-written onto "b".
*pSome_struct->a = 5; both a & b are 5. But if I write it
as *(unsigned short*)&pSome_struct->a = 5 << 8; the value is not
over-written into b Any clue as to why this happens ?

First, "shared memory" is unclear. Do you mean a piece
of memory that is accessible both to your program and to some
other program? If so, the other program may be doing things
to it without your program's knowledge -- and any further
questions along that line should be directed to some other
newsgroups, because C has no notion of or support for "other
programs."

Second, how do you "find" that `b' changes? How do you
know it didn't hold a 5 before you stored into `a', and how
did you determine its value afterward?

Third, what does `pSome_struct' point to? Don't say
"to a `struct some_struct'," explain exactly which struct
it points to and how that struct was created.

Fourth, the subterfuge with the pointer cast produces
undefined behavior as far as the C language is concerned.
Your machine may define a useful behavior for this construct,
but the C language does not. Any further pursuit of that part
of your inquiry belongs in a forum devoted to your machine,
not in a forum about the C language.

Fifth -- ah, the heck with it. You've told the doctor
that you're experiencing joint pain, but since you haven't
revealed whether it's in your ankle, your elbow, or your
hand-rolled illegal cigarette you can't expect much in the
way of a diagnosis. Provide a short, complete, compilable
example that exhibits your problem, and maybe somebody will
be able to do something about it.
 
T

Thomas Matthews

curious_one said:
All,
I have a struct
struct {
char a;
char b;
}some_struct;

I have a shared memory that can contain 16bit wide data, I find that
when writing an 8bit value in to char "a" the same value is
over-written onto "b".

[Assumes 8 bits per byte or char]
Get a new compiler. Yours is broken.
Writing an 8-bit value on top of a char variable should not
overflow onto the next variable. However, writing a larger
bit quantity _may_ overwrite the next variable depending on
the amount of padding, if any, that the compiler inserts between
the two fields.

*pSome_struct->a = 5; both a & b are 5. But if I write it
as *(unsigned short*)&pSome_struct->a = 5 << 8; the value is not
over-written into b Any clue as to why this happens ?
For an 8-bit quantity, the expression 5 << 8 is invalid since
the quantity is being shifted a distance equal to the number
of bits.

The value 5 is represented in binary as 101, which is 3 bits.
Shifting this value left by 8 bits requires 11 bits to represent
the result (3 bits for the value of 5 plus 8 more for shifting).
Net problem: you can't store 11 bits in an 8 bit container.

You need to use a data type that contains 16 bits if your
shared memory is 16-bit wide. Use the bitwise arithmetic
operators to mask out the bits you want. Also don't assume
that two 8-bit fields in a structure will be contiguous; a
compiler is allowed to add padding between fields.

unsigned int value; // ints are at least 16-bits
value = 5 << 8;

/* Mask out lower 8 bits of the value and
* store into field 'a' of the structure.
*/
pSome_struct->a = (char)(value & 0xff);

/* Mask out the next 8 bits of the value and
* store into field 'b' of the structure.
*/
pSome_struct->b = (char)((value & 0xff00) >> 8);

I'm using the casting to inform the reader that
a value is being downsized. Some "Lint" utilities
may require this cast.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
M

Mike Wahler

curious_one said:
All,
I have a struct
struct {
char a;
char b;
}some_struct;

I have a shared memory that can contain 16bit wide data, I find that
when writing an 8bit value in to char "a" the same value is
over-written onto "b".
*pSome_struct->a = 5; both a & b are 5. But if I write it
as *(unsigned short*)&pSome_struct->a = 5 << 8; the value is not
over-written into b Any clue as to why this happens ?

You have a bug on line 37.

-Mike
 
B

Barry Schwarz

curious_one said:
All,
I have a struct
struct {
char a;
char b;
}some_struct;

I have a shared memory that can contain 16bit wide data, I find that
when writing an 8bit value in to char "a" the same value is
over-written onto "b".

[Assumes 8 bits per byte or char]
Get a new compiler. Yours is broken.
Writing an 8-bit value on top of a char variable should not
overflow onto the next variable. However, writing a larger
bit quantity _may_ overwrite the next variable depending on
the amount of padding, if any, that the compiler inserts between
the two fields.

Would you care to expand on how an assignment statement can cause data
to flow into the next variable. I can see it happening with numerous
functions like memcpy but, except for the undefined behavior if the
value doesn't fit, I don't see how it can happen with an assignment.
For an 8-bit quantity, the expression 5 << 8 is invalid since
the quantity is being shifted a distance equal to the number
of bits.
snip


<<Remove the del for email>>
 
B

Barry Schwarz

All,
I have a struct
struct {
char a;
char b;
}some_struct;

I have a shared memory that can contain 16bit wide data, I find that
when writing an 8bit value in to char "a" the same value is
over-written onto "b".
*pSome_struct->a = 5; both a & b are 5. But if I write it

Since pSome_struct->a is a char, this has got to be a syntax error
because you cannot dereference a char. Why are you executing code
that does not compile cleanly?
as *(unsigned short*)&pSome_struct->a = 5 << 8; the value is not
over-written into b Any clue as to why this happens ?

regards
Kurious One.



<<Remove the del for email>>
 

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

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top