Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > 4-bytes or 8-bytes alignment?

Reply
Thread Tools

4-bytes or 8-bytes alignment?

 
 
mrby
Guest
Posts: n/a
 
      11-02-2004
Hi,guys,

char s1[10] = "abcde";
char s2[3];
printf("%d",s1-s2);

What should be the result on a 32-bit machine? It is 8 on my machine,
why?

Here is the output of "uname -a" on my machine:
$uname -a
SunOS virgo 5.8 Generic_108528-19 sun4u sparc SUNW,Ultra-250

--
mrby
 
Reply With Quote
 
 
 
 
Karthik Kumar
Guest
Posts: n/a
 
      11-02-2004
mrby wrote:
> Hi,guys,
>
> char s1[10] = "abcde";
> char s2[3];
> printf("%d",s1-s2);
>


How are these two variables's1' and 's2' related and
why would you want to subtract them ?

> What should be the result on a 32-bit machine? It is 8 on my machine,
> why?


The starting address of where s1 is allocated is entirely independent
of where s2 is allocated and vice-versa. So it does not matter
what your machine is. For the same machine, a different implementation
can give a diff. answer.

--
Karthik. http://akktech.blogspot.com .
' Remove _nospamplz from my email to mail me. '
 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      11-02-2004
On 1 Nov 2004 19:19:48 -0800, http://www.velocityreviews.com/forums/(E-Mail Removed) (mrby) wrote in
comp.lang.c:

> Hi,guys,
>
> char s1[10] = "abcde";
> char s2[3];
> printf("%d",s1-s2);


Comparing pointers that are not part of the same object or array, or
one past the end of the same object or array, produces undefined
behavior.

> What should be the result on a 32-bit machine?


There is no 'should', your program produces undefined behavior so the
C standard no longer places any requirements at all on what happens.

> It is 8 on my machine,
> why?


Since the behavior is undefined, there is no 'why' as far as C is
concerned.

> Here is the output of "uname -a" on my machine:
> $uname -a
> SunOS virgo 5.8 Generic_108528-19 sun4u sparc SUNW,Ultra-250


What is 'uname' and why do we care what it outputs?

If you are questioning the alignment your compiler uses in defining
data objects, and there is no guarantee that the output of your code
has anything at all to do with that, then you are asking in the wrong
place.

Data alignment in C is not defined by the language, it is specifically
implementation-defined. That means it is up to the compiler provider
to do whatever it is they want to do. It may be based on the
architecture of the underlying hardware, it may be based on the kind
of car the head programmer drives, it may be based on the fact that
the compiler writer has stock in a memory chip company, and wants to
make sure that you need to buy lots of extra memory.

If you want to know what alignment your compiler uses and why, either
contact their technical support or ask in a group that supports your
compiler. It is not a language issue.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      11-02-2004
"mrby" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Hi,guys,
>
> char s1[10] = "abcde";
> char s2[3];
> printf("%d",s1-s2);
>
> What should be the result on a 32-bit machine?


It could be anything, or nothing at all. Attempting
to compute the difference between two addresses which
are not within the same object (or in the case of
any array, one element past its end) produces undefined behavior.

Also undefined is the your use of the %d printf() specifier
for an object not of type 'int'.

Also note that simply because your two array definitions
occur sequentially in your code, doesn't mean they will
be stored anywhere near each other in memory.

>It is 8 on my machine,
> why?


Just because. Tomorrow it might be 42.

> Here is the output of "uname -a" on my machine:
> $uname -a
> SunOS virgo 5.8 Generic_108528-19 sun4u sparc SUNW,Ultra-250


Your machine doesn't matter. Here we discuss ISO standard C,
which is platform-neutral, and whose specification deems the
behavior of that code as undefined.

As far as alignment requirements which might be imposed by
your particular implementation and/or platform, you'll need
to consult your documentation. The C language doesn't control
that.


-Mike


 
Reply With Quote
 
=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=
Guest
Posts: n/a
 
      11-02-2004
mrby wrote:
> Hi,guys,
>
> char s1[10] = "abcde";
> char s2[3];
> printf("%d",s1-s2);
>
> What should be the result on a 32-bit machine? It is 8 on my machine,
> why?

Whatever the compiler thinks suitable,often influenced by hardware.

It might very well fail somehow also, as you invoke undefined behavior
with the above.

On most common machines with a flat memory layout you'll
get the diffrence in memory locations though.

The compiler/linker might decide reorder your 2 variables...

--
Nils O. Selåsdal
www.utelsystems.com
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      11-02-2004
Mike Wahler wrote:
>
> "mrby" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> > Hi,guys,
> >
> > char s1[10] = "abcde";
> > char s2[3];
> > printf("%d",s1-s2);
> >
> > What should be the result on a 32-bit machine?

>
> It could be anything, or nothing at all. Attempting
> to compute the difference between two addresses which
> are not within the same object (or in the case of
> any array, one element past its end) produces undefined behavior.
>
> Also undefined is the your use of the %d printf() specifier
> for an object not of type 'int'.


s1-s2 is an expression of type ptrdiff_t.
There are no objects of type ptrdiff_t,
refered to in the above code.

--
pete
 
Reply With Quote
 
=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=
Guest
Posts: n/a
 
      11-02-2004
pete wrote:
> Mike Wahler wrote:
>
>>"mrby" <(E-Mail Removed)> wrote in message
>>news:(E-Mail Removed) .com...
>>
>>>Hi,guys,
>>>
>>>char s1[10] = "abcde";
>>>char s2[3];
>>>printf("%d",s1-s2);
>>>
>>>What should be the result on a 32-bit machine?

>>
>>It could be anything, or nothing at all. Attempting
>>to compute the difference between two addresses which
>>are not within the same object (or in the case of
>>any array, one element past its end) produces undefined behavior.
>>
>>Also undefined is the your use of the %d printf() specifier
>>for an object not of type 'int'.

>
>
> s1-s2 is an expression of type ptrdiff_t.
> There are no objects of type ptrdiff_t,
> refered to in the above code.

Not really. Two pointers to compatible types may be subtracted
to yield ptrdiff_t, but is only valid for two pointers into the
same array, or one past it.

--
Nils O. Selåsdal
www.utelsystems.com
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      11-02-2004

"pete" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Mike Wahler wrote:
> >
> > "mrby" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed) om...
> > > Hi,guys,
> > >
> > > char s1[10] = "abcde";
> > > char s2[3];
> > > printf("%d",s1-s2);
> > >
> > > What should be the result on a 32-bit machine?

> >
> > It could be anything, or nothing at all. Attempting
> > to compute the difference between two addresses which
> > are not within the same object (or in the case of
> > any array, one element past its end) produces undefined behavior.
> >
> > Also undefined is the your use of the %d printf() specifier
> > for an object not of type 'int'.

>
> s1-s2 is an expression of type ptrdiff_t.
> There are no objects of type ptrdiff_t,
> refered to in the above code.


OK, make that "for a value not of type int".

All better now?

-Mike


 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      11-02-2004
On 1 Nov 2004 19:19:48 -0800, in comp.lang.c , (E-Mail Removed) (mrby)
wrote:

>Hi,guys,
>
>char s1[10] = "abcde";
>char s2[3];
>printf("%d",s1-s2);
>
>What should be the result on a 32-bit machine?


absolutely anything.

>It is 8 on my machine, why?


luck.

To explain more fully, there's nothing that requires adjacently declared
pointers to be stored adjacently, nor for any specific meaning to be given
to the difference between two arbitrary pointers.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>


----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments