struct declaration...

This is a discussion on struct declaration... within the C Programming forums, part of the General Programming Boards category; Pardon me for asking so many questions but i am really having lots of questions as i learn more about ...

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?

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

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.

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.

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

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.

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.

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.

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

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.

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.