473,796 Members | 2,625 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

int a[17]; int b = -1; /* a[17] same as b guaranteed in ANSI C? */

bq
In the code
int a[17];
int b = -1;
does ANSI C guarantee that "b" is located in memory right after "a[16]"
so that "a[17]" refers to "b"?

Thanks.
bq
Nov 14 '05
44 1974
Joona I Palaste <pa*****@cc.hel sinki.fi> spoke thus:
a[17] definitely results in UB, but a+17 doesn't.
You didn't quote the Standard ;( I know a+17 is legal, and I also
know that *(a+17) is illegal and that a+18 is also illegal...
This does not mean,
however, that a+17 will equal &b. It can, but it's not required to.


Is (a+17)==&b a legal comparison?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #11
Christopher Benson-Manica wrote:
You didn't quote the Standard ;( I know a+17 is legal, and I also
know that *(a+17) is illegal and that a+18 is also illegal...


How can a+18 be illegal?

Would (long int)a + 42 be illegal too?

Nov 14 '05 #12
bq wrote:
In the code
int a[17];
int b = -1;
does ANSI C guarantee that "b" is located in memory right after "a[16]"
so that "a[17]" refers to "b"?


Your question is related to:
http://www.eskimo.com/~scs/C-faq/q6.17.html

Nov 14 '05 #13
[Given something like "int a[17], b;" as in the subject line...]

In article <news:br******* ***@chessie.cir r.com>,
Christopher Benson-Manica <at***@nospam.c yberspace.org> asks:
Is (a+17)==&b a legal comparison?


Yes, but it the result is not useful in a strictly conforming sense.
The result is either 0 ((a+17) != &b) or 1 ((a+17) == &b), but even
if the result is 1, *(a+17) technically has undefined behavior.
In particular, a C compiler that does array bounds checking might
deliver a (compile- or run-time) array bounds error rather than
accessing the object named b.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #14
Nudge wrote:
Christopher Benson-Manica wrote:
You didn't quote the Standard ;( I know a+17 is legal, and I also
know that *(a+17) is illegal and that a+18 is also illegal...

How can a+18 be illegal?


Because the standard says so.

6.5.6 Additive operators

[#8] When an expression that has integer type is added to or
subtracted from a pointer, the result has the type of the
pointer operand.
[...]
Moreover, if the
expression P points to the last element of an array object,
the expression (P)+1 points one past the last element of the
array object, and if the expression Q points one past the
last element of an array object, the expression (Q)-1 points
to the last element of the array object. If both the
pointer operand and the result point to elements of the same
array object, or one past the last element of the array
object, the evaluation shall not produce an overflow;
otherwise, the behavior is undefined. If the result points
one past the last element of the array object, it shall not
be used as the operand of a unary * operator that is
evaluated.

In other words, you may form (but not dereference) a pointer to the area
one past the end of an array, but no farther. You cannot, in general,
create any pointer value you feel like. The behavior is undefined if you
create a "bad" pointer value - even if you don't dereference it.

Would (long int)a + 42 be illegal too?


It would be undefined if 'a' cannot be represented as a long int, or if
the addition produces overflow. Other than that, it's fine. You can
create whatever integer values you want (as long as they are in range
for the type), but the same is not true of pointers.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #15
Toni Uusitalo wrote:
oh, to ensure struct variables to be contiguous you must turn off struct
padding of course.

I just checked the standard, and I failed to see the section that talks
about turning off struct padding. Perhaps you could enlighten me?


Brian Rodenborn
Nov 14 '05 #16


"Default User" <fi********@boe ing.com.invalid > wrote in message
news:3F******** *******@boeing. com.invalid...
Toni Uusitalo wrote:
oh, to ensure struct variables to be contiguous you must turn off struct
padding of course.

I just checked the standard, and I failed to see the section that talks
about turning off struct padding. Perhaps you could enlighten me?


Well I guess that isn't defined in the standard, sorry. It's only slightly
less
"wise guy trick" to use something like
#pragma packed
than what's proposed in the subject line ;-)
No real gain, only possible pain.
with respect,
Toni Uusitalo

Nov 14 '05 #17
bq wrote:
In the code
int a[17];
int b = -1;
does ANSI C guarantee that "b" is located in memory right after "a[16]"
No.
so that "a[17]" refers to "b"?


a[17] is a bounds error.


--
Martin Ambuhl

Nov 14 '05 #18
Toni Uusitalo <to************ **@luukkudot.ko m> scribbled the following:
"Default User" <fi********@boe ing.com.invalid > wrote in message
news:3F******** *******@boeing. com.invalid...
Toni Uusitalo wrote:
> oh, to ensure struct variables to be contiguous you must turn off struct
> padding of course.
I just checked the standard, and I failed to see the section that talks
about turning off struct padding. Perhaps you could enlighten me?

Well I guess that isn't defined in the standard, sorry. It's only slightly
less
"wise guy trick" to use something like
#pragma packed
than what's proposed in the subject line ;-)
No real gain, only possible pain.


Those "wise guy tricks" are compiler-dependent and not supported by the
C standard.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"You have moved your mouse, for these changes to take effect you must shut down
and restart your computer. Do you want to restart your computer now?"
- Karri Kalpio
Nov 14 '05 #19
> How can a+18 be illegal?

Consider this example (in a normal PC setting):
you have a PC with 4Gb virtual mem. The address of a[] happens to be
0xFFFFFFBC (ie. so that a[16] is the last 4 bytes of the address space).
Then doing a+18 will cause an overflow (you will end up with 0x00000004)

This would cause so many problems that it is easiest to just say 'undefined
behaviour'. Eg: int *i; for (i = a; i < a+18; ++i) {...} would not do anything.
Would (long int)a + 42 be illegal too?


Yep, assuming you meant: (long int *)a + 42
Nov 14 '05 #20

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

Similar topics

100
7029
by: Roose | last post by:
Just to make a tangential point here, in case anyone new to C doesn't understand what all these flame wars are about. Shorthand title: "My boss would fire me if I wrote 100% ANSI C code" We are discussing whether this newsgroup should focus on 100% ANSI C or simply topics related to the C language in the real world. There is a C standard which is defined by an international committee. People who write compilers refer to this in...
9
7065
by: Olumide | last post by:
Thats the question. I know about virtual memory, and the MMU. I just wonder if array members guaranteed to be contiguous in physical memory (and if so, why). Thanks, Olumide
127
5542
by: bz800k | last post by:
Hi Does this code satisfy ANSI C syntax ? void function(void) { int a = 2; a = ({int c; c = a + 2;}); /* <<-- here !! */ printf("a=%d\n", a);
0
10604
NeoPa
by: NeoPa | last post by:
ANSI-89 v ANSI-92 Before we get into all the various types of pattern matching that can be used, there are two ANSI standards used for the main types of wildcard matching (matching zero or more characters or simply matching a single character) : ANSI-89 - Mainly used only by Jet / ACE SQL ANSI-92 - Mainly used by SQL Server and other grown-up products In the later versions of Access it is now possible to select ANSI-92 compatibility as an...
41
3171
by: jaysome | last post by:
It's been almost eight years since ISO/IEC approved ISO/IEC 9899:1999. Does anyone know if ANSI has approved it? A Google search shows arguably confusing answers as to whether ANSI has approved it. For example, on this site: http://en.wikipedia.org/wiki/C_(programming_language)#ANSI_C_and_ISO_C it says that "It was adopted as an ANSI standard in March 2000."
2
2139
by: Mara Guida | last post by:
"Each time I run my program, I get the same sequence of numbers back from rand()." The answer to question 13.17, in the c-faq is: #include <stdlib.h> #include <time.h> srand((unsigned int)time((time_t *)NULL));
0
9685
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
10239
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
10190
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
9057
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...
1
7555
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6796
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2928
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.