memory leak

This is a discussion on memory leak within the C++ Programming forums, part of the General Programming Boards category; hi What is a memory leak? and how can we stop it? thnks...

  1. #1
    gatli
    Guest

    memory leak

    hi

    What is a memory leak? and how can we stop it?

    thnks

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    25
    The biggest cause of memory leaks for beginner programmers is stray pointers. Stray pointers occur when you lose focus of a pointer but don't delete the variable it pointedto on the heap. The variable will still be on the heap until the program exits but you will have no way to destroy it or access it.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    Code:
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    int main(int argc, char* argv[])
    {
    //created on the heap. The new operator returns a pointer to the
    //80 chars on the heap. If the delete operator is not used on the 
    //pointer the allocated memory will exist until the program exits. 
    //The delete operator will give the allocated memory back to the 
    //heap. The delete operator should be called when the memory is
    //no longer needed else it is considered a leak.
    
    	char * pHeapChar = new char[80];
    
    	if( pHeapChar )
    	{
    		delete [] pHeapChar;
    	}
    	else
    	{
    		cout << "Insufficient memory error " << endl;
    	}
    
    
    //created on the stack no memory leak possible
    	char Stack[80];
    	char * pStackChar = Stack;
    	pStackChar = &Stack[0]; //same a above
    
    	return 0;
    }

    Any local or global automatic variables are on the stack and cannont cause a memory leak.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Question Question

    What's a stack? Heard about it before, but never understood what it is...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    A stack is a first in last out (filo) data structure. This allows for scoped variables. That is automatic variables to be placed on the stack while in a programming block. When that block is left those variables are popped off the stack. In actuality their left there but the stack pointer is moved down and those location are made availible for the next variables.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory leak in this case?
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 03-22-2008, 06:05 AM
  2. memory leak in the code?
    By George2 in forum C++ Programming
    Replies: 20
    Last Post: 01-13-2008, 06:50 AM
  3. Is this code memory leak free? ---> POSIX Threads
    By avalanche333 in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2007, 04:19 PM
  4. Any Memory Leak Checking Tool?
    By George2 in forum C Programming
    Replies: 4
    Last Post: 06-22-2006, 12:02 AM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 10:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21