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

pointer conversion

guys,

I have a question regarding pointer conversion. Please look at the
following code snippet.

char *cptr;
int *iptr;

/* Some code here that initializes "iptr" */

cptr = (char *)iptr; /* Line 1
*/
cptr = cptr + sizeof(int); /* Line 2 */
iptr = (int *)cptr; /* Line 3 */

Now as per the pointer conversion rule:
Page 47: n1124.pdf

A pointer to an object or incomplete type may be converted to a
pointer to a different object or incomplete type. If the resulting
pointer is not correctly aligned57) for thepointed-to type, the
behavior is undefined. Otherwise, when converted back again, the
result shall compare equal to the original pointer.

According to above conversion rule, "Line 1" should be perfectly fine.
But, I want to know if the conversion done at "Line 3" is allowed or
not.
I have this doubt because the "cptr" has been changed.
But, it is properly aligned to point to an integer object.

Aug 31 '07 #1
13 2976

<ju**********@yahoo.co.inwrote in message
news:11**********************@i38g2000prf.googlegr oups.com...
guys,

I have a question regarding pointer conversion. Please look at the
following code snippet.

char *cptr;
int *iptr;

/* Some code here that initializes "iptr" */

cptr = (char *)iptr; /* Line 1
*/
cptr = cptr + sizeof(int); /* Line 2 */
iptr = (int *)cptr; /* Line 3 */

Now as per the pointer conversion rule:
Page 47: n1124.pdf

A pointer to an object or incomplete type may be converted to a
pointer to a different object or incomplete type. If the resulting
pointer is not correctly aligned57) for thepointed-to type, the
behavior is undefined. Otherwise, when converted back again, the
result shall compare equal to the original pointer.

According to above conversion rule, "Line 1" should be perfectly fine.
But, I want to know if the conversion done at "Line 3" is allowed or
not.
I have this doubt because the "cptr" has been changed.
But, it is properly aligned to point to an integer object.
You will be OK.
char is always 1 byte. So casting an arbitrary pointer to a char *, adding
an exact multiple of the size of the original type, and casting back is
guaranteed to preserve alignment.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Aug 31 '07 #2
>>>>"M" == Malcolm McLean <re*******@btinternet.comwrites:

MYou will be OK. char is always 1 byte. So casting an arbitrary
Mpointer to a char *, adding an exact multiple of the size of
Mthe original type, and casting back is guaranteed to preserve
Malignment.

I am not so sure about that; would you care to cite C&V, please, if
you claim that it's guaranteed by the standard?

Charlton
--
Charlton Wilbur
cw*****@chromatico.net
Aug 31 '07 #3

"Charlton Wilbur" <cw*****@chromatico.netwrote in message
news:87************@mithril.chromatico.net...
>>>>>"M" == Malcolm McLean <re*******@btinternet.comwrites:

MYou will be OK. char is always 1 byte. So casting an arbitrary
Mpointer to a char *, adding an exact multiple of the size of
Mthe original type, and casting back is guaranteed to preserve
Malignment.

I am not so sure about that; would you care to cite C&V, please, if
you claim that it's guaranteed by the standard?
Arrays have to be contiguous in memory. No padding bytes may be inserted
between items.
The rest follows from that.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Aug 31 '07 #4
Charlton Wilbur wrote:
>>>>>"M" == Malcolm McLean <re*******@btinternet.comwrites:

MYou will be OK. char is always 1 byte. So casting an arbitrary
Mpointer to a char *, adding an exact multiple of the size of
Mthe original type, and casting back is guaranteed to preserve
Malignment.

I am not so sure about that; would you care to cite C&V, please, if
you claim that it's guaranteed by the standard?
He's wrong: it's not guaranteed. Simple example:

int target = 42;
int *ptr = &target + 1; /* "an arbitrary pointer" */
ptr = (int*)((char*)ptr + sizeof *ptr); /* U.B. */

If the original pointer points at an actual object of its
type (so it's not "arbitrary"), the conversion is safe.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Aug 31 '07 #5
In article <_e******************************@comcast.com>,
Eric Sosman <es*****@ieee-dot-org.invalidwrote:
> MYou will be OK. char is always 1 byte. So casting an arbitrary
Mpointer to a char *, adding an exact multiple of the size of
Mthe original type, and casting back is guaranteed to preserve
Malignment.
>I am not so sure about that; would you care to cite C&V, please, if
you claim that it's guaranteed by the standard?
He's wrong: it's not guaranteed. Simple example:

int target = 42;
int *ptr = &target + 1; /* "an arbitrary pointer" */
ptr = (int*)((char*)ptr + sizeof *ptr); /* U.B. */

If the original pointer points at an actual object of its
type (so it's not "arbitrary"), the conversion is safe.
That seems to be excessive pedantry. He said it "preserves
alignment", not that it's legal. Presumably you would deny that
adding 2 to an int preserves it odd/even parity, because you might
choose INT_MAX.

-- Richard

--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Aug 31 '07 #6

"Richard Tobin" <ri*****@cogsci.ed.ac.ukwrote in message
news:fb***********@pc-news.cogsci.ed.ac.uk...
In article <_e******************************@comcast.com>,
Eric Sosman <es*****@ieee-dot-org.invalidwrote:
>> MYou will be OK. char is always 1 byte. So casting an arbitrary
Mpointer to a char *, adding an exact multiple of the size of
Mthe original type, and casting back is guaranteed to preserve
Malignment.
>>I am not so sure about that; would you care to cite C&V, please, if
you claim that it's guaranteed by the standard?
> He's wrong: it's not guaranteed. Simple example:

int target = 42;
int *ptr = &target + 1; /* "an arbitrary pointer" */
ptr = (int*)((char*)ptr + sizeof *ptr); /* U.B. */

If the original pointer points at an actual object of its
type (so it's not "arbitrary"), the conversion is safe.

That seems to be excessive pedantry. He said it "preserves
alignment", not that it's legal. Presumably you would deny that
adding 2 to an int preserves it odd/even parity, because you might
choose INT_MAX.
Yes, to be a successful pedant you've got to be absolutely, precisely
accurate.

Clearly if the pointer is misaligned, adding sizeof(*ptr) will preserve the
(mis) alignment.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
Aug 31 '07 #7
Richard Tobin wrote:
In article <_e******************************@comcast.com>,
Eric Sosman <es*****@ieee-dot-org.invalidwrote:
>> MYou will be OK. char is always 1 byte. So casting an arbitrary
Mpointer to a char *, adding an exact multiple of the size of
Mthe original type, and casting back is guaranteed to preserve
Malignment.
>>I am not so sure about that; would you care to cite C&V, please, if
you claim that it's guaranteed by the standard?
> He's wrong: it's not guaranteed. Simple example:

int target = 42;
int *ptr = &target + 1; /* "an arbitrary pointer" */
ptr = (int*)((char*)ptr + sizeof *ptr); /* U.B. */

If the original pointer points at an actual object of its
type (so it's not "arbitrary"), the conversion is safe.

That seems to be excessive pedantry. He said it "preserves
alignment", not that it's legal. Presumably you would deny that
adding 2 to an int preserves it odd/even parity, because you might
choose INT_MAX.
Adding two to an "arbitrary" integer need not preserve
parity, although adding two to most integers does. For the
two problematic integers you get undefined behavior, after
which assertions about what is and isn't preserved -- or
pickled -- just evaporate.

Adding sizeof *ptr to an "arbitrary" value of ptr need
not preserve alignment, although adding it to a restricted
class of values certainly does. But once U.B. occurs, the
Standard abdicates and no longer supports arguments that
formerly relied on it. It is wrong to claim that alignment
preservation is "guaranteed" in light of U.B.; "guaranteed"
by whom or by what?

BTW, "excessive pedantry" is unnecessarily redundant. ;-)

--
Eric Sosman
es*****@ieee-dot-org.invalid
Aug 31 '07 #8
In article <K6******************************@bt.com>,
Malcolm McLean <re*******@btinternet.comwrote:
>Yes, to be a successful pedant you've got to be absolutely, precisely
accurate.

Clearly if the pointer is misaligned, adding sizeof(*ptr) will preserve the
(mis) alignment.
No. If you're a real pedant, you will observe that the undefined
behaviour of creating the misaligned pointer could be that the addition
produces an aligned one.

But the example wasn't a misaligned pointer - it was that the addition
produced a pointer outside of the object.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Aug 31 '07 #9
In article <D5******************************@comcast.com>,
Eric Sosman <es*****@ieee-dot-org.invalidwrote:
BTW, "excessive pedantry" is unnecessarily redundant. ;-)
I thought the comp.lang.c view was that it's an oxymoron.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Aug 31 '07 #10

"Richard Tobin" <ri*****@cogsci.ed.ac.ukwrote in message
news:fb***********@pc-news.cogsci.ed.ac.uk...
In article <K6******************************@bt.com>,
Malcolm McLean <re*******@btinternet.comwrote:
>>Yes, to be a successful pedant you've got to be absolutely, precisely
accurate.

Clearly if the pointer is misaligned, adding sizeof(*ptr) will preserve
the
(mis) alignment.

No. If you're a real pedant, you will observe that the undefined
behaviour of creating the misaligned pointer could be that the addition
produces an aligned one.
I surrender. Yes, the misaligned pointer can only be assigned to a pointer
of that type with UB, after which any futher operations are allowed to
produce any results whatsoever.
>
But the example wasn't a misaligned pointer - it was that the addition
produced a pointer outside of the object.
That's an example of useful pedantry. I should have specified that the
operation is not safe, if the new pointer cannot be contained in the old
object.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
Aug 31 '07 #11
Eric Sosman wrote:
>
.... snip ...
>
Adding two to an "arbitrary" integer need not preserve parity,
although adding two to most integers does. For the two problematic
integers you get undefined behavior, after which assertions about
what is and isn't preserved -- or pickled -- just evaporate.
Oh? When I was in primary school:

4 had 1 bit
4 + 2 is 6, with 2 bits
6 + 2 is 8, with 1 bit

which doesn't seem to preserve parity.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Aug 31 '07 #12
In article <46**************@yahoo.com>,
CBFalconer <cb********@maineline.netwrote:
>Oh? When I was in primary school:

4 had 1 bit
4 + 2 is 6, with 2 bits
6 + 2 is 8, with 1 bit

which doesn't seem to preserve parity.
Oops. I was using the term "parity" in the mathematical sense of
whether a number is odd or even; not in the computer sense of whether
it has an odd or even number of bits set. I used the phrase "odd/even
parity" to emphasize the kind of parity I meant, but of course it does
nothing of the kind.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Aug 31 '07 #13
On 31 Aug 2007 12:24:43 -0400, Charlton Wilbur
<cw*****@chromatico.netwrote:
>>>>>"M" == Malcolm McLean <re*******@btinternet.comwrites:

MYou will be OK. char is always 1 byte. So casting an arbitrary
Mpointer to a char *, adding an exact multiple of the size of
Mthe original type, and casting back is guaranteed to preserve
Malignment.

I am not so sure about that; would you care to cite C&V, please, if
you claim that it's guaranteed by the standard?
It follows directly from paragraph 2 in section 6.5.2.1 and the first
few sentences of paragraph 8 in section 6.5.6 of n1124.

While the arithmetic is guaranteed to preserve alignment, it can still
invoke undefined behavior. If the resulting address is not within (or
just 1 past the end of) the original object, the next to last sentence
of paragraph 8 of section 6.5.6 applies.
Remove del for email
Sep 1 '07 #14

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

Similar topics

8
by: chessc4c6 | last post by:
The program below creates a char pointer call charPtr...... i then declare an char array string "Good Luck" When i assign charPtr = string, I expect an error. However, It actually runs and...
11
by: x-pander | last post by:
given the code: <file: c.c> typedef int quad_t; void w0(int *r, const quad_t *p) { *r = (*p); }
16
by: junky_fellow | last post by:
According to Section A6.6 Pointers and Integers (k & R) " A pointer to one type may be converted to a pointer to another type. The resulting pointer may cause addressing exceptions if the...
10
by: junky_fellow | last post by:
K&R say that, It is guaranteed that 1) a pointer to an object may be converted to a pointer to an object whose type requires less or equally strict storage alignment and 2) back again without...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
28
by: Wonder | last post by:
Hello, I'm confused by the pointer definition such as int *(p); It seems if the parenthesis close p, it defines only 3 integers. The star is just useless. It can be showed by my program: ...
48
by: yezi | last post by:
Hi, all: I want to record some memory pointer returned from malloc, is possible the code like below? int memo_index; int i,j; char *tmp; for (i=0;i<10;i++){
49
by: elmar | last post by:
Hi Clers, If I look at my ~200000 lines of C code programmed over the past 15 years, there is one annoying thing in this smart language, which somehow reduces the 'beauty' of the source code...
6
by: Lighter | last post by:
How to read "The lvalue-to-rvalue, array-to-pointer, and function-to- pointer standard conversionsare not applied to the left expressions"? In 5.18 Comma operator of the C++ standard, there is a...
8
by: tfelb | last post by:
Hey group! I have 2 questions. I saw functions with char *dst = (char *)src. In that case if I remember what I've learned I assign (an) (the) address of src to dst. Right? But I can assign...
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
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: 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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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.