Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 04-09-2009
Member
 
Join Date: Aug 2009
Posts: 53
Difference between heap and stack

Assume that I have the below statement in my code. I would like to ask where will memory be allocated, in heap or in stack? What is the difference between a heap and a stack? How can I free the allocated memory?

Code:
char* x = "hello";
Reply With Quote
  #2  
Old 04-09-2009
Member
 
Join Date: Nov 2008
Posts: 1,221
Re: Difference between heap and stack

The stack is the part of system memory where all the variables are stored before run-time. The heap is the part of system memory where all the variables are stored during run-time. This means that if you are declaring a variable "i" in your code and assigning the value of say "123" then that will be stored in your stack because the compiler knows the value during the compile time (step that is before run-time). Whereas if you define a pointer variable (as in your statement) but want to initialize it somewhere else then that will be stored in your heap, since it is unknown to compiler at the compile time.
Reply With Quote
  #3  
Old 04-09-2009
Member
 
Join Date: May 2008
Posts: 685
Re: Difference between heap and stack

If you perform certain tasks such as if you have a variable "area" and its value is to be computed by some expression then this variable will remain in stack. In short stack is used for storing local variables (variables declared inside a function), function parameters, etc. These variables do not have any proper value and usually stores garbage in them. The heap stores dynamically assigned variables (one declared with malloc). It stores any global variables used, static local variables etc.
Reply With Quote
  #4  
Old 04-09-2009
Member
 
Join Date: Apr 2008
Posts: 1,948
Re: Difference between heap and stack

Code:
char* x = "hello";
Since you have directly declared the variable and assigned the value in your code, the variable "x" will be stored in your stack because the compiler already knows the variable value. You can free the memory using 2 ways, either using "delete" or if it is declared using "malloc" then use "free"
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Difference between heap and stack"
Thread Thread Starter Forum Replies Last Post
What is the difference between binary tree sort and heap sort in data structure sRIPRIYA Software Development 3 04-01-2011 01:25 AM
Need Help in Heap Code in C++ Pratim Software Development 5 21-01-2010 05:57 PM
What is Heap and Stack? samualres Software Development 5 27-11-2009 10:56 AM
Heap size in JVM and Setting max Heap Size for the JVM Conner Software Development 3 19-05-2009 08:47 PM
Bluetooth, Changing from Toshiba stack to MS stack LaKisha Vista Hardware Devices 1 18-10-2007 11:25 PM


All times are GMT +5.5. The time now is 03:10 PM.