Thread: struct declaration...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305

    struct declaration...

    Pardon me for asking so many questions but i am really having lots of questions as i learn more about the language.

    If i make a declaration like
    [insert]
    Code:
    struct stack
    {
        int a[MAX]; // MAX is defined to be of some size
        int  top;
    };
    I was thinking what happens if i have after this declaration the statement

    struct stack s;

    Does the compiler allocate space for it in the memory. Coming from a little java background how does this work out in c ? Like when is the actual allocation done it it at compilation time or at run time?

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    In the declaration
    struct stack s;
    the compiler allocates space for it in memory at compile time. This is basically a definition where the varaible 's' gets space in memory.
    Code:
    struct stack
    {
        int a[MAX]; // MAX is defined to be of some size
        int  top;
    };
    This is the declaration of the stack. No space is set aside for it here.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    If "struct stack s;" is inside a function, it's allocated at run-time.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The compiler will calculate how much space it needs on the stack and reserve that much at run-time. So basically it is allocated at compile time. It's different from dynamic memory which is actually reserved at run-time.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by Elysia View Post
    The compiler will calculate how much space it needs on the stack and reserve that much at run-time. So basically it is allocated at compile time.
    How is that not a contradiction?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Nope. I suppose you might see it as this...
    Place 10 variables on the stack. The compiler will reserve space ONCE for all of the variables.
    Allocate 10 variables with dynamic memory. Space will be reserved 10 times.

    Further, when using dynamic memory, a lot of things things happen. You may even get more than you asked for.
    But the compiler gets exactly the space it wants when reserving space on the stack.

    So I'd like to think of it as compile-time and run-time.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Ok, so here goes my query. For the simple case
    Code:
    int i=10;
    printf("%p",(void *)&i); // say it prints ABCDEF
    When will the address ABCDEF be given to 'i', at compile time or run time?
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Runtime, because it's created in memory, yes?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    @ BEN10

    In the declaration

    struct stack s;
    the compiler allocates space for it in memory at compile time. This is basically a definition where the varaible 's' gets space in memory.
    @robWhit

    If "struct stack s;" is inside a function, it's allocated at run-time.
    Now what does this mean . It means that if i have struct stack s outside main it gets allocated at compile time but if have struct stack inside main or any other function it gets allocated at run time. Is this right?


    [Quote]
    @Elysia

    The compiler will calculate how much space it needs on the stack and reserve that much at run-time.
    [\Quote]

    Doesnt this imply that allocation is done at run time because its simply calculating the chunk of bytes that it will require but the space has not yet been reserved in memory. So the actual allocation is done at run time.

  10. #10
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    No. During compile time, the compiler allocates the exact memory the program needs as of that time. The memory is allocated before-hand. When allocating memory dynamically, the application goes through a series of queries to your OS, by requesting memory, etc.

  11. #11
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by roaan View Post
    Now what does this mean . It means that if i have struct stack s outside main it gets allocated at compile time but if have struct stack inside main or any other function it gets allocated at run time. Is this right?
    If you declare struct stack, space is not reserved for it in memory, but if you declare a variable like struct stack s in main(), space gets reserved for it in memory and the address to it is allocated at run time. This is what I've understood till now.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    there are sections in an executable file. some are for read-only data (like constants), some are for read-write data (file scope and static modifiable variables), some for initialization values, etc. these get copied into memory when the program image is loaded. then the stack is set up. then the program starts running. the program allocates space on the stack for its local variables and function parameters and return addresses and etc. if a function is recursive, a function could have multiple copies of its variables on the stack. obviously, they are not copies of the same variable, but refer to different variables, each of which corresponds to the same identifier in the source code, but is different depending on which function invocation it is.

    so local vars are "allocated" at the time of the function call (roughly speaking. there are some optimizations that can be done), and their init values are copied from the init section of the program image.

    Depending on function call order, the local vars/parameters/return address might get stored in a different place in the stack as compared to another call. so it can't be allocated at compile time. it has to be at run time (ignoring optimizations). And with stack allocation, there might be gaps in the stack too (you get more than you ask for) because of alignment requirements, for example.

  13. #13
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    I would argue that all memory is only allocated at run-time. The compiler can only make calculations as to the amounts needed - whether they are reserved in memory during the time the executable's image is loaded (variables in "global"), or during function calls when the stack is used for localized variables.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  2. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  3. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  4. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  5. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM