473,769 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

addresses and integers

I've read in the standard that addresses
basically can't be interpreted as integers.
If they can, it is implementation defined
behavior. However, if they can't be viewed
as integers in any sense as far as portability
goes, what then, should one think of addresses
being composed of?
Nov 14 '05
87 3363
David R Tribble wrote:
CBFalconer wrote:
junky_fellow wrote:
can we test portably whether a pointer is word-aligned or not?
No. By storing the address of an object of <type> in a pointer we
ensure it is properly aligned for <type>. Pointers returned by
malloc are guaranteed aligned for any type. Structures and unions
are already aligned for any contained type. So there is
absolutely no need for any such test. If you apparently have such
a need your code is mis-organized.


Except, of course, when you are trying to do system-dependent things,
such as really fast memory-copy functions, in which case you will

.... snip ...
None of this code is very portable, of course, but most programs are
not written to be universally portable in the first place.


Except in c.l.c we deal with portable code, and leave those things
to system specific newsgroups.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!

Nov 14 '05 #81
In <ln************ @nuthaus.mib.or g> Keith Thompson <ks***@mib.or g> writes:
Da*****@cern.c h (Dan Pop) writes:
In <pa************ *************** @and.org> James Antill
<ja***********@ and.org> writes:
>On Wed, 01 Sep 2004 11:36:50 +0000, Dan Pop wrote:
>> You receive a pointer value from the caller and you need to check whether
>> it's properly aligned for its intended purpose or not.
>>
>> Try to write a highly optimised memcpy-like function in (as portable as
>> reasonably possible) C and see how far you can get without converting
>> both pointers to integers.
>
> I've seen people change the pointers to _pointers to integers_, to do
>"large" copies per iteration, but not convert them to integers themselves.


How do you check that the pointer is aligned for an integer access?
If it isn't, how do you align it?


Non-portably, of course.


Conversions between pointers and integers provide a fairly portable way
(not absolutely portable, of course) that is also blessed by the C
standard (no extensions are needed at all). It works on most hosted
implementations in current use and you'll have to resort to things like
AS/400 for exceptions.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #82
Da*****@cern.ch (Dan Pop) writes:
In <ln************ @nuthaus.mib.or g> Keith Thompson <ks***@mib.or g> writes:
Da*****@cern. ch (Dan Pop) writes: [...]
How do you check that the pointer is aligned for an integer access?
If it isn't, how do you align it?


Non-portably, of course.


Conversions between pointers and integers provide a fairly portable way
(not absolutely portable, of course) that is also blessed by the C
standard (no extensions are needed at all). It works on most hosted
implementations in current use and you'll have to resort to things like
AS/400 for exceptions.


Or Cray vector systems.

The conversions themselves are "blessed by the C standard"; the
semantics of using them to check alignment are not, even by
implication.

--
Keith Thompson (The_Other_Keit h) 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.
Nov 14 '05 #83
junky_fellow wrote:
can we test portably whether a pointer is word-aligned or not?
CBFalconer wrote: No.
David R Tribble wrote: Except, of course, when you are trying to do system-dependent things,
such as really fast memory-copy functions, in which case you will
... snip ...

None of this code is very portable, of course, but most programs are
not written to be universally portable in the first place.

CBFalconer wrote: Except in c.l.c we deal with portable code, and leave those things
to system specific newsgroups.

Sure, our primary focus is on standard-conforming code, but we don't
limit our discussion to such code alone. We also discuss existing practice
a great deal, and we certainly don't ignore the fact that most existing
C code is, in fact, not written to be portable.

Statements such as "most implementations provide reasonable and expected
conversions between pointers and integers" are entirely relevant to this
newsgroup [comp.std.c], even though it obviously doesn't cover all
conforming implementations .

In fact, discussions of implementations that don't fit the most common
model provide support for many of the decisions in, and limitations of,
ISO C.

-drt
[posting to news:comp.std.c]
Nov 14 '05 #84
In <ln************ @nuthaus.mib.or g> Keith Thompson <ks***@mib.or g> writes:
Da*****@cern.c h (Dan Pop) writes:
In <ln************ @nuthaus.mib.or g> Keith Thompson <ks***@mib.or g> writes:
Da*****@cern .ch (Dan Pop) writes:[...] How do you check that the pointer is aligned for an integer access?
If it isn't, how do you align it?

Non-portably, of course.
Conversions between pointers and integers provide a fairly portable way
(not absolutely portable, of course) that is also blessed by the C
standard (no extensions are needed at all). It works on most hosted
implementations in current use and you'll have to resort to things like ^^^^^^^^^^^^^^ AS/400 for exceptions.


Or Cray vector systems.


Are they still "in current use"?
The conversions themselves are "blessed by the C standard"; the
semantics of using them to check alignment are not, even by
implication.


It's a quality of implementation issue: if the conversions can be
meaningfully supported, the implementors do the right thing. And on most
hosted implementations *in current use* the conversions are as trivial as
conversions between char pointers and void pointers: a bit pattern is
given a different type.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #85
Da*****@cern.ch (Dan Pop) writes:
In <ln************ @nuthaus.mib.or g> Keith Thompson <ks***@mib.or g> writes:
Da*****@cern. ch (Dan Pop) writes:
In <ln************ @nuthaus.mib.or g> Keith Thompson <ks***@mib.or g> writes:
Da*****@cer n.ch (Dan Pop) writes:

[...]
> How do you check that the pointer is aligned for an integer access?
> If it isn't, how do you align it?

Non-portably, of course.

Conversions between pointers and integers provide a fairly portable way
(not absolutely portable, of course) that is also blessed by the C
standard (no extensions are needed at all). It works on most hosted
implementations in current use and you'll have to resort to things like ^^^^^^^^^^^^^^ AS/400 for exceptions.


Or Cray vector systems.


Are they still "in current use"?


Yes. We don't have any at SDSC, but I have accounts on a couple of
SV1s at other sites.
The conversions themselves are "blessed by the C standard"; the
semantics of using them to check alignment are not, even by
implication .


It's a quality of implementation issue: if the conversions can be
meaningfully supported, the implementors do the right thing. And on most
hosted implementations *in current use* the conversions are as trivial as
conversions between char pointers and void pointers: a bit pattern is
given a different type.


Conversions between pointers and integers are just reinterpretatio ns
of bit patterns on Cray vector systems too, which means you're going
to shoot yourself in the foot if you treat the result as if it were an
index into a byte array.

(A reminder for those just tuning in: on a Cray vector system, a
machine address points to a 64-bit word. The C compiler uses
CHAR_BIT==8 for compabitility. A char* pointer consists of a word
pointer with a byte offset in the otherwise unused high-order 3 bits;
byte accesses are handled in software.)

Certainly the vast majority of hosted implementations incurrent use
aren't AS/400s, Cray vector systems, or anything else as exotic. But
my guess is that systems on which a char* converted to an integer
can't be tested for alignment by examining the low-order bits are
roughly as common as systems on which a null pointer is something
other than all-bits-zero. I wouldn't use memset() to set a bunch of
pointers to null unless I had a very good reason, and I'd carefully
document that the code is non-portable. I wouldn't use the low-order
bits of the result of a pointer-to-integer conversion to check the
pointer's alignment unless I had a very good reason, and I'd carefully
document that the code is non-portable.

I'm not arguing against non-portable code, but it's important to
recognize that it's non-portable, even if it works on 99.9% of current
systems.

I suspect we're actually in agreement on most or all of this.

--
Keith Thompson (The_Other_Keit h) 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.
Nov 14 '05 #86
On 6 Sep 2004 15:44:57 -0700, in comp.lang.c , da***@tribble.c om (David R
Tribble) wrote:
CBFalconer wrote:
Except in c.l.c we deal with portable code, and leave those things
to system specific newsgroups.


Statements such as "most implementations provide reasonable and expected
conversions between pointers and integers" are entirely relevant to this
newsgroup [comp.std.c],


Maybe. But CBF was (and I am) reading this in comp.lang.c, where such
discussion is NOT topical. CBF even noted in his reply where he was reading
it.
--
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 =---
Nov 14 '05 #87
In <ln************ @nuthaus.mib.or g> Keith Thompson <ks***@mib.or g> writes:
Conversions between pointers and integers are just reinterpretatio ns
of bit patterns on Cray vector systems too, which means you're going
to shoot yourself in the foot if you treat the result as if it were an
index into a byte array.

(A reminder for those just tuning in: on a Cray vector system, a
machine address points to a 64-bit word. The C compiler uses
CHAR_BIT==8 for compabitility. A char* pointer consists of a word
pointer with a byte offset in the otherwise unused high-order 3 bits;
byte accesses are handled in software.)
There are two different approaches used by C implementations on Cray
vector processors. The second one has the byte offset in the 3 least
signficant bits of the pointer, thus emulating a platform with genuine
byte address support. It simplifies pointer arithmetic on character
pointers and it makes pointer to integer conversions meaningful on
that platform, too. I think Dennis Ritchie mentioned it in a comp.std.c
post.
I'm not arguing against non-portable code, but it's important to
recognize that it's non-portable, even if it works on 99.9% of current
systems.


And, since I wrote "as portable as reasonably possible" upthread, I fail
to see your point: I neither claimed nor implied that such code is
absolutely portable.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #88

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

Similar topics

14
5492
by: pras.vaidya | last post by:
hi, please help me with this problem : - how to swap two addresses .For eg variable i is located at 2000 and j at 3000 . Now i call swap function . Result should be that i should be now having address 3000 and j should be having 2000. Is it the normal swapping of two number ? if no please help me out. Thanks in advance
13
17189
by: In a little while | last post by:
thanks
0
9586
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10210
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10043
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9990
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9861
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8869
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3956
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2814
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.