C++ Lesson: That Which Owns the Pointer to the Heap Gets to delete[].

Language butchery by Mr Rich on  14.3.10 @ 13:04

Again, stuff that you don't learn in school, but thanks to the power of the Internet your answers become solved. I'm charged with making a class that reads in a file, parses strings from the file and allocates memory on the heap for those strings. I must use cstrings, new, and delete. Here's the gist of the program:
  1. Main creates a class variable "Dictionary."
  2. The constructor for the Dictionary class:
    • Reads an input file
    • Creates an array of "Entries" on the heap
      ("Entry" is another class consisting of pointers to two dynamically allocated strings)
  3. A menu is presented
  4. etc.. etc..

So what did I find out that was so novel? Thanks to this post on stackoverflow, (yes... that's my post), I realized that I had a memory leak with my Entry Class. I was storing the pointer to the dynamic memory in the entry, but not deleting it when I was done.

A new commandment: That which owns the pointer to the heap gets to delete[]

Comments: Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?