MQ said:
There are some topics that are clearly off-topic, and some that are
marginal. It is up to each individual to decide this, not the group
"moderators".
There are no moderators. There are only individuals. As an
individual, I have decided, in accordance with a fairly broad
consensus of knowledgeable regulars (other individuals), that some
things are off-topic, for reasons that have been explained at great
length. "BSS" is one of these things.
When someone answers a question that's off-topic, we
always see the bureaucracy chime in with "chapter and verse" bollocks.
I know that the topic is not standard C, but I don't care.
Perhaps you should. Standard C is what this newsgroup is about.
But the "chapter and verse" challenges are usually in response to
off-topic *answers*, not off-topic questions.
That is
what I mean by intolerance. And I refute the accusation that the
answer is misleading. Misleading in a universal context, maybe, but
not in this case...
You haven't refuted the accusation, you've merely denied it. (Or
maybe I'm misusing the words "refute" and "deny", but you get the
idea.)
Here's the article that started this thread, posted by "onkar":
| 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 ??
And here's the initial response, posted by "MQ" (that's you):
| onkar wrote:
[quoted text deleted]
|
| Local variables are stored on the stack. BSS is used for uninitialized
| global or static variables.
The question was asked in comp.lang.c. You replied with
system-specific information that may be accurate for some particular
implementations (perhaps even most of them), but is not specified by
the C language, and that very likely is untrue or meaningless for some
conforming C implementations. You did not qualify your answer in any
way. The OP likely was starting out with the misconception that "BSS"
and "stack" are defined by the C language; you reinforced that
misconception.
Furthermore, the phrase "uninitialized global or static variables" is
potentially misleading. You probably meant to refer to variables that
are not *explicitly* initialized; such variables (if they have static
storage duration) are implicitly initialized to zero, not
uninitialized.
For comparison, here's what I probably would have written if I'd seen
the thread sooner:
========================================
Roughly, yes, it's allocated on the stack, but read on.
Strictly speaking, the C language has no concept of "bss" or "stack".
It defines several storage durations, namely "static", "automatic",
and "allocated".
Variables with static storage duration exist throughout the lifetime
of the program. They're either declared outside of any function, or
inside a function with the 'static" keyword. Such variables are
implicitly initialized to zero if they don't have an explicit
initialier. Some implementations may allocate such variables from a
"bss" section, but that's system-specific; consult another newsgroup
or your system's documentation for more information about that.
Variables with automatic storage duration are declared inside
functions *without* the "static" keyword, and exist only until the end
of their scope; your variable "i" is an example of this. (On many
implementations, automatic variables are allocated on a "stack".)
Unlike a static variable, an automatic variable that isn't explicitly
initialized has a garbage value until you assign something to it.
Allocated storage duration is for objects created by the
malloc() function. Such objects exist from the time they're created
until they're destroyed by a call to free(). (It's a little more
complicated than that; see also the calloc() and realloc() functions.)
Finally, you need to add a "#include <stdio.h>" to be able to use the
printf function. If your compiler didn't warn you about this, you
should set whatever options it needs to persuade it to do so. It may
happen to work without the "#include <stdio.h>", but it's not
guaranteed to do so.
========================================
This answers the OP's question *and* teaches enough fundamental
concepts of C for the answer to make sense.