Click here to Skip to main content
15,936,171 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

If we declare a variable (int a;), is there any memory allocated to the variable at this declaration time?

And also if we declare a class pointer (MyClass *ptr) any amount of static memory allocated to 'ptr' to store the address of the dynamically created memory (ptr = new MyClass()) on this declaration time?

Thanks a lot in advance...
Updated 18-Oct-11 18:43pm

First, what is "declaration time"? Some memory is allocated by the loader of you application, some is additionally allocated during run time. It depends on what is your variable. It can be static and allocated by the loaded is some section of memory dedicated to static data. It can be "segment", some pages of pages memory, etc., depending on system architecture. Stack variable is allocated on stack, but the stack itself is allocated when its thread is created, as a phase of loading of application in case of main (starting) thread and during run time for threads created during run time.

Now, about the object of the class allocated on heap… Not just heap is involved: there are two different parts of this object: the variable points to the pointer, and the pointer points to the memory in heap where "object itself" is placed. So, the memory referenced by the pointer is, yes, allocated in heap, but the pointer itself is placed in memory exactly as in the case of integer variables, as described above. But on the top of it, a pointer can be allocated in heap itself. Simple example: a pointer can be a member of other class. And the pointer to the object of that class&hellop; well, you understand… :-)

Share this answer
Silju MC 19-Oct-11 3:01am    
Thank you :-) 5+ and I'm accepting your answer.
Sergey Alexandrovich Kryukov 19-Oct-11 11:21am    
You are welcome.
Simon Bang Terkildsen 19-Oct-11 11:30am    
Sergey Alexandrovich Kryukov 19-Oct-11 12:06pm    
Thank you, Simon.
There are several types of memory, but, as a programmer, you only have to deal with two types: the 'stack' and the 'heap'.

The stack is memory that your compiler will take care of. Variables such as pointers or ints declared as local parameters in a function will be stored there. This memory usually will be allocated the moment your function gets called (but the compiler may occasonally handle this otherwise), and will be released the moment your function returns. You cannot do much regarding how the compiler treats this, but some compiler settings may subtly change the behaviour.

The heap is the memory that you can access programatically. Any amount of memory that you allocate dynamically using new statements will be allocated there, and it is your responsibility to release that memory later, using delete (or delete []).

So, to answer your question, both the int and pointer variables you mentioned would be allocated on the stack, every time the function is called that declares them, and the memory will be released once your function returns. If the variables are declared global however, the memory will be allocated upon starting the application, and released upon exiting (this is just one more reason not to use global variables!).

If you declare static variables inside a function, they will mostly behave like global variables, but allocating the memory required for them may be postponed until the function is called for the first time. The memory will still remain in use until the application exits.
Share this answer
Sergey Alexandrovich Kryukov 19-Oct-11 11:23am    
Some good points here, but a programmer also deals with the memory reserved for static variables. I voted 4.
Stefan_Lang 19-Oct-11 11:31am    
I mentioned static variables in the last paragraph. Or did you consider that information inaccurate?
Simon Bang Terkildsen 19-Oct-11 11:30am    
Got my 5

When we declare a variable the memory allocated in the stack memory and its removed automaticaly based on the scope of the variable

And the secon case When the object is created that time memory allocated in the heap. its we can handle programmatically also

Share this answer
And it gets even better. If your variable is of local scope (dynamically alloced on the stack) then the compiler's optimization phase may decide, based on the usage of the variable, that it does not need any physical memory at all and optimize it into processor registers. This, of course, only applies to simple data types and not to class objects (but could apply to pointers to class objects). Playing the "find the memory location" game can be fun.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900