What's automatic memory allocation ?

Jan 10, 2010 at 1:58pm
Hi there!

Can you tell me what Automatic memory allocation allocates ? I know that:

* Static memory allocation allocates global and static variables (at compile-time)
* Dynamic memory allocation allocates dynamic variables (at runtime)

So - according to this - Automatic memory allocation allocates local variables (at runtime), doesn't it ?
Jan 10, 2010 at 2:24pm
Automatic memory is used on normal variables.
The following two lines are equivalent with the current standard:
1
2
int x;
auto int x;



1
2
3
4
5
6
7
8
9
10
11
int *Dynamic;

{
    static int Static; // allocated only once - at runtime -
    int Auto; // allocated each time the current { } block is executed
    Dynamic = new int; // allocated when you want - each call of new -
} // Auto is destructed when out of scope

delete Dynamic; // Dynamic deleted whenever you want

// Static is destructed when the program ends 
Last edited on Jan 10, 2010 at 2:36pm
Jan 10, 2010 at 2:33pm
So - simply puts - Automatic memory allocation allocates local variables which are automatic :)
Jan 10, 2010 at 4:47pm
No. Simply put, automatic variables are named variables on the stack.
Jan 10, 2010 at 10:33pm
I know... But who does allocate them - Automatic memory allocation ?
Jan 10, 2010 at 10:36pm
As in who does the allocation?
The compiler - hence, automatic.
Jan 10, 2010 at 11:07pm
Global variables are allocated by the compiler, but they are not auto.
Jan 11, 2010 at 7:36pm
The compiler - hence, automatic.


And the name of this allocation is Automatic memory allocation, right ?

( http://en.wikipedia.org/w/index.php?title=Automatic_memory_allocation&redirect=no )
Jan 12, 2010 at 12:52am
They're automatic because they're automatically recreated each time the scope of execution is entered, and destroyed when the scope is exited.

C99 6.2.4
http://www.open-std.org/JTC1/SC22/wg14/www/docs/n1124.pdf
Jan 12, 2010 at 1:00am
Jan 16, 2010 at 4:20pm
I've got one more question: is Stack-based memory allocation the same thing as Automatic memory allocation allocates ?
Last edited on Jan 16, 2010 at 4:21pm
Jan 16, 2010 at 4:23pm
Yes
Jan 16, 2010 at 5:06pm
In my opinion, as the name says:

* Stack-based memory allocation allocates space for auto variables,
* Heap-based memory allocation allocates space for dynamic variables (created by 'new' operator),
* Static memory allocation allocates space for global and static variables.

Is that right ?
Jan 16, 2010 at 6:00pm
Yes
Jan 16, 2010 at 8:12pm
OK, thanks Bazzy ;)
Topic archived. No new replies allowed.