Thread: Help with the Heap

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    40

    Help with the Heap

    Hi there
    As you would guess from my question, I am a complete noob.I have a question about Heaps. They are made so that a variable is defined in one function and used in another, I noticed that this variable is a pointer, is it always a pointer, or could it be any other type?
    Thanks.

  2. #2
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Heap is something from which you can allocate memory (ofcourse at run time)
    The memory can be allocated only to pointer variables.
    Pointer is a variable which points to a location in the heap.
    You get a segmentation fault when you access the pointer which has not been allocated any memory or is pointing nowhere.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Blacky Ducky View Post
    ... They are made so that a variable is defined in one function and used in another ...
    You're either describing a global variable there which is in what some call the data segment, or a local variable which is simply on the stack. However, it does not matter where a variable is declared in order to be able to access data on the heap. A heap is about allocating something additional elsewhere and having it hang around for as long as you want, being able to dispose of it whenever you want, and not having it tied to a particular variable.

    For all practical purposes a variable that allows you to get to allocated heap memory will be a pointer, but in theory it could be a reference, or if type casting is involved it could be just about anything.

    The question is somewhat like asking if your address must always be written on paper in order to be able to drive there.
    You should learn more about pointers before learning what they can be used for.
    Last edited by iMalc; 02-14-2011 at 01:49 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    40
    Thanks guys , Appericiate your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. heap sort
    By mherald81 in forum C Programming
    Replies: 16
    Last Post: 10-31-2010, 09:21 AM
  2. heap
    By George2 in forum Windows Programming
    Replies: 2
    Last Post: 11-10-2007, 11:49 PM
  3. Heap Work
    By AndyBomstad in forum C++ Programming
    Replies: 1
    Last Post: 05-16-2005, 12:09 PM
  4. Do you know...
    By davejigsaw in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 10:33 AM
  5. heap question
    By mackol in forum C Programming
    Replies: 1
    Last Post: 11-30-2002, 05:03 AM