Memory Leaks

This is a discussion on Memory Leaks within the C++ Programming forums, part of the General Programming Boards category; I have question regarding memory leaks. I was using some test code to create anonymous object arrays of a certain ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    178

    Memory Leaks

    I have question regarding memory leaks. I was using some test code to create anonymous object arrays of a certain size and return the addresses to a pointer. I put the code in a loop and was curious if I could run my computer out of memory.

    Code:
            long double *mem = new long double(1073741824);
    
    	long double *poop;
    
    	while (1)
    	{
    		poop = new long double[1073741824];
    		std::cout << "Memory Allocated: " <<
    				(*mem += 1073741824) << std::endl;
    	}
    I was not sure what I observed in my activity monitor; it locked up. The program eventually crashed with this error:

    Memory Allocated: 8.79395e+12
    Memory Allocated: 8.79502e+12
    Memory Allocated: 8.79609e+12
    C++Primer(2721) malloc: *** mmap(size=17179869184) failed (error code=12)
    *** error: can't allocate region
    *** set a breakpoint in malloc_error_break to debug
    terminate called after throwing an instance of 'std::bad_alloc'
    what(): std::bad_alloc


    What did I manage to do and why did I manage to not lock up my computer in the process with that much memory leak? Am I clueless? yes!

    PS> Can someone please explain why when I delete a dynamically allocated memory location it can still be referenced by the pointer?
    Last edited by Imanuel; 09-25-2011 at 09:56 PM.

  2. #2
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,665
    Quote Originally Posted by Imanuel View Post
    What did I manage to do and why did I manage to not lock up my computer in the process with that much memory leak? Am I clueless? yes!
    Would you rather your OS did crash? It's called design - the OS tries to terminate a program that misbehaves while staying alive in the process.
    Quote Originally Posted by Imanuel View Post
    Can someone please explain why when I delete a dynamically allocated memory location it can still be referenced by the pointer?
    What's the alternative? If you free that dynamically allocated memory, the pointer to it still exists. the delete function has no way of knowing what is or is not pointing to it. A pointer is in no way "attached" to the memory it points to.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73
    That made me smile ahah ! Did the very same thing when I began C++ a few years ago and got the same result. However, for whatever reason I rran it a few more times and it crashed my computer so much that I had to do a recovery :\

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    178
    Quote Originally Posted by Alexandre View Post
    That made me smile ahah ! Did the very same thing when I began C++ a few years ago and got the same result. However, for whatever reason I rran it a few more times and it crashed my computer so much that I had to do a recovery :\
    I am using Mac OS X Leopard and while playing with the code, I saw the virtual memory jump into the terabytes but the program memory never got higher than about 8-9 megabytes.

    I attempted to run the same code on my Pavilion notebook running Windows 7 and it told me no thank you; the program would not execute at all.

    Thank you for all the replies.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    2,492
    the reason the memory never got over 8-9 megabytes is because the system never attempted to actually allocate physical memory pages for the virtual memory space you were allocating. modern OS kernels only allocate physical pages upon a write to a given region of memory. otherwise, it's just entries in a page table somewhere, and the present bit is not set, meaning the space is not actually allocated in physical memory.

    consider the following code:

    Code:
    #include <iostream>
    #include <unistd.h>
    
    int main(void)
    {
    	int pageSize = getpagesize();
    	
    	long totalMemory = 0;
    	while (true)
    	{
    		char* foo = new char[pageSize];
    		totalMemory += pageSize;
    		foo[0] = 0;
    		std::cout << "Allocated " << totalMemory << " bytes (" << totalMemory / 1024 <<  "KB) of memory." << std::endl;
    	}
    	
    	return 0;
    }
    this will allocate (and write to) memory until it cannot anymore. either the system will crash, or the kernel will terminate your program because it cannot allocate any more memory.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory usage and memory leaks
    By vsanandan in forum C Programming
    Replies: 1
    Last Post: 05-03-2008, 05:45 AM
  2. Are there any memory leaks here??
    By cmasri in forum C++ Programming
    Replies: 9
    Last Post: 01-18-2007, 12:14 AM
  3. Odd memory leaks
    By VirtualAce in forum C++ Programming
    Replies: 11
    Last Post: 05-25-2006, 12:56 AM
  4. Memory leaks!!
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 05-13-2002, 04:01 AM
  5. Memory Leaks
    By SwirlingVortex in forum C++ Programming
    Replies: 2
    Last Post: 09-21-2001, 06:34 AM

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