I hot the following program from the book 'C++ For Dummies' but have two questions about it. First I compiled the program but I'm afraid to run because the memery created from the heap does not appear to me to get de-allocated. Am I correct in not wanting to run this and is my reason valid? Secondly, on the line Student* pS = new Student; is it alway a rule that memory created from the heap must be access via a pointer? I know when I removed the * I get the following error from g++. constructor.cpp:30:22: error: conversion from ‘Student*’ to non-scalar type ‘Student’ requested -> Student pS = new Student;

// constructor - example that invokes a constructor
//
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

class Student
{
    public:
        Student()
        {
            cout << "constructing student" << endl;
            semesterHours = 0;
            gpa = 0.0;
        }

        // ...other public memebers...
    protected:
        int semesterHours;
        double gpa;
};

int main()
{
    cout << "Creating a new Student object" << endl;
    Student s;

    cout << "Creating a new object off the heap" << endl;
    Student* pS = new Student;
}

All program memory allocated on the heap within a program will be returned to the operating system when the program terminates.

As for your line 30, that should be just fine. The results of new Type is a pointer to a Type object, Student in your case, so if you take the'*'away from the variable on line 30, you got the error you saw, which was correct. That all said though, you don't have to pass the pointer around in function calls, but can pass by reference and pass *pS to a function that uses it, declaring the usage as a Student& or const Student& as appropriate. This has a couple of benefits over passing as a pointer.

  1. The called function doesn't need to check the passed pointer for NULL (the compiler will deal with that for you).
  2. If it is passed as a const reference, then the called function cannot modify the object, but can only call public const methods that it provides.
Comments
Great explanation.

What programmers mostly don't know about heaps is that even when the memory is freed via the delete operator, the heap actually retains memory. Normal heaps don't free memory.

The heap shipped with I++ is a Binary Heap - which is to say, it is built using AVL Binary Trees. It shrinks as well as grows - so a load of deletes will actually result in memory being freed back to the operating system.

@Ben - yes, simple delete operations on objects won't return the memory to the OS, but will be available to the program for future allocations. I'm not sure about what you say about "Binary Heap" allocations. The usual means to allocate more memory, and return it to the OS, is the sbrk function. It can only return (safely) the unallocated memory at the end of the heap. This is NOT an exercise for amateurs! I spent a lot of time studying, and writing, low-level memory allocation routines. Trust me, it is a LOT more trouble than it is worth!

Edited 11 Months Ago by rubberman

The Binary Heap is a revolution in technology. If you define 'garbage collection' to mean minimizing the free store of the heap, then Binary Heaps are garbage collecting heaps. A normal heap consists of free lists whereas a Binary Heap consists of free trees. These trees do binary searches when a unit is freed and merge the units into blocks. When a complete block is present in the Free Trees it is deleted. It is not more trouble than its worth - Binary Heaps are practically a miracle in heap technology - a giant step forward.

This is not an uncommon technique to minimize heap fragmentation; however, unless I can see the complete code for this I have trouble believing that they are returning that memory to the OS. Windows may do that, but *nixes won't without a call to sbrk().

This article has been dead for over six months. Start a new discussion instead.