variable allocated from stack/bss ??

Discussion in 'C Programming' started by onkar, Nov 29, 2006.

  1. onkar

    onkar Guest

    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 ??
    onkar, Nov 29, 2006
    #1
    1. Advertising

  2. onkar

    MQ Guest

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


    Local variables are stored on the stack. BSS is used for uninitialized
    global or static variables.
    MQ, Nov 29, 2006
    #2
    1. Advertising

  3. onkar

    santosh Guest

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


    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.
    santosh, Nov 29, 2006
    #3
  4. MQ said:

    <snip>

    > Local variables are stored on the stack.


    C&V please.

    > BSS is used for uninitialized
    > global or static variables.


    C&V please.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, Nov 29, 2006
    #4
  5. 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.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, Nov 29, 2006
    #5
  6. onkar 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 ??


    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.
    Martin Ambuhl, Nov 29, 2006
    #6
  7. MQ wrote:
    > onkar 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 ??

    >
    > 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.
    Martin Ambuhl, Nov 29, 2006
    #7
  8. onkar

    jacob navia Guest

    Martin Ambuhl wrote:
    > onkar 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 ??

    >
    >
    > 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.
    jacob navia, Nov 29, 2006
    #8
  9. onkar

    jacob navia Guest

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


    Yes, you are right
    jacob navia, Nov 29, 2006
    #9
  10. jacob navia said:

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

    >
    > Yes, you are right


    Chapter and verse, please.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, Nov 29, 2006
    #10
  11. onkar

    jacob navia Guest

    Richard Heathfield wrote:
    > jacob navia said:
    >
    >
    >>onkar 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 ??
    >>>

    >>
    >>Yes, you are right

    >
    >
    > 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
    jacob navia, Nov 29, 2006
    #11
  12. jacob navia said:

    > Richard Heathfield wrote:
    >> jacob navia said:
    >>
    >>
    >>>onkar 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 ??
    >>>>
    >>>
    >>>Yes, you are right

    >>
    >>
    >> Chapter and verse, please.
    >>

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

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, Nov 29, 2006
    #12
  13. onkar

    jacob navia Guest

    Richard Heathfield wrote:
    > jacob navia said:
    >
    >
    >>Richard Heathfield wrote:
    >>
    >>>jacob navia said:
    >>>
    >>>
    >>>
    >>>>onkar 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 ??
    >>>>>
    >>>>
    >>>>Yes, you are right
    >>>
    >>>
    >>>Chapter and verse, please.
    >>>

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


    What?
    Can't you read?
    I said that that wasn't a bss variable because (precisely) is not
    cleared to zero.
    jacob navia, Nov 29, 2006
    #13
  14. jacob navia said:

    > Richard Heathfield wrote:


    <snip>

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


    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.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, Nov 29, 2006
    #14
  15. onkar

    CBFalconer Guest

    Richard Heathfield wrote:
    > jacob navia said:
    >

    .... snip ...
    >>
    >> 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.


    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.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>
    CBFalconer, Nov 29, 2006
    #15
  16. onkar

    Al Balmer Guest

    On Wed, 29 Nov 2006 13:54:23 +0000, Richard Heathfield
    <> wrote:

    >jacob navia said:
    >
    >> Richard Heathfield wrote:

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

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


    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.

    --
    Al Balmer
    Sun City, AZ
    Al Balmer, Nov 29, 2006
    #16
  17. onkar

    jacob navia Guest

    Al Balmer wrote:
    > On Wed, 29 Nov 2006 13:54:23 +0000, Richard Heathfield
    > <> wrote:
    >
    >
    >>jacob navia said:
    >>
    >>
    >>>Richard Heathfield wrote:

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

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

    >
    >
    > 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.
    jacob navia, Nov 29, 2006
    #17
  18. jacob navia said:

    > Al Balmer wrote:


    <snip>
    >>
    >> 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.


    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.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, Nov 29, 2006
    #18
  19. onkar

    jacob navia Guest

    CBFalconer wrote:
    > Richard Heathfield wrote:
    >
    >>jacob navia said:
    >>

    >
    > ... snip ...
    >
    >>>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.

    >
    >
    > 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.
    jacob navia, Nov 29, 2006
    #19
  20. onkar

    jacob navia Guest

    Richard Heathfield wrote:
    > jacob navia said:
    >
    >
    >>Al Balmer wrote:

    >
    >
    > <snip>
    >
    >>>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.

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

    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. :)
    jacob navia, Nov 29, 2006
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    617
    Matt Wharton
    Dec 9, 2004
  2. termiflyer
    Replies:
    4
    Views:
    625
    S.Tobias
    Oct 10, 2005
  3. Replies:
    4
    Views:
    447
    Keith Thompson
    May 23, 2007
  4. karthikbalaguru

    Heap & BSS

    karthikbalaguru, Aug 21, 2007, in forum: C Programming
    Replies:
    13
    Views:
    904
    Ravishankar S
    Aug 23, 2007
  5. text,data and bss

    , Nov 16, 2008, in forum: C Programming
    Replies:
    27
    Views:
    7,159
Loading...

Share This Page