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

Does pointer assignment preserve bit pattern?

The more I think about pointers, the more my head hurts.

From what I can tell, nothing in the standard guarantees that pointer
assignment preserves bit patterns.

Section 6.5.16.1, paragraph 2, states:

In simple assignment (=), the value of the right operand is
converted to the type of the assignment expression and replaces
the value stored in the object designated by the left operand.

Since we know that multiple bit patterns can represent the same "value",
it's possible that the following function could return a non-zero value.

int ptr_test(void)
{
char c;
void *p, *q;

p = &c;
q = p;

return memcmp(&p, &q, sizeof(void *));
}

Since I know you'll ask, I was thinking about implementing some simple
functions modeled after Win32's HeapAlloc, HeapFree, etc. A "heap" in
this case would just be a binary search tree, using the pointers
returned by malloc (or calloc or realloc) as keys.

This won't work, though, if I can't somehow compare pointers to
different objects, which rules out a simple comparison.

Am I missing something?

Thanks!

--
================================================== ======================
Ian Pilcher pi******@attbi.com
================================================== ======================

Nov 13 '05 #1
6 2021
Ian Pilcher <pi******@attbi.com> wrote:
The more I think about pointers, the more my head hurts.

From what I can tell, nothing in the standard guarantees that pointer
assignment preserves bit patterns.

Section 6.5.16.1, paragraph 2, states:

In simple assignment (=), the value of the right operand is
converted to the type of the assignment expression and replaces
the value stored in the object designated by the left operand.

Since we know that multiple bit patterns can represent the same "value",
it's possible that the following function could return a non-zero value.

int ptr_test(void)
{
char c;
void *p, *q;

p = &c;
q = p;

return memcmp(&p, &q, sizeof(void *));
}

Since I know you'll ask, I was thinking about implementing some simple
functions modeled after Win32's HeapAlloc, HeapFree, etc. A "heap" in
this case would just be a binary search tree, using the pointers
returned by malloc (or calloc or realloc) as keys.

This won't work, though, if I can't somehow compare pointers to
different objects, which rules out a simple comparison.

Am I missing something?


You can compare pointers to different objects - a pointer to an object
is guaranteed to compare unequal to a pointer to any other object, and
unequal to NULL.

What you can't do is use (in comparisons, or otherwise) pointer values
that are indeterminate - ones that *don't* point to an object.

You can almost certainly do what you need by setting any pointer object
that holds an indeterminate value (eg. after its value has been freed)
to NULL.

- Kevin.

Nov 13 '05 #2
Kevin Easton wrote:
You can compare pointers to different objects - a pointer to an object
is guaranteed to compare unequal to a pointer to any other object, and
unequal to NULL.


Yes, but I need to establish a consistent sorting order, and using
relational operators with pointers to different objects is UB.

This led me to consider using memcmp, but memcmp won't necessarily be
reliable if pointer assignment isn't guaranteed to preserve bit
patterns.

--
================================================== ======================
Ian Pilcher pi******@attbi.com
================================================== ======================

Nov 13 '05 #3
Ian Pilcher wrote:
The more I think about pointers, the more my head hurts.

From what I can tell, nothing in the standard guarantees that pointer
assignment preserves bit patterns.

Section 6.5.16.1, paragraph 2, states:

In simple assignment (=), the value of the right operand is
converted to the type of the assignment expression and replaces
the value stored in the object designated by the left operand.

Since we know that multiple bit patterns can represent the same "value",
it's possible that the following function could return a non-zero value.

int ptr_test(void) {
char c;
void *p, *q;

p = &c;
q = p;

return memcmp(&p, &q, sizeof(void *));
}

Since I know you'll ask, I was thinking about implementing some simple
functions modeled after Win32's HeapAlloc, HeapFree, etc. A "heap" in
this case would just be a binary search tree, using the pointers
returned by malloc (or calloc or realloc) as keys.

This won't work, though, if I can't somehow compare pointers to
different objects, which rules out a simple comparison.

Am I missing something?

Thanks!


If q = p then (q == p) is true and (q != p) if false.
There are also two zeros in almost all floating-point representations.

(-0.0 == +0.0) is true.

Nov 13 '05 #4
[Crossposted - added comp.lang.c++ to the group list]
On Mon, 04 Aug 2003 01:12:21 GMT, Ian Pilcher <pi******@attbi.com> wrote:
Kevin Easton wrote:
You can compare pointers to different objects - a pointer to an object
is guaranteed to compare unequal to a pointer to any other object, and
unequal to NULL.


Yes, but I need to establish a consistent sorting order, and using
relational operators with pointers to different objects is UB.


In C, yes, as far as I know.

In C++ that (you can't compare pointers in general wrt. order) is also
true at the language level, but not at the standard library level.

So perhaps -- switch to C++?

Nov 13 '05 #5
In <kj********************************@4ax.com> Jack Klein <ja*******@spamcop.net> writes:
Pointer assignment preserves all _value_ bit values, assuming the
assignment is between pointers to the same type.


Chapter and verse, please. Where does the standard say that each
address is guaranteed to have a unique representation (in terms of value
bits)?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #6
In <TmhXa.57430$uu5.6008@sccrnsc04> Ian Pilcher <pi******@attbi.com> writes:
The more I think about pointers, the more my head hurts.

From what I can tell, nothing in the standard guarantees that pointer
assignment preserves bit patterns.


This is correct. The same address (pointer value) can have multiple
representations (as many as 4096 in the case of the 8086).

Then again, pointers can have padding bits, although the standard
doesn't explicitly say so. Pointer assignment need not replicate them.

OTOH, the standard guarantees that copying the representation also copies
the value, so, if this is important to you, you can use memcpy instead of
pointer assignment.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #7

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

Similar topics

3
by: Teis Draiby | last post by:
I want to write a base class that includes a member function that creates an instance of a derrived class and returns a pointer to it. Problem: The derived class definition has to follow the base...
3
by: Mr Fish | last post by:
I don't understand why this crashes in the dtor of Info. Can someone explain to me what I'm missing? Thanks //--------------------------------------------------------- struct Info { int*...
15
by: Chris Readle | last post by:
Hi all, Somewhat new to C and I'm getting the following error from my latest code. Here's the warning I'm getting: chris_readle_project3_assignment3.c: In function `main':...
11
by: Vinod Patel | last post by:
I have a piece of code : - void *data; ...... /* data initialized */ ...... struct known_struct *var = (struct known_struct*) data; /*typecasting*/ How is this different from simple...
16
by: aegis | last post by:
Given the following: int a = 10; int *p; void *p1; unsigned char *p2; p = &a;
7
by: Tom | last post by:
Hi Is this a conditional ? what is the structure of the statement? ch Tom
1
by: Hemant Mohan | last post by:
Consider the following program snipet: <snip> typedef struct { unsigned char a ; unsigned char b ; unsigned char c ;
52
by: Julie | last post by:
I'm supporting an application at work. Below are some code segments that I can't understand how they work. First let me say, I would never code this standard. I'm just really creeped out that it...
21
by: Chen Shusheng | last post by:
Hello, I have a small piece of code that I want to directly assign a segment of memory to a pointer. But complier tell me wrong. Pls you help.Codes below: ------------------ int * p;...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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
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
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: 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...

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.