Thread: memory leak

  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, 05: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, 03:19 PM
  4. Any Memory Leak Checking Tool?
    By George2 in forum C Programming
    Replies: 4
    Last Post: 06-21-2006, 11:02 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM