Results 1 to 4 of 4

Thread: Difference between heap and stack

  1. #1
    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";

  2. #2
    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.

  3. #3
    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.

  4. #4
    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"

Similar Threads

  1. Replies: 3
    Last Post: 04-01-2011, 01:25 AM
  2. Need Help in Heap Code in C++
    By Pratim in forum Software Development
    Replies: 5
    Last Post: 21-01-2010, 05:57 PM
  3. What is Heap and Stack?
    By samualres in forum Software Development
    Replies: 5
    Last Post: 27-11-2009, 10:56 AM
  4. Heap size in JVM and Setting max Heap Size for the JVM
    By Conner in forum Software Development
    Replies: 3
    Last Post: 19-05-2009, 08:47 PM
  5. Bluetooth, Changing from Toshiba stack to MS stack
    By LaKisha in forum Vista Hardware Devices
    Replies: 1
    Last Post: 18-10-2007, 11:25 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,043,828.29188 seconds with 17 queries