473,796 Members | 2,586 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 1975
ark

"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


Since a and b belong (whatever th official terminology) to different
categories -- uninitialized and initialized data respectively, a compiler
may have sound reasons to put them in different, e.g., named segments, to
optimize the startup code, for instance.

However, if both are initialized (int a=1,b=-1;) or both are not (int a,
b;), both are arrays or both are not (and both have identical types and
qualifiers) it is rather hard to imagine reasons why a compiler would prefer
an allocation different from the order of occurrence. (For the heck of it,
it could be the opposite order, but then it would break some existing code,
would it not? :)

Now, the standard quoting in this thread... undefined behavior and such...
If a function receives an int *, it has no idea where it came from, so it
(function) must allow adding any int to it (pointer). (As is usual in C, it
is the programmer's job to care about overflows.) So this "undefinedn ess"
looks like a lip service.

I wonder if I am correct in this assessment and also if indeed for all
practical purposes (&a+1==&b) holds true for statements <qual><type>a ;
<qual><type>b ;?

Thanks
- Ark

Nov 14 '05 #21
"ark" <ar****@comcast .net> writes:
"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
Since a and b belong (whatever th official terminology) to different
categories -- uninitialized and initialized data respectively, a compiler
may have sound reasons to put them in different, e.g., named segments, to
optimize the startup code, for instance.

However, if both are initialized (int a=1,b=-1;) or both are not (int a,
b;), both are arrays or both are not (and both have identical types and
qualifiers) it is rather hard to imagine reasons why a compiler would prefer
an allocation different from the order of occurrence. (For the heck of it,
it could be the opposite order, but then it would break some existing code,
would it not? :)


Any existing code that assumes declared variables are allocated
consecutively is already badly broken.

I just tried the above code with gcc; the address of b was 4 bytes
"lower" than the address of a.

There are any number of reasons why a compiler might choose to
allocate declared variables in a seemingly arbitrary order. For
example, on some architectures it's cheaper to access variables in the
first N bytes of the current activation frame; in that case, the
compiler is likely to group smaller and/or frequently accessed
variables together.
Now, the standard quoting in this thread... undefined behavior and such...
If a function receives an int *, it has no idea where it came from, so it
(function) must allow adding any int to it (pointer). (As is usual in C, it
is the programmer's job to care about overflows.) So this "undefinedn ess"
looks like a lip service.
No, undefined behavior in this case is really undefined behavior. A
function receiving an int* had better be written so it won't try to
access memory outside the object it points to. If goes beyond the
bounds of the original object and steps on neighboring objects, that's
a bug. (And yes, it is the programmer's job to care about overflows;
if the programmer makes a mistake, the compiler isn't obligated to
clean up the resulting mess.)
I wonder if I am correct in this assessment and also if indeed for all
practical purposes (&a+1==&b) holds true for statements <qual><type>a ;
<qual><type>b ;?


Nope.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
(Note new e-mail address)
Nov 14 '05 #22

"Joona I Palaste" <pa*****@cc.hel sinki.fi> wrote in message
news:br******** **@oravannahka. helsinki.fi...
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.


Yes, you're right.
--
/-- 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


Poor guy, I've seen some windows boxes that are in really bad shape, but
this Karri guy's machine is something ;-) must be beta of upcoming Windows
Longboot BE (bluescreen edition)?

with respect,
Toni Uusitalo
Nov 14 '05 #23
Toni Uusitalo <to************ **@luukkudot.ko m> scribbled the following:
"Joona I Palaste" <pa*****@cc.hel sinki.fi> wrote in message
news:br******** **@oravannahka. helsinki.fi...
/-- 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

Poor guy, I've seen some windows boxes that are in really bad shape, but
this Karri guy's machine is something ;-) must be beta of upcoming Windows
Longboot BE (bluescreen edition)?


I happen to know Karri Kalpio in person (he was my team leader at my
former job, until I got fired), and he's a really adamant Linux
enthusiast and Windows hater. So it's no wonder he said that quote -
and even gave it to me on paper so I could stick it on my monitor at
work.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"A bee could, in effect, gather its junk. Llamas (no poor quadripeds) tune
and vow excitedly zooming."
- JIPsoft
Nov 14 '05 #24
Joona I Palaste <pa*****@cc.hel sinki.fi> spoke thus:
until I got fired)


*You* got fired? How'd you manage that?

(the point being that, if you, being much more competent than I, are
being fired, I may have some issues in my future...)

--
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 #25
Christopher Benson-Manica <at***@nospam.c yberspace.org> scribbled the following:
Joona I Palaste <pa*****@cc.hel sinki.fi> spoke thus:
until I got fired)
*You* got fired? How'd you manage that? (the point being that, if you, being much more competent than I, are
being fired, I may have some issues in my future...)


I got fired because the company had to subsidise (sp?) to avoid going
bankrupt, and I was chosen as "least productive programmer". Not "least
competent" - least *productive*. The code I was writing was fine, but it
could just as well have been written by any other programmer on our
team.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Stronger, no. More seductive, cunning, crunchier the Dark Side is."
- Mika P. Nieminen
Nov 14 '05 #26
Joona I Palaste <pa*****@cc.hel sinki.fi> spoke thus:
I got fired because the company had to subsidise (sp?) to avoid going
bankrupt, and I was chosen as "least productive programmer". Not "least
competent" - least *productive*. The code I was writing was fine, but it
could just as well have been written by any other programmer on our
team.


So what you meant to say is that you got laid off, yes? That might be
a language thing, really. One might also say that you were
"downsized. " My condolences.

--
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 #27
Christopher Benson-Manica <at***@nospam.c yberspace.org> scribbled the following:
Joona I Palaste <pa*****@cc.hel sinki.fi> spoke thus:
I got fired because the company had to subsidise (sp?) to avoid going
bankrupt, and I was chosen as "least productive programmer". Not "least
competent" - least *productive*. The code I was writing was fine, but it
could just as well have been written by any other programmer on our
team.
So what you meant to say is that you got laid off, yes? That might be
a language thing, really. One might also say that you were
"downsized. " My condolences.


Yes, that's right. IANAL, so I use the term "get fired" for all
occurrences of quitting one's job by someone else's decision, not one's
own. Thanks for your condolences.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"B-but Angus! You're a dragon!"
- Mickey Mouse
Nov 14 '05 #28
Christopher Benson-Manica wrote:
So what you meant to say is that you got laid off, yes?


Email would appear more appropriate for such a conversation.

Nov 14 '05 #29
Joona I Palaste wrote:
Christopher Benson-Manica <at***@nospam.c yberspace.org> scribbled the following:
Joona I Palaste <pa*****@cc.hel sinki.fi> spoke thus:
until I got fired)


*You* got fired? How'd you manage that?


(the point being that, if you, being much more competent than I, are
being fired, I may have some issues in my future...)

I got fired because the company had to subsidise (sp?) to avoid going
bankrupt, and I was chosen as "least productive programmer". Not "least
competent" - least *productive*. The code I was writing was fine, but it
could just as well have been written by any other programmer on our
team.


Man, that sucks. :(

I don't suppose they took into consideration anything other than amount
of code? So you might have been producing code that took a little longer
to write, but which saved a great deal of time in debugging and
maintenance, and was of overall better quality. But the managers
apparently aren't getting paid to look at the big picture, so you get
the boot.

I'm guessing they let go of one of their best programmers. Those kinds
of decisions are probably a major reason for their financial troubles.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #30

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
9535
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10465
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
10242
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...
0
9061
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
7558
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
6800
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
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4127
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.