473,395 Members | 1,677 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,395 software developers and data experts.

alignment

Hi everyone! I have a problem about the bus error.
main(void) {
union { char a[10];
int i;
} u;
int *p = (int*)&(u.a[1]);
*p = 17;
}
Somebook said that it would cause a bus error for the alignment
problem.But I find it can run well.
What is the problem? Is there something wrong with me? Thank you!

Jan 16 '06 #1
10 1658
Kies Lee said:
Hi everyone! I have a problem about the bus error.
main(void) {
union { char a[10];
int i;
} u;
int *p = (int*)&(u.a[1]);
*p = 17;
}
Somebook said that it would cause a bus error for the alignment
problem.
It could, yes. It might and it might not. The behaviour is undefined, since
p is not guaranteed to be a valid pointer (for, as you say, alignment
reasons).
But I find it can run well.


One of the possible outcomes of undefined behaviour is whatever it is that
you naively expected to happen - and of course an infinite variety of
behaviours that are minor or major variations on what you expected to
happen.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jan 16 '06 #2
I can't understand your meaning exactly. I think it will cause a bus
error, but it not. Is your meaning that the result depands on the
system or the compiler?
--
Kies

Jan 16 '06 #3
Kies Lee said:
I can't understand your meaning exactly. I think it will cause a bus
error, but it not. Is your meaning that the result depands on the
system or the compiler?


Yes. The C language does not guarantee what will happen.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jan 16 '06 #4
Kies Lee wrote:
Hi everyone! I have a problem about the bus error.
main(void) {
union { char a[10];
int i;
} u;
int *p = (int*)&(u.a[1]);
*p = 17;
}
Somebook said that it would cause a bus error for the alignment
problem.But I find it can run well.
What is the problem? Is there something wrong with me? Thank you!

This would be expected to be quite slow, on current hardware platforms
which can make it work. Not that you would notice if you did it only
once. Besides, if it does "work," how can you check that it does the
same thing on all the platforms of interest to you? I suppose, for many
little-endian platforms, it would set u.a[1] to 17, and zero out
u.a[2..4]. The effect on u.i would depend on what was there before. On
a long-ago platform I learned on, it would (efficiently) zero out
u.a[0..2], and set u.a[3] and u.i to 17, without affecting u.a[4].
What's the point, other than to check how your platform behaves in odd
situations? You could do it more efficiently, portably, and readably,
with a simple assignment and a memset().
Jan 16 '06 #5
> main(void) {
union { char a[10];
int i;
} u;
int *p = (int*)&(u.a[1]);
*p = 17;
} This would be expected to be quite slow, on current hardware platforms
which can make it work. Not that you would notice if you did it only
once. Besides, if it does "work," how can you check that it does the
same thing on all the platforms of interest to you? I suppose, for many
little-endian platforms, it would set u.a[1] to 17, and zero out
u.a[2..4]. The effect on u.i would depend on what was there before. On
a long-ago platform I learned on, it would (efficiently) zero out
u.a[0..2], and set u.a[3] and u.i to 17, without affecting u.a[4].
What's the point, other than to check how your platform behaves in odd
situations? You could do it more efficiently, portably, and readably,
with a simple assignment and a memset().


I don't want to know how it work, but I want to know why it works.
Because the address of the p(pointer) could be 3221217958(one possible
value),
it is not on the edge of the memory(cann't be divided by 4). I don't
know why ?
--
Kies

Jan 16 '06 #6
Kies Lee wrote:
main(void) {
union { char a[10];
int i;
} u;
int *p = (int*)&(u.a[1]);
*p = 17;
}
This would be expected to be quite slow, on current hardware platforms
which can make it work. Not that you would notice if you did it only
once. Besides, if it does "work," how can you check that it does the
same thing on all the platforms of interest to you? I suppose, for many
little-endian platforms, it would set u.a[1] to 17, and zero out
u.a[2..4]. The effect on u.i would depend on what was there before. On
a long-ago platform I learned on, it would (efficiently) zero out
u.a[0..2], and set u.a[3] and u.i to 17, without affecting u.a[4].
What's the point, other than to check how your platform behaves in odd
situations? You could do it more efficiently, portably, and readably,
with a simple assignment and a memset().


I don't want to know how it work, but I want to know why it works.


If you learn how it works, you'll also find out why it works -- or not,
as the case may be depending on the implementation/host system.
Because the address of the p(pointer) could be 3221217958(one possible
value),
it is not on the edge of the memory(cann't be divided by 4).
<OT>
Apart from this being entirely irrelevant, I'm wondering why the fact
that X is not divisible by 4 ensures it cannot be on the edge of memory?
</OT>
I don't
know why ?
Neither do I.
--
Kies


Cheers

Vladimir

--
My e-mail address is real, and I read it.
Jan 16 '06 #7
Ico
Kies Lee <ll****@gmail.com> wrote:
Hi everyone! I have a problem about the bus error.
main(void) {
union { char a[10];
int i;
} u;
int *p = (int*)&(u.a[1]);
*p = 17;
}
Somebook said that it would cause a bus error for the alignment
problem.But I find it can run well.
What is the problem? Is there something wrong with me? Thank you!


This actually is off-topic on comp.lang.c, since your question is highly
implementation and/or processor specific. Your `problem' is probably
caused by a CPU which has special support for handling unaligned data:
when it tries to access *p, a special function in the CPU is triggered
that handles this transparantly for you by doing multiple aligned memory
accesses, altough it is often much slower then proper aligned access.
This is called a 'trap'. If you want more information about this, find a
newsgroup or mailinglist that is dedicated for your CPU or platform and
ask your question there.

But still : what you are doing here is not valid in C, so it is not safe
to expect this to work (or not work) in any way, C does *not* guarantee
anything in this case. Just don't do it.

--
:wq
^X^Cy^K^X^C^C^C^C
Jan 16 '06 #8
Kies Lee wrote:

Please provide context for your posts, there is no guarantee that the
person reading it has seen what you are replying to. See
http://cfaj.freeshell.org/google/ for details on how to do this.

By chance, I happen to remember that this was a possibly unalligned
access to an in caused by casting a char* to an int*
I can't understand your meaning exactly. I think it will cause a bus
error, but it not.
That is one reason why you should not rely on it doing what you expect
when there is undefined behaviour.
Is your meaning that the result depands on the
system or the compiler?


Not only is it compiler and system dependant, it also depends on what
options you give some compilers, what version of the compiler and, if
the author of the implementations is feeling nasty, the day of the week.

When you invoke undefined behaviour (which is what an unaligned access
does if the system requires an aligned access) the C standard says that
*anything* is valid, and there is no requirement for the behaviour to
have any form of consistency.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Jan 16 '06 #9
"Kies Lee" <ll****@gmail.com> writes:
main(void) {
union { char a[10];
int i;
} u;
int *p = (int*)&(u.a[1]);
*p = 17;
}

This would be expected to be quite slow, on current hardware platforms
which can make it work. Not that you would notice if you did it only
once. Besides, if it does "work," how can you check that it does the
same thing on all the platforms of interest to you? I suppose, for many
little-endian platforms, it would set u.a[1] to 17, and zero out
u.a[2..4]. The effect on u.i would depend on what was there before. On
a long-ago platform I learned on, it would (efficiently) zero out
u.a[0..2], and set u.a[3] and u.i to 17, without affecting u.a[4].
What's the point, other than to check how your platform behaves in odd
situations? You could do it more efficiently, portably, and readably,
with a simple assignment and a memset().


I don't want to know how it work, but I want to know why it works.
Because the address of the p(pointer) could be 3221217958(one possible
value),
it is not on the edge of the memory(cann't be divided by 4). I don't
know why ?


As far as the C language is concerned, what you're doing is invoking
undefined behavior. The standard doesn't say, and doesn't need to
say, anything more than that. The possible effects, as the standard
joke around here goes, include demons flying out of your nose.

The reason the authors of the C standard decided to make this
undefined behavior is that the actual result can differ widely on
different systems. It would have been possible to define a consistent
behavior and enforce it across all C implementations, but that would
have caused problems for some implementations. Instead, in certain
areas, each implementation is permitted to do whatever is most
natural, and leads to the most efficient code, for that particular
implementation.

If you have an odd address and try to read a 4-byte integer at that
address, one of several things can happen:

- You can get the full 4-byte integer from that address, if the
underlying hardware happens to support it. This might be slower
than for an aligned address, or it might not.

- You can get the full 4-byte integer from that address via some
kind of software support. For example, the attempt to read the
value can cause a system trap, and the memory management code in
the operating system can respond to the trap by reading the
value one byte at a time. This is likely to be much slower than
reading from an aligned address.

- You can get a full 4-byte integer, but not the one you were
expecting. I've worked with a system that simply ignores the
low-order bits of a misaligned address.

- You can get a system trap that crashes your program.

- You can get some other result that I haven't thought of.

Most of these things depend on the underlying hardware and/or
operating system. Any requirement for some particular behavior on
unaligned accesses would slow down *aligned* accesses on some systems,
making all programs slower.

For anything that the standard says is undefined behavior, you're
*never* guaranteed anything in particular; you can't assume that
you'll get a bus error, a segmentation fault, or anything else. The
system won't catch your error; it's entirely up to you to avoid making
the error in the first place.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 16 '06 #10
On 16 Jan 2006 06:24:06 -0800, in comp.lang.c , "Kies Lee"
<ll****@gmail.com> wrote:
I can't understand your meaning exactly.


Neither can anyone else, since you didn't quote any context. Please
read the below link.

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jan 17 '06 #11

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

Similar topics

4
by: Shashi | last post by:
Can somebody explain how the byte alignment for structures work, taking the following example and considering: byte of 1 Byte word of 2 Bytes dword of 4 Bytes typedef struct { byte a; word...
10
by: j0mbolar | last post by:
for any pointer to T, does a pointer to T have different or can have different alignment requirement than a pointer to pointer to T? if so, where is the exact wording in the standard that would...
67
by: S.Tobias | last post by:
I would like to check if I understand the following excerpt correctly: 6.2.5#26 (Types): All pointers to structure types shall have the same representation and alignment requirements as each...
7
by: Earl | last post by:
Any known fixes for the wacky right-alignment bug in the WinForms datagrid (VS2003)? I've tried Ken's workaround...
13
by: aegis | last post by:
The following was mentioned by Eric Sosman from http://groups.google.com/group/comp.lang.c/msg/b696b28f59b9dac4?dmode=source "The alignment requirement for any type T must be a divisor of...
12
by: Yevgen Muntyan | last post by:
Hey, Consider the following code: #include <stdlib.h> #define MAGIC_NUMBER 64 void *my_malloc (size_t n) { char *result = malloc (n + MAGIC_NUMBER);
10
by: haomiao | last post by:
I want to implement a common list that can cantain any type of data, so I declare the list as (briefly) --------------------------------------- struct list { int data_size; int node_num;...
55
by: fishpond | last post by:
How to declare a variable guaranteed to have the strictest possible alignment? -- The defense attorney was hammering away at the plaintiff: "You claim," he jeered, "that my client came at you...
2
by: somenath | last post by:
Hi All, I have one question regarding the alignment of pointer returned by malloc. In K&R2 page number 186 one union is used to enforce the alignment as mentioned bellow. typedef long...
2
by: uamusa | last post by:
I am Dynamically generating a proposal(report) in MS Word. By default the Paragraph Alignment is "Left". For the First 6 Paragraphs I set the Alignment to "Center", and then when attempting to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.