473,785 Members | 2,557 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 #1
44 1972
In article <6c************ **************@ posting.google. com>,
bq <fo**********@y ahoo.com> 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"?


I have not seen reference in the C standard to any guarrantee of this
sort.

Additionally, testing the addresses of your a and b on a recent
version of gcc, it turns out that b comes just _before_ a in the
memory address space. Other compilers may behave differently.

--
Rouben Rostamian
Nov 14 '05 #2
fo**********@ya hoo.com (bq) wrote in
news:6c******** *************** ***@posting.goo gle.com:
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"?


No.

--
- Mark ->
--
Nov 14 '05 #3
On 18 Dec 2003 08:51:00 -0800, fo**********@ya hoo.com (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"?

Thanks.
bq


No. In fact, "Hell, no!" Don't do that.

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 14 '05 #4
a and b are separate variables, compiler can locate them anywhere. only way
to ensure contiguous memory block is to use array (like your a),
struct/union or course allocate some block of memory.

with respect,
Toni Uusitalo

"bq" <fo**********@y ahoo.com> wrote in message
news:6c******** *************** ***@posting.goo gle.com...
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 #5
On 18 Dec 2003 08:51:00 -0800, fo**********@ya hoo.com (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]"
Only if they are in a struct, and then there still might be some padding
between a and b.

so that "a[17]" refers to "b"?


Not only no, but "hell, no". There is no possible legitimate reason to
want to do that.

--
#include <standard.discl aimer>
_
Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
Per the FCA, this address may not be added to any commercial mail list
Nov 14 '05 #6


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"?

Thanks.
bq


No

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94 (206)544-5225
Nov 14 '05 #7

"Toni Uusitalo" <to************ **@luukkudot.ko m> wrote in message
news:sA******** ********@reader 1.news.jippii.n et...
a and b are separate variables, compiler can locate them anywhere. only way to ensure contiguous memory block is to use array (like your a),
struct/union or course allocate some block of memory.


oh, to ensure struct variables to be contiguous you must turn off struct
padding of course.
This whole thing might make sense if you think it from the memory allocation
perspective i.e. if you for example malloc 2 different blocks of memory, for
memory management they're totally separate blocks (they might be contiguous
or they might not) etc. just as separate variables.

with respect,
Toni Uusitalo
Nov 14 '05 #8
bq <fo**********@y ahoo.com> spoke thus:
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"?


a[17] results in UB, I imagine. I'd be grateful if someone would
quote the Standard on this issue, however...

--
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 #9
Christopher Benson-Manica <at***@nospam.c yberspace.org> scribbled the following:
bq <fo**********@y ahoo.com> spoke thus:
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"?
a[17] results in UB, I imagine. I'd be grateful if someone would
quote the Standard on this issue, however...


a[17] definitely results in UB, but a+17 doesn't. This does not mean,
however, that a+17 will equal &b. It can, but it's not required to.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Last year he disrespected me - and then he showed lack of respect."
- Anthony Mason
Nov 14 '05 #10

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

Similar topics

100
7028
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
10601
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
3166
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
2138
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
9647
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
10356
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
10161
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
10098
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
8986
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
7506
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
6743
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();...
2
3662
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2890
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.