Confusion between Stack and Data segment - Unix

This is a discussion on Confusion between Stack and Data segment - Unix ; Hi, I am not clear with my understanding of stack and data segment. Consider this example int main() { int i; int j=5; return 0; } Does variable " j " get stored in the data segment as it is ...

+ Reply to Thread
Results 1 to 13 of 13

Thread: Confusion between Stack and Data segment

  1. Confusion between Stack and Data segment

    Hi,
    I am not clear with my understanding of stack and data segment.
    Consider this example

    int main()
    {
    int i;
    int j=5;
    return 0;
    }

    Does variable " j " get stored in the data segment as it is
    initialized OR is it stored in the stack segment as it is a local
    variable.
    Does variable " i " get stored in the stack segment or bss
    (uninitialized).

    This may be a pretty trivial question but I don't seem to have my
    basics right.

    Thanks
    Utkado

  2. Re: Confusion between Stack and Data segment

    Utkado writes:
    > I am not clear with my understanding of stack and data segment.
    > Consider this example
    >
    > int main()
    > {
    > int i;
    > int j=5;
    > return 0;
    > }
    >
    > Does variable " j " get stored in the data segment as it is
    > initialized OR is it stored in the stack segment as it is a local
    > variable.
    > Does variable " i " get stored in the stack segment or bss
    > (uninitialized).


    The basics are that two (simplification) different storage classes
    exist in C, automatic storage and static storage. An object with
    automatic storage duration starts to live when the function defining
    it is invoked and ceases to exit when it returns. An object with
    static storage duration comes into existence at program start and
    continues to be a live object until the program terminates.

    Both i and j have automatic storage duration, because they are defined
    in 'main', consequently, they would usually be stack-allocated.

  3. Re: Confusion between Stack and Data segment

    In article <871w8r1f1e.fsf@fever.mssgmbh.com>,
    Rainer Weikusat wrote:

    > Utkado writes:
    > > I am not clear with my understanding of stack and data segment.
    > > Consider this example
    > >
    > > int main()
    > > {
    > > int i;
    > > int j=5;
    > > return 0;
    > > }
    > >
    > > Does variable " j " get stored in the data segment as it is
    > > initialized OR is it stored in the stack segment as it is a local
    > > variable.
    > > Does variable " i " get stored in the stack segment or bss
    > > (uninitialized).

    >
    > The basics are that two (simplification) different storage classes
    > exist in C, automatic storage and static storage. An object with
    > automatic storage duration starts to live when the function defining
    > it is invoked and ceases to exit when it returns. An object with
    > static storage duration comes into existence at program start and
    > continues to be a live object until the program terminates.
    >
    > Both i and j have automatic storage duration, because they are defined
    > in 'main', consequently, they would usually be stack-allocated.


    I suspect the OP is thinking more of something like this:

    void foo() {
    char *a = "abcdefg";
    }

    The pointer a is stored in the stack segment, as it's a local variable.
    But the string that it points to is stored in the data segment, because
    literal strings are static. Actually, some implementations will store
    literal strings in the text segment, where the executable code lives,
    because literals are not supposed to be modified.

    Another case is local variables like:

    char a[] = "abcdefg";

    In this case, there's an array in the stack segment, and whenever the
    functionis entered it's initialized by copying the contents of the
    "abcdefg" string into it, i.e. this is equivalent to:

    char a[8];
    strcpy(a, "abcdefg");

    As above, the literal string may live in the data or text segments; in
    this case, I suspect almost all implementations will use the text
    segment.

    In the case of small numbers used to initialize int variables, though,
    the initialization is most likely done using immediate data in the
    instructions used to fill in the stack frame. But if a function has
    lots of initialized local variables, another way to do it would be to
    have a template of the stack frame in the text segment, and copy it when
    entering the function.

    --
    Barry Margolin, barmar@alum.mit.edu
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***
    *** PLEASE don't copy me on replies, I'll read them in the group ***

  4. Re: Confusion between Stack and Data segment

    Barry Margolin writes:

    >In the case of small numbers used to initialize int variables, though,
    >the initialization is most likely done using immediate data in the
    >instructions used to fill in the stack frame. But if a function has
    >lots of initialized local variables, another way to do it would be to
    >have a template of the stack frame in the text segment, and copy it when
    >entering the function.


    There is, of course, no requirement that the data is ever stored anywhere,
    or perhaps only in a register, depending on further use in the function.

    Casper
    --
    Expressed in this posting are my opinions. They are in no way related
    to opinions held by my employer, Sun Microsystems.
    Statements on Sun products included here are not gospel and may
    be fiction rather than truth.

  5. Re: Confusion between Stack and Data segment

    Barry Margolin writes:
    > Rainer Weikusat wrote:
    >> Utkado writes:
    >> > I am not clear with my understanding of stack and data segment.
    >> > Consider this example
    >> >
    >> > int main()
    >> > {
    >> > int i;
    >> > int j=5;
    >> > return 0;
    >> > }
    >> >
    >> > Does variable " j " get stored in the data segment as it is
    >> > initialized OR is it stored in the stack segment as it is a local
    >> > variable.
    >> > Does variable " i " get stored in the stack segment or bss
    >> > (uninitialized).

    >>
    >> The basics are that two (simplification) different storage classes
    >> exist in C, automatic storage and static storage. An object with
    >> automatic storage duration starts to live when the function defining
    >> it is invoked and ceases to exit when it returns. An object with
    >> static storage duration comes into existence at program start and
    >> continues to be a live object until the program terminates.
    >>
    >> Both i and j have automatic storage duration, because they are defined
    >> in 'main', consequently, they would usually be stack-allocated.

    >
    > I suspect the OP is thinking more of something like this:
    >
    > void foo() {
    > char *a = "abcdefg";
    > }
    >
    > The pointer a is stored in the stack segment, as it's a local variable.
    > But the string that it points to is stored in the data segment, because
    > literal strings are static.


    [...]

    While it is certainly possible that someone 'meant' to write something
    completely different from what he actually wrote, there is no way to
    determine what was 'meant' to be written. And I think he was confused
    about the distinction between 'data' and 'bss', the first of which is
    used for initialized variables and the second for uninitalized ones,
    and automatic variables which can either be initialized or
    uninitialzed, but reside in neither segment, because no storage needs
    to be allocated for them at program load time.


  6. Re: Confusion between Stack and Data segment

    On Jan 9, 1:09*am, Utkado wrote:
    > Hi,
    > I am not clear with my understanding of stack and data segment.
    > Consider this example
    >
    > int main()
    > {
    > *int i;
    > *int j=5;
    > return 0;
    >
    > }
    >
    > Does variable " j " get stored in the data segment as it is
    > initialized OR is it stored in the stack segment as it is a local
    > variable.
    > Does variable " i " *get stored in the stack segment or bss
    > (uninitialized).
    >
    > This may be a pretty trivial question but I don't seem to have my
    > basics right.
    >
    > Thanks
    > Utkado


    C has no requirements that such a thing as "stack" exists. It only
    recognizes "static" and "automatic" storage classes. Thel anguage
    specification says nothing about WHERE objects with either storage
    class are put; that is implementation dependent. There are platforms
    with no heap or stack.
    --
    Fred Kleinschmidt

  7. Re: Confusion between Stack and Data segment

    fred.l.kleinschmidt@boeing.com writes:
    > On Jan 9, 1:09*am, Utkado wrote:
    >> Hi,
    >> I am not clear with my understanding of stack and data segment.
    >> Consider this example
    >>
    >> int main()
    >> {
    >> *int i;
    >> *int j=5;
    >> return 0;
    >>
    >> }
    >>
    >> Does variable " j " get stored in the data segment as it is
    >> initialized OR is it stored in the stack segment as it is a local
    >> variable.
    >> Does variable " i " *get stored in the stack segment or bss
    >> (uninitialized).
    >>
    >> This may be a pretty trivial question but I don't seem to have my
    >> basics right.
    >>
    >> Thanks
    >> Utkado

    >
    > C has no requirements that such a thing as "stack" exists. It only
    > recognizes "static" and "automatic" storage classes. Thel anguage
    > specification says nothing about WHERE objects with either storage
    > class are put; that is implementation dependent. There are platforms
    > with no heap or stack.


    This is comp.unix.programmer, and UNIX(*) has both conventions and
    requirements regarding this (eg the ELF-standard).

  8. Re: Confusion between Stack and Data segment

    In article <871w8rx9b2.fsf@fever.mssgmbh.com>,
    Rainer Weikusat wrote:

    > fred.l.kleinschmidt@boeing.com writes:
    > > On Jan 9, 1:09*am, Utkado wrote:
    > >> Hi,
    > >> I am not clear with my understanding of stack and data segment.
    > >> Consider this example
    > >>
    > >> int main()
    > >> {
    > >> *int i;
    > >> *int j=5;
    > >> return 0;
    > >>
    > >> }
    > >>
    > >> Does variable " j " get stored in the data segment as it is
    > >> initialized OR is it stored in the stack segment as it is a local
    > >> variable.
    > >> Does variable " i " *get stored in the stack segment or bss
    > >> (uninitialized).
    > >>
    > >> This may be a pretty trivial question but I don't seem to have my
    > >> basics right.
    > >>
    > >> Thanks
    > >> Utkado

    > >
    > > C has no requirements that such a thing as "stack" exists. It only
    > > recognizes "static" and "automatic" storage classes. Thel anguage
    > > specification says nothing about WHERE objects with either storage
    > > class are put; that is implementation dependent. There are platforms
    > > with no heap or stack.

    >
    > This is comp.unix.programmer, and UNIX(*) has both conventions and
    > requirements regarding this (eg the ELF-standard).


    Yeah, that looked like one of those hair-trigger responses you generally
    see from the comp.lang.c crowd. Maybe Fred was confused about which
    newsgroup he was in -- I'll bet similar questions get misposted there
    all the time.

    --
    Barry Margolin, barmar@alum.mit.edu
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***
    *** PLEASE don't copy me on replies, I'll read them in the group ***

  9. Re: Confusion between Stack and Data segment

    fred.l.kleinschmidt@boeing.com wrote:
    > C has no requirements that such a thing as "stack" exists. It only
    > recognizes "static" and "automatic" storage classes. Thel anguage
    > specification says nothing about WHERE objects with either storage
    > class are put; that is implementation dependent. There are platforms
    > with no heap or stack.


    I'm not necessarily saying that such platforms don't exist, but I am
    curious how such a platform would allow for, say, a recursive function.
    (Perhaps the answer is that they do not.)

    - Logan

  10. Re: Confusion between Stack and Data segment

    Logan Shaw wrote:
    > fred.l.kleinschmidt@boeing.com wrote:
    > > C has no requirements that such a thing as "stack" exists. It only
    > > recognizes "static" and "automatic" storage classes. Thel anguage
    > > specification says nothing about WHERE objects with either storage
    > > class are put; that is implementation dependent. There are platforms
    > > with no heap or stack.

    >
    > I'm not necessarily saying that such platforms don't exist, but I am
    > curious how such a platform would allow for, say, a recursive function.
    > (Perhaps the answer is that they do not.)
    >


    If you google comp.lang.c, you'll find that often the IBM 360 is mentioned.
    It doesn't have a hardware stack (PUSH, POP instructions, etc), and the C
    language implementation uses something like a linked-list of call frames
    which is, effectively, something of a stack from the perspective of the
    application programmer.


  11. Re: Confusion between Stack and Data segment

    On Jan 9, 1:09 am, Utkado wrote:

    > int main()
    > {
    > int i;
    > int j=5;
    > return 0;
    >
    > }
    >
    > Does variable " j " get stored in the data segment as it is
    > initialized OR is it stored in the stack segment as it is a local
    > variable.


    It can't be stored in the data segment. Think about a program like
    this:

    int main(int argc, char **argv)
    {
    int j=5;
    j=argc;
    if(j>1) return main(j-1, argv+1);
    return 0;
    }

    Now suppose I invoke this program with twelve arguments. The first
    main, with 'j' at 12 will call the second 'main' with 'j' at 11, and
    so on. How can all those 'j's live in the data segment? How can the
    compiler possibly know how much space to make for those 'j's in the
    data segment at compile time?

    DS

  12. Re: Confusion between Stack and Data segment

    Logan Shaw writes:

    >fred.l.kleinschmidt@boeing.com wrote:
    >> C has no requirements that such a thing as "stack" exists. It only
    >> recognizes "static" and "automatic" storage classes. Thel anguage
    >> specification says nothing about WHERE objects with either storage
    >> class are put; that is implementation dependent. There are platforms
    >> with no heap or stack.


    >I'm not necessarily saying that such platforms don't exist, but I am
    >curious how such a platform would allow for, say, a recursive function.
    >(Perhaps the answer is that they do not.)


    There have been platforms like that, including, I think, one from Cray.

    Stack frames are allocated from the heap and linked together; and
    freed when pop'ed. (More tricky for longjmp but not impossible either.

    Casper
    --
    Expressed in this posting are my opinions. They are in no way related
    to opinions held by my employer, Sun Microsystems.
    Statements on Sun products included here are not gospel and may
    be fiction rather than truth.

  13. Re: Confusion between Stack and Data segment

    David Schwartz wrote:
    > On Jan 9, 1:09 am, Utkado wrote:
    >
    >> int main()
    >> {
    >> int i;
    >> int j=5;
    >> return 0;
    >>
    >> }
    >>
    >> Does variable " j " get stored in the data segment as it is
    >> initialized OR is it stored in the stack segment as it is a local
    >> variable.

    >
    > It can't be stored in the data segment. Think about a program like
    > this:
    >
    > int main(int argc, char **argv)
    > {
    > int j=5;
    > j=argc;
    > if(j>1) return main(j-1, argv+1);
    > return 0;
    > }
    >
    > Now suppose I invoke this program with twelve arguments. The first
    > main, with 'j' at 12 will call the second 'main' with 'j' at 11, and
    > so on. How can all those 'j's live in the data segment? How can the
    > compiler possibly know how much space to make for those 'j's in the
    > data segment at compile time?


    Tail-recursion elimination? :-)

    - Logan

+ Reply to Thread