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

Word alignment - Why doesn't this crash?

Hi,

I try to confirm my understanding of word alignment by writing a
program that screw this particular matter up.

I run linux on:
Intel(R) Core(TM)2 CPU T7600 @ 2.33GHz

my compiler is:
gcc version 4.1.2

I compile with no arguments other than source file name and target
name (-o ptr_test).

I write this program but it doesn't crash. Is it because I'm testing
the wrong thing (my understanding is wrong) or is it because my
environment is very forgiving?

#include <stdlib.h>
#include <stdio.h>

int main( int argc, char** argv )
{
char* ptr = malloc( 32 );

ptr++;

*(int*)ptr = 20;

printf("ptr: %d\n", *ptr );

*(int*)ptr = *(int*)ptr + 20;
printf("ptr: %d\n", *ptr );

return(0);
}
Thanks
/Sune
Mar 31 '08 #1
4 1780
ol***********@gmail.com wrote:
I try to confirm my understanding of word alignment by writing a
program that screw this particular matter up.

I run linux on:
Intel(R) Core(TM)2 CPU T7600 @ 2.33GHz

my compiler is:
gcc version 4.1.2

I compile with no arguments other than source file name and target
name (-o ptr_test).

I write this program but it doesn't crash. Is it because I'm testing
the wrong thing (my understanding is wrong) or is it because my
environment is very forgiving?
Your program exhibits undefined behaviour. The answer to FAQ 11.35 is
relevant (and much better written than anything I write):

A compiler may do anything it likes when faced with undefined
behavior (and, within limits, with implementation-defined and
unspecified behavior), including doing what you expect. It's unwise
to depend on it, though.

Here is another way of looking at it, due to Roger Miller:

``Somebody told me that in basketball you can't hold the ball and
run. I got a basketball and tried it and it worked just fine. He
obviously didn't understand basketball.''

It seems your implementation doesn't mind that particular alignment
issue. C doesn't say that alignment issues /will/ happen, only that they
/might/.

Philip

Mar 31 '08 #2
ol***********@gmail.com wrote:
>
I write this program but it doesn't crash. Is it because I'm testing
the wrong thing (my understanding is wrong) or is it because my
environment is very forgiving?
There's no such thing as some mandatory "word alignment". Alignment
requirements are type-specific and platform-specific. It is quite
possible that in your platform objects of type 'int' can be placed in
memory in any way you want, i.e. there are no alignment restrictions as all.

Note also, that the real "platform" you are working with is really the
platform provided (emulated) by the compiler. It doesn't necessarily
have to inherit all (or any) requirements from the underlying hardware
platform. Normally it does for performance reasons, but it is not
impossible to implement a C compiler that wouldn't impose any alignment
restrictions on any of its data types, even though the underlying
hardware platform does have such restriction for "corresponding"
hardware data types.

--
Best regards,
Andrey Tarasevich
Mar 31 '08 #3
On 1 Apr, 03:53, Andrey Tarasevich <andreytarasev...@hotmail.com>
wrote:
ollemblomg...@gmail.com wrote:
I write this program but it doesn't crash. Is it because I'm testing
the wrong thing (my understanding is wrong) or is it because my
environment is very forgiving?

There's no such thing as some mandatory "word alignment". Alignment
requirements are type-specific and platform-specific. It is quite
possible that in your platform objects of type 'int' can be placed in
memory in any way you want, i.e. there are no alignment restrictions as all.

Note also, that the real "platform" you are working with is really the
platform provided (emulated) by the compiler. It doesn't necessarily
have to inherit all (or any) requirements from the underlying hardware
platform. Normally it does for performance reasons, but it is not
impossible to implement a C compiler that wouldn't impose any alignment
restrictions on any of its data types, even though the underlying
hardware platform does have such restriction for "corresponding"
hardware data types.

--
Best regards,
Andrey Tarasevich
Hi,

ok your answer set my mind straight. C impose very little requirements
and governance when it comes to word alignment. It ultimately depends
on the compiler.

Thanks
/Olle
Apr 1 '08 #4
<ol***********@gmail.comwrote in message
news:c2**********************************@c19g2000 prf.googlegroups.com...
I try to confirm my understanding of word alignment by writing a
program that screw this particular matter up.

I run linux on:
Intel(R) Core(TM)2 CPU T7600 @ 2.33GHz

my compiler is:
gcc version 4.1.2

I compile with no arguments other than source file name and target
name (-o ptr_test).

I write this program but it doesn't crash. Is it because I'm testing
the wrong thing (my understanding is wrong) or is it because my
environment is very forgiving?
There is no _requirement_ that your program crash when you perform an
unaligned access. That is merely one possible result.

x86 happens to be rather forgiving in that respect: unaligned accesses can
be significantly slower, but they do not cause a crash.

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking

Apr 1 '08 #5

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

Similar topics

13
by: Bryan Parkoff | last post by:
I have two variables: "char A" and "short B". I can be able to convert from A to B using explicit case conversion with no problem like "B = short (A);". Right now, I have two variables: "char T"...
3
by: aneesh | last post by:
Hi All, Im having a program int main() { char* p = "hello"; printf("%d",*((int*)p)); } char* is acutally non aligned and we r casting it to int* and
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...
2
by: Mikey | last post by:
Sample VB .NET source code to create mailing labels or customized letters using MS Word MailMerge This VB .NET source code will start MS Word and call methods and set properties in MS Word to...
5
by: Mason | last post by:
I'm having some problems converting VBA for Word 2000 to code that VB.Net understands. I recorded a macro in Word to add numbering (a. b. c.) to my paragraphs. I managed to translate quite a bit...
3
by: Anne Richmond | last post by:
The following code creates a word document. How do I add a footer to each page containing the date and page number? Dim wrdApp As Word.Application Dim wrdDoc As Word.Document Dim...
0
by: Nitin | last post by:
How can I create and use Tables in Microsoft word document through VB .net? I've been using the following code for printing an envelope using paragraphs. Tables have to be used for better...
66
by: Why Tea | last post by:
typedef struct some_struct { int i; short k, int m; char s; } some_struct_t; Assuming 16 bit or 32-bit alignment, can I assume that s always gets 4 or 8 bytes of allocation due to padding
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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...

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.