variable allocated from stack/bss ??

O

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

MQ

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

Local variables are stored on the stack. BSS is used for uninitialized
global or static variables.
 
S

santosh

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

The C standard doesn't specify the exact places where variables of
different types are stored. It just specifies their scope,
implementations are free to do whatever they want behind the scenes, as
long as all the necessary semantics are maintained.

Coming to your off-topic question, the short answer is yes, on most
implementations.
 
R

Richard Heathfield

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 ?

The C Standard doesn't require your implementation to have a bss or a stack.
It does require, however, that you provide a proper prototype for printf,
and it also requires that you don't evaluate indeterminately-valued
objects.
 
M

Martin Ambuhl

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

Neither 'bss' nor 'stack' have any meaning in C. All we can say is that
'i' is an uninitialized automatic variable, and so has an indeterminate
initial value. If your implementation has some segment of memory
initialized to zero -- and that is by no means a given -- then it may,
or may not, use parts of that for some automatic variables. The
mechanism is completely up to the implementation.
 
M

Martin Ambuhl

MQ said:
Local variables are stored on the stack. BSS is used for uninitialized
global or static variables.

There is no such requirement for an implementation of the C programming
language. There is, in fact, no requirement for things called 'stack'
or 'BSS' to even exist. Please don't give such foolish and incorrect
'answers'. They only show your limited experience and lack of
qualification to give meaningful answers.
 
J

jacob navia

Martin said:
Neither 'bss' nor 'stack' have any meaning in C. All we can say is that
'i' is an uninitialized automatic variable, and so has an indeterminate
initial value. If your implementation has some segment of memory
initialized to zero -- and that is by no means a given -- then it may,
or may not, use parts of that for some automatic variables. The
mechanism is completely up to the implementation.

Translation:

In some weird implementation that could exist somewhere there is no
stack and no bss. 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.
 
J

jacob navia

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

Yes, you are right
 
J

jacob navia

Richard said:
jacob navia said:




Chapter and verse, please.

Since bss variables are initialized to zero (the standard does not
name them "bss" of course but it requires that uninitialized
variables be initilaized to zero), that variable CAN'T be an
uninitialized variable in that sense. That mens is an automatic
variable allocated in temporary storage. This storage is normally
the stack in most implementations
 
R

Richard Heathfield

jacob navia said:
Since bss variables

Where does the Standard define these?
are initialized to zero (the standard does not
name them "bss" of course but it requires that uninitialized
variables be initilaized to zero),

It says no such thing.


#include <stdio.h>

int main(void)
{
int bss;

You seem to be claiming that this "bss variable" must be initialised to 0.
If so, you're wrong.
 
J

jacob navia

Richard said:
jacob navia said:




Where does the Standard define these?




It says no such thing.


#include <stdio.h>

int main(void)
{
int bss;

You seem to be claiming that this "bss variable" must be initialised to 0.
If so, you're wrong.

What?
Can't you read?
I said that that wasn't a bss variable because (precisely) is not
cleared to zero.
 
R

Richard Heathfield

jacob navia said:
Richard Heathfield wrote:


What?
Can't you read?

Sure. Can you?
I said that that wasn't a bss variable because (precisely) is not
cleared to zero.

No, the object's failure to get a default static initialiser value does not,
as you claim, imply that it's a "bss variable" (for which you have yet to
provide a reference to the Standard's definition); rather, it implies that
the object doesn't have static storage duration and is not a sub-member of
a partly-initialised aggregate object.
 
C

CBFalconer

Richard said:
jacob navia said:
.... snip ...

Where does the Standard define these?


It says no such thing.

If someone has excess time on their hands, it might be amusing to
collect statistics on which posters have corrections applied to
their posts, and in what proportion. To avoid arguments we can
dispense with whether or not the corrections are accurate. If the
results are published they would serve as a (very) rough guide to
poster usefulness.
 
A

Al Balmer

jacob navia said:


Sure. Can you?


No, the object's failure to get a default static initialiser value does not,
as you claim, imply that it's a "bss variable" (for which you have yet to
provide a reference to the Standard's definition); rather, it implies that
the object doesn't have static storage duration and is not a sub-member of
a partly-initialised aggregate object.

I'm only seeing one side of this discussion, but now I'm curious. Has
Jacob invented a new type of variable called "bss"? In my assembler
coding days, "bss" meant "block storage symbol" and was implemented by
simply incrementing the program counter by the requested amount, which
of course had no effect on memory thus skipped.
 
J

jacob navia

Al said:
I'm only seeing one side of this discussion, but now I'm curious. Has
Jacob invented a new type of variable called "bss"? In my assembler
coding days, "bss" meant "block storage symbol" and was implemented by
simply incrementing the program counter by the requested amount, which
of course had no effect on memory thus skipped.

Well things have changed since those days probably...
Nowadays bss means the same thing BUT when the OS loads
the stuff, it clears the bss section to zero.

bss variables are

int i;

struct Customer f;

etc. At global level of course. They correspond to the
uninitialized global variables of C.
 
R

Richard Heathfield

jacob navia said:
Al Balmer wrote:


Well things have changed since those days probably...
Nowadays bss means the same thing BUT when the OS loads
the stuff, it clears the bss section to zero.

Where does the Standard say this?
bss variables are

int i;

struct Customer f;

Chapter and verse, please.
etc. At global level of course. They correspond to the
uninitialized global variables of C.

There is no such thing as an uninitialized global variable in C.
 
J

jacob navia

CBFalconer said:
... snip ...



If someone has excess time on their hands, it might be amusing to
collect statistics on which posters have corrections applied to
their posts, and in what proportion. To avoid arguments we can
dispense with whether or not the corrections are accurate. If the
results are published they would serve as a (very) rough guide to
poster usefulness.
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.
My reasoning holds, even if heathfield gets all upset.
 
J

jacob navia

Richard said:
jacob navia said:




Where does the Standard say this?




Chapter and verse, please.




There is no such thing as an uninitialized global variable in C.
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.

My reasoning holds, even if you get all upset. :)
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top