variable allocated from stack/bss ??

R

Richard Heathfield

jacob navia said:

ANSI C Standard 6.7.8.10 page 126
If an object that has automatic storage duration is not initialized
explicitly, its value is indeterminate. If an object that has static
storage duration is not initialized explicitly, then:
? if it has pointer type, it is initialized to a null pointer;
? if it has arithmetic type, it is initialized to (positive or unsigned)
zero;
? if it is an aggregate, every member is initialized (recursively)
according to these rules;
? if it is a union, the first named member is initialized (recursively)
according to these rules.

This means that if an uninitialized object has a value different than
zero, it can't be a static object.

THEN, it must be an automatic storage class object.

Nobody disputes any of this. What we'd like to know is where you think the
Standard says anything about "bss variables".
 
R

Richard Heathfield

jacob navia said:

<all the irrelevant stuff snipped>

Nothing left.

You seem to be unable or unwilling to address, or even to understand, the
point I'm making. Nobody is disputing the initialisation rules, and I don't
know why you think anyone is doing so. Nor is anyone disputing your
deduction that an object that is indeterminately-valued by the sequence
point after its definition must have automatic storage duration.
 
J

jacob navia

Richard said:
jacob navia said:




Nobody disputes any of this. What we'd like to know is where you think the
Standard says anything about "bss variables".

Bss variables are global uninitialized variables in all systems
that implement an automatically zeroed section.

The global uninitialized variables map to the bss section in the
executable. Please read the OP question.
 
J

jacob navia

Richard said:
jacob navia said:

<all the irrelevant stuff snipped>

Nothing left.

You seem to be unable or unwilling to address, or even to understand, the
point I'm making. Nobody is disputing the initialisation rules, and I don't
know why you think anyone is doing so. Nor is anyone disputing your
deduction that an object that is indeterminately-valued by the sequence
point after its definition must have automatic storage duration.

OK. We agree then. How nice
 
R

Richard Heathfield

jacob navia said:
Bss variables are global uninitialized variables in all systems
that implement an automatically zeroed section.

Where does the Standard state this?
The global uninitialized variables map to the bss section in the
executable. Please read the OP question.

We can reasonably assume that the OP is unaware of the niceties of the C
language, and thus might not realise that the concept of a "bss section" is
unknown to the language. I trust we do not have to assume the same of you?
 
M

Mark McIntyre

ANSI C Standard 6.7.8.10 page 126

This quote, which I'm not bothering to retain in its entirety, is
irrelevant since...
If an object that has automatic storage duration is not initialized
explicitly, its value is indeterminate.

.....file-scope objects do not have automatic storage duration ( 6.2.2
and others).
This means that if an uninitialized object has a value different than
zero, it can't be a static object.

The main difficulty with this is that its impossible to detect what
value an uninitialised variable has. Annex J.2, while informative
only, deals with this.
THEN, it must be an automatic storage class object.

The risk with this is that newbies will apply the corollory, which is
of course false since zero is a valid undefined value.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
M

Mark McIntyre

Bss variables are global uninitialized variables in all systems
that implement an automatically zeroed section.

Fascinating, but the question was, where does the C Standard say this?
And by the way, the C standard makes it explicitly impossible to have
uninitialised globals, so the idea is moot anyway.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
J

jacob navia

Mark said:
This quote, which I'm not bothering to retain in its entirety, is
irrelevant since...




....file-scope objects do not have automatic storage duration ( 6.2.2
and others).




The main difficulty with this is that its impossible to detect what
value an uninitialised variable has. Annex J.2, while informative
only, deals with this.




The risk with this is that newbies will apply the corollory, which is
of course false since zero is a valid undefined value.

There is NO risk, since it is NOT zero. The risk would be if there was
a zero in that uninitialized variable. Since there ISN'T it CAN'T BE static.

Q.E.D
 
J

jacob navia

Richard said:
jacob navia said:




Where does the Standard state this?




We can reasonably assume that the OP is unaware of the niceties of the C
language, and thus might not realise that the concept of a "bss section" is
unknown to the language. I trust we do not have to assume the same of you?

There is more to C than what the standard text says. Mostly it is silent
in many important questions. The linker is not mentioned, neither is the
debugger, the executable format, and many other questions that are
central to building a functioning program. Since I have implemented
those under several OSes for lcc-win32 I am aware of those maybe more
than you that seem to dwell in the standard text as it was the holy
bible "Chapter and verse" is a dogmatic answer heathfield.

I think that since C is a systems programming language it is important
to be aware of how it works, not only aware of the standard or being
able to throw to the "adversary" a chapter and verse.

For short:

Uninitialized variables are collected into a common section of the
executable by the linker, a section that will not be written to
the disk. When the loader loads the program, it allocates more space
than needed for the data section, and zeroes it. This is the bss section.

For long:

Many systems now avoid the bss section altogether and declare a data
section that is bigger than the size of the data section in the disk.
If the loader supports it, this allows for a section with identical
characteristics than the bss section but without consuming more space
in an own section.

For all:

Many people are completely unaware of this stuff and think this is not
important. It may be. It depends on your curiosity, in your interest for
details, or in the type of software you are writing.
 
W

websnarf

onkar said:
Given the following code & variable i .

int main(int argc,char **argv) {
int i;
printf("%d\n",i);
return 0;
}

here i is allocated from bss or stack ?

I think it is stack,because it prints garbage value; If it were
allocated from bss the default value would be 0
please tell me if I am right or wrong ??

Well, from a storage point of view, the standard calls these static and
auto respectively instead of bss or stack. (These latter two are
usually the names of segments commonly given by some platforms and they
implement these two kinds of storage.)

Basically, auto variables are anything declared inside the scope of a
function without a static decorator, and correspond to your
implementation's "stack". An auto variable that is not autoinitialized
may have arbitrary contents until it is assigned a value (things are
more complicated with structs and unions, but they behave exactly as
you would expect). Everything else one way or another ends up in some
kind of static memory and as you suspect will have a default
initialization to 0. (There is a little confusion here, since static
and globals have different semantic meaning, but have the same storage
properties. Let's ignore this for now and just consider globals to be
statics with extra scope.)

Using the name "stack" is a bit misleading since the stack that can be
inferred from the standard is, in fact, the call-stack. Many
implementations will mix in auto data in their implementations of the
call-stack and call the whole thing just the stack. But it is not
necessary for implements to do things this way (and in fact doing this
commonly leads to security problems related to imprecise usage of the
standard C library.)

Probably only the most cynical or the ignorant would be unaware of what
you mean by bss, but again this is technically a platform-specific
name. The heap, and some static storage, for example, may in fact be
found in entirely different segments as a matter of convenience to the
compiler as they are with WATCOM C/C++ and MSVC++. (Note that the heap
is not pre-initialized to 0 either, but it is certainly not
auto-storage.)

In your example the variable i is an auto. That doesn't necessarily
mean its on any particular implementation's stack -- it just behaves as
if it were. The reality is that auto variables are commonly mapped to
machine registers, and in the case you show, it is very likely that
that is where i ends up.
 
R

Richard Heathfield

jacob navia said:
There is NO risk, since it is NOT zero. The risk would be if there was
a zero in that uninitialized variable. Since there ISN'T it CAN'T BE
static.

Since we don't know what value the uninitialised variable has, we can't be
sure that it isn't zero.

That only works when you're right.
 
R

Richard Heathfield

jacob navia said:

There is more to C than what the standard text says. Mostly it is silent
in many important questions. The linker is not mentioned, neither is the
debugger, the executable format, and many other questions that are
central to building a functioning program.

All of those things vary from platform to platform, but the C language
doesn't, and the C language is what we discuss here. Implementation details
are best discussed in newsgroups devoted to those implementations.
 
J

jacob navia

Richard said:
jacob navia said:




Since we don't know what value the uninitialised variable has, we can't be
sure that it isn't zero.
Yes we can. The original poster wrote:
Given the following code & variable i .

int main(int argc,char **argv){
int i;
printf("%d\n",i);
return 0;
}

here i is allocated from bss or stack ?

I think it is stack,because it prints garbage value; If it were
allocated from bss the default value would be 0
please tell me if I am right or wrong ??

NOTE:
"... it prints a garbage value. If it were allocated from the
bss the default value would be 0"

He said it was NOT ZERO.
That only works when you're right.

I am right
 
R

Richard Heathfield

jacob navia said:
Richard Heathfield wrote:
Yes we can.

No, we can't. Below, I explain why.
The original poster wrote:
Given the following code & variable i .

int main(int argc,char **argv){
int i;
printf("%d\n",i);
return 0;
}

here i is allocated from bss or stack ?

I think it is stack,because it prints garbage value; If it were
allocated from bss the default value would be 0
please tell me if I am right or wrong ??

NOTE:
"... it prints a garbage value. If it were allocated from the
bss the default value would be 0"

He said it was NOT ZERO.

The program exhibits undefined behaviour for not just one but two reasons,
one of which is that printf is not properly prototyped. The other is that
he attempts to evaluate an expression which refers to an object of
indeterminate value. Since the behaviour of the program is undefined, the
Standard imposes no requirements on its behaviour. Consequently, even if i
happened by some strange chance to be 0, the program could still print some
non-zero value. Basically, the C Standard tells us that we can't trust the
output of such a program.
I am right

No.
 
J

jacob navia

Richard said:
jacob navia said:



No, we can't. Below, I explain why.




The program exhibits undefined behaviour for not just one but two reasons,
one of which is that printf is not properly prototyped.

Irrelevant heathfield, completely irrelevant. Non
prototyped functions are assumed returning an int.
So what?
The other is that
he attempts to evaluate an expression which refers to an object of
indeterminate value.

Yes, and so what?
Since the behaviour of the program is undefined, the
Standard imposes no requirements on its behaviour.

Nowhere is specified that the behavior is
undefined. It has an UNDEFINED VALUE heathfield but the
program is not "undefined" nor it invokes "undefined behavior"

Talking nonsense again.


Consequently, even if i
happened by some strange chance to be 0, the program could still print some
non-zero value.

WOW. And you believe in Santa Claus also? I mean if that variable
has an unspecified value (that will be between INT_MIN and INT_MAX
anyway) automagically the printf function will print non zero when
it is zero ?

This is completely nuts.!


Basically, the C Standard tells us that we can't trust the
output of such a program.

Now, I will send you back your stuff:

CHAPTER AND VERSE PLEASE.

Where does the standard say we can't trust the output of
such a program???

The value of the variable is undefined. That is all. But
it will be between INT_MIN and INT_MAX. And please do not
cheat:

"... It could exist an implementation, call it heathfield C,
or HC for short, where printf would first look at a hidden
parameter where it is indicated whether the integer variable
passed to it was initialized or not. In that implementation
we could see a zero"

That would be cheating. Please show me an actual example ok?

Thanks
 
M

Mark McIntyre

There is NO risk, since it is NOT zero. The risk would be if there was
a zero in that uninitialized variable. Since there ISN'T it CAN'T BE static.

Zero is a valid uninitialised value (as it were). Heck, ANY
bitpattern is a valid value.

Your logic is similar to "all men are humans, therefore all humans are
men".

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
R

Richard Heathfield

jacob navia said:
Irrelevant heathfield, completely irrelevant. Non
prototyped functions are assumed returning an int.

You are missing the point again. This is nothing to do with return values.

C89 3.7.1: "If a function that accepts a variable number of arguments is
defined without a parameter type list that ends with the ellipsis
notation, the behavior is undefined."

The wording is identical in C99 - see 6.9.1(8) of n1124.

So the behaviour is undefined, so we can't trust the behaviour, that's so
what.
Yes, and so what?

So the behaviour is undefined, so we can't trust the behaviour, that's so
what.
Nowhere is specified that the behavior is
undefined.

See previous cite. Also see the definition of undefined behaviour in C89:

* Undefined behavior --- behavior, upon use of a nonportable or
erroneous program construct, of erroneous data, or of
indeterminately-valued objects, for which the Standard imposes no
requirements. Permissible undefined behavior ranges from ignoring the
situation completely with unpredictable results, to behaving during
translation or program execution in a documented manner characteristic
of the environment (with or without the issuance of a diagnostic
message), to terminating a translation or execution (with the issuance
of a diagnostic message).

Also see the definition of undefined behaviour in C99: "behavior, upon use
of a nonportable or erroneous program construct or of erroneous data,
for which this International Standard imposes no requirements".

The data is erroneous, and therefore the program's behaviour is undefined.
It has an UNDEFINED VALUE heathfield
Correct.

but the
program is not "undefined" nor it invokes "undefined behavior"
Incorrect.


Talking nonsense again.

You say that a lot, but I have yet to see it proved correct. Rather, it's
you who is talking nonsense.
Consequently, even if i

WOW. And you believe in Santa Claus also? I mean if that variable
has an unspecified value (that will be between INT_MIN and INT_MAX
anyway) automagically the printf function will print non zero when
it is zero ?

It might, or it might not. The behaviour is undefined.
This is completely nuts.!

The program, or your argument? I vote both.
Basically, the C Standard tells us that we can't trust the

Now, I will send you back your stuff:

CHAPTER AND VERSE PLEASE.

Already cited. See above.
 
R

Richard Heathfield

Mark McIntyre said:
Zero is a valid uninitialised value (as it were). Heck, ANY
bitpattern is a valid value.

Depends what you mean by "valid". :) If you s/valid/possible/ then that
might be a bit clearer.
 
O

Old Wolf

jacob said:
In some weird implementation that could exist somewhere there is no
stack and no bss.

I have programmed on an implementation with no stack-based variables
(it did have a 128-byte call stack), and no zero-initialized static
data.
Since I want to be pedantic I will generalize then to say that "The
C language dfoesn't require a stack" even if I know that 99% of all
the implementations in work stations today use exactly those.

This NG is confined to workstation implementations now?

It's not hard to say "automatic storage" rather than "stack".
 
K

Keith Thompson

jacob navia said:
Bss variables are global uninitialized variables in all systems
that implement an automatically zeroed section.

The global uninitialized variables map to the bss section in the
executable. Please read the OP question.

Please demonstrate that the automatically zeroed section is called
"bss" on *all* systems that implement such a thing.

The C standard is quite clear about scope, linkage, and storage class.
There is no need to drag system-specific terminology into a discussion
of the C language.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top