473,406 Members | 2,390 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,406 software developers and data experts.

Address constant expression

The code is:
extern int x[1];

char *ptr1 = 8 + (char *)&x;
char *ptr2 = (char *)(8 + (unsigned)&x);

My understanding is that the ptr1 declaration is correct but the
ptr2 is not, and the compiler can reject it (even if the compiler
has a meaningful definition of integer<->pointer conversions).

The expression "&x" certainly is an address constant. C99 6.6/7
says that the initializer of a static object can be an address
constant plus or minus an integer constant, which is certainly
what we have for ptr1. It seems to be intentionally worded to
allow the initializer to not point to an lvalue in this case.

Howver, it 6.6/9 says that an address constant must be either a
null pointer, a function designator, or a pointer to an lvalue
designating an object. (It goes on to give rules for how that pointer
can be formed). Since (8 + (unsigned)&x) could be pointing off
into the wild, it does not designate an lvalue, so it is not an
address
constant, so the initialization is ill-formed.

Is this correct?

Apr 10 '07 #1
3 2318
Old Wolf said:
The code is:
extern int x[1];

char *ptr1 = 8 + (char *)&x;
char *ptr2 = (char *)(8 + (unsigned)&x);

My understanding is that the ptr1 declaration is correct
No, the ptr1 declaration is not correct unless sizeof(int) >= 8. You can
legitimately form a pointer into an object, and you can point "one
past" the tail end of an array of objects. If sizeof(int) is 8 or more,
then x will have a size of 8 (or more) bytes, and thus &x is a pointer
to an array of at least 8 bytes, and thus 8 + (char *)&x will put you
either somewhere in x or one past the end. But if int is smaller than 8
bytes, then the pointer is not legitimate.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 10 '07 #2
On Apr 10, 12:58 pm, Richard Heathfield <r...@see.sig.invalidwrote:
Old Wolf said:
The code is:
extern int x[1];
char *ptr1 = 8 + (char *)&x;
char *ptr2 = (char *)(8 + (unsigned)&x);
My understanding is that the ptr1 declaration is correct

No, the ptr1 declaration is not correct unless sizeof(int) >= 8.
Is the compiler allowed to reject ptr1's declaration at compile-time?

Apr 10 '07 #3
Old Wolf wrote:
On Apr 10, 12:58 pm, Richard Heathfield <r...@see.sig.invalidwrote:
Old Wolf said:
The code is:
extern int x[1];
char *ptr1 = 8 + (char *)&x;
char *ptr2 = (char *)(8 + (unsigned)&x);
My understanding is that the ptr1 declaration is correct
No, the ptr1 declaration is not correct unless sizeof(int) >= 8.

Is the compiler allowed to reject ptr1's declaration at compile-time?
It is, if (and only if) the initialisation would be performed during
every run of the program. Address constants are only relevant if ptr1
and ptr2 have static storage duration, and in that case, the
initialisation would always be performed, so the compiler is allowed
to reject it.

Apr 10 '07 #4

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

Similar topics

9
by: cppsks | last post by:
Taking the address of a static const resulted in a unresolved symbol. Why is that? Is the address assigned at load time? Thanks.
15
by: dandelion | last post by:
Hi, Just another question for the standards jockeys... Suppose I have an Interrupt Vector Table located at address 0x0000 (16-bit machine). I want to dump the context of the IVT, by treating...
3
by: lovecreatesbeauty | last post by:
Both `K&R C, 2nd' and `C: A reference manual, 5th' introduce the "hello, world" thing using the name "string-constant". But `ISO/IEC 9899:TC2' does not include this kind of thing in section `A.1.5...
33
by: Pietro Cerutti | last post by:
Hi group, assume the following declarations: char *func_1(void); void func_2(char **); I am allowed to do: char *c = func_1();
3
by: Dan Smithers | last post by:
What constitutes a constant-expression? I know that it is something that can be determined at compile time. I am trying to use template code and keep getting compiler errors "error: cannot...
17
by: Ben Bacarisse | last post by:
candide <toto@free.frwrites: These two statements are very different. The first one is just wrong and I am pretty sure you did not mean to suggest that. There is no object in C that is the...
7
by: John Koleszar | last post by:
Hi all, I'm porting some code that provides compile-time assertions from one compiler to another and ran across what I believe to be compliant code that won't compile using the new compiler. Not...
7
by: Hendrik Schober | last post by:
Hi, this #include <string> class test { typedef std::string::size_type size_type; static const size_type x = std::string::npos; }; doesn't compile using either VC9 ("expected constant...
56
by: Adem | last post by:
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15) says under...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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
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,...
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.