473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

byte writing

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.
Nov 14 '05 #1
9 1936
curious_one wrote:
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.

--
Er*********@sun.com

Nov 14 '05 #2
curious_one wrote:
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

Nov 14 '05 #3
In <de*************************@posting.google.com> cu*********@eudoramail.com (curious_one) writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #4

"curious_one" <cu*********@eudoramail.com> wrote in message
news:de*************************@posting.google.co m...
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
Nov 14 '05 #5
On 23 Jul 2004 07:15:32 -0700, cu*********@eudoramail.com
(curious_one) wrote:
All,
I have a struct
struct {
volatile struct {
char a;
char b;
}some_struct;

I have a shared memory that can contain 16bit wide data, I find that


Nick.

Nov 14 '05 #6
cu*********@eudoramail.com (curious_one) writes:
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.

Nov 14 '05 #7
On Fri, 23 Jul 2004 14:56:46 GMT, Thomas Matthews
<Th****************************@sbcglobal.net> wrote:
curious_one wrote:
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.

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

snip
<<Remove the del for email>>
Nov 14 '05 #8
On 23 Jul 2004 07:15:32 -0700, cu*********@eudoramail.com
(curious_one) wrote:
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>>
Nov 14 '05 #9
Nick Austin <ni***@r-e-m-o-v-e.nildram.co.uk> wrote:
On 23 Jul 2004 07:15:32 -0700, cu*********@eudoramail.com
(curious_one) wrote:
I have a struct
struct {


volatile struct {


Erm... bull.
char a;
char b;
}some_struct;

I have a shared memory that can contain 16bit wide data, I find that


Are you sure that that says "struct", and not "union"?

Richard
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Hal Vaughan | last post by:
If I have a byte and I convert it to string (String sData = new String(byte bData), then convert it back (byte bData = sData.getBytes()), will all data be intact, or do Strings have problems with...
4
by: DraguVaso | last post by:
Hi, In my application I receive a Byte Stream (Dim bytFile() As Byte) which contains a jpeg-picture, which I want to display in a picturebox. I want to display it directly from the bytfile()...
2
by: Sathyaish | last post by:
I am using MCI (winmm.dll) to read, record and playback sound. For now, I am doing this with disk files instead of realtime doing it straight from the memory. If I want to stream/relay/transmit...
3
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At...
2
by: Dunc | last post by:
Hi, I've got an HttpWebResponse object, all working fine. I'm getting the underlying stream and currently writing it to a file using the ReadToEnd() method. All happy. I want to do a bit of...
4
by: Dennis Myrén | last post by:
Hi. Is there a way to utilize the great primitive data type formatting routines available in .NET without working with strings? I want a byte directly rather than a string. I think it is...
4
by: Gidi | last post by:
Hi, i have a byte array, and i'm writing it to a file, i want to start a new line, how can i do it? (like WriteLine in StreamWriter) I'm using FileStream: FileStream fs = new...
2
by: Craig | last post by:
I have the need to write a byte of information to a specific location in a text file. eg. the file looks something like this. FYYNN Line 1 Line 2 <eof>
11
by: chaitali.pats | last post by:
Hi , I have implemented MD5 in C language . I am getting an output of 32 bits Hexadecimal number . for example : 83a80d3ca057492f0ce99ac1db8dced0 I need to convert this string same to 16...
4
by: Oleg Parashchenko | last post by:
Hello, I'm working on an unicode-aware application. I like to use "print" to debug programs, but in this case it was nightmare. The most popular result of "print" was: UnicodeDecodeError:...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.