memory leak

memory leak

Post by pretentious on Sat Nov 30, 2013 10:21 pm
([msg=78361]see memory leak[/msg])

Put together a little something something when i was bored
Code: Select all
#include <iostream>
#include <stdlib.h>
int main(int argc, char* argv[]){
    for(int i = 0; i < atoi(argv[1]) ; i ++){
        new char[1000000];
    }
    int i;
    std::cin>>i;
}

Mathematically, It doesn't work exactly how i expected but it did make my computer hang...which was useful
Enjoi
Goatboy wrote:Oh, that's simple. All you need to do is dedicate many years of your life to studying security.

IF you feel like exchanging ASCII arrays, let me know ;)
pretentious wrote:Welcome to bat country
User avatar
pretentious
Contributor
Contributor
 
Posts: 573
Joined: Wed Mar 03, 2010 12:48 am
Blog: View Blog (0)


Re: memory leak

Post by Goatboy on Sat Nov 30, 2013 11:36 pm
([msg=78362]see Re: memory leak[/msg])

Wouldn't call that a memory leak exactly, more just using a lot of memory. A memory leak is a situation in which memory is not freed after it is used. Most often you see this in C as opposed to a similarly-functioning C++ program. Manual memory management is more common in C, but still doable in C++, just most people don't do so.
Assume that everything I say is or could be a lie.
1UHQ15HqBRZFykqx7mKHpYroxanLjJcUk
User avatar
Goatboy
Expert
Expert
 
Posts: 2788
Joined: Mon Jul 07, 2008 9:35 pm
Blog: View Blog (0)


Re: memory leak

Post by pretentious on Mon Dec 02, 2013 3:40 am
([msg=78378]see Re: memory leak[/msg])

Goatboy wrote:A memory leak is a situation in which memory is not freed after it is used
Like even after termination of the program? Could you give an example?
Goatboy wrote:Most often you see this in C as opposed to a similarly-functioning C++ program

I could have just as easily used malloc. I thought C++ worked the same way, just provided more abstraction
Goatboy wrote:Oh, that's simple. All you need to do is dedicate many years of your life to studying security.

IF you feel like exchanging ASCII arrays, let me know ;)
pretentious wrote:Welcome to bat country
User avatar
pretentious
Contributor
Contributor
 
Posts: 573
Joined: Wed Mar 03, 2010 12:48 am
Blog: View Blog (0)


Re: memory leak

Post by centip3de on Mon Dec 02, 2013 7:48 pm
([msg=78381]see Re: memory leak[/msg])

This is an odd little program you've thrown together, because usually you assign the memory that's been allocated to you to something, or you know... actually use it. In fact, I can't find any use cases of just allocating memory without actually assigning the pointer of that memory to anything, so most of what I say here is just going to be an educated guess.

That said, yes, this is a memory leak. You're allocating memory that you never deallocate, which is, the definition of a memory leak. Although you're using new, instead of the C version of malloc/alloc/calloc, you still have to eventually deallocate the memory using delete. The issue here arises in that because you never assign the pointer to anything, you can't specifically delete that block of memory. Moreover, because C++ does not have garbage collection, it won't automatically delete the block of memory for you when the scope of the for-loop ends as well. However, because you aren't assigning the block of memory to anything, C++ may or may not delete the memory block once you leave the scope anyway.

Essentially, because you did this in a weird way, I have no idea what the hell will happen. Here's a normal memory leak below:

Code: Select all
int main()
{
int * p = new int;  //Memory leak as it's never deallocated
return 0;
}


Q/A tiem:
pretentious wrote:Like even after termination of the program? Could you give an example?


Yes after the termination of the program, and already did.

pretentious wrote:I could have just as easily used malloc. I thought C++ worked the same way, just provided more abstraction


Essentially. There are bit more use cases in C++ and odd rules, but that's essentially right.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rick Cook
User avatar
centip3de
Moderator
Moderator
 
Posts: 1412
Joined: Fri Aug 20, 2010 5:46 pm
Blog: View Blog (0)


Re: memory leak

Post by Goatboy on Mon Dec 02, 2013 8:36 pm
([msg=78382]see Re: memory leak[/msg])

Unless you're using an OS from 1995 or an obscure embedded system, the memory you allocate should be automatically freed by the OS after program termination. Can you imagine how fast your system would grind to a halt if this was not the case?

The best example I can give of a good memory leak was several years ago in Firefox. I don't recall the specifics, but basically tabs were not closing completely so if you had a long session and were constantly opening and closing tabs, you could see 800,000K+ of RAM usage (when that was a lot). However once you closed Firefox the issue went away.

So yes I guess technically what you wrote was a memory leak in the same sense that a paper airplane is a 747.

centip3de wrote:In fact, I can't find any use cases of just allocating memory without actually assigning the pointer of that memory to anything [...]

I'll sometimes set aside memory if I know I'll need a block quickly later on.
Assume that everything I say is or could be a lie.
1UHQ15HqBRZFykqx7mKHpYroxanLjJcUk
User avatar
Goatboy
Expert
Expert
 
Posts: 2788
Joined: Mon Jul 07, 2008 9:35 pm
Blog: View Blog (0)


Re: memory leak

Post by centip3de on Mon Dec 02, 2013 9:10 pm
([msg=78384]see Re: memory leak[/msg])

Goatboy wrote:So yes I guess technically what you wrote was a memory leak in the same sense that a paper airplane is a 747.


A memory leak is any allocated memory that isn't deallocated when the program ends. Just because it doesn't crash the program or OS doesn't mean it isn't a memory leak ;) .

Goatboy wrote:I'll sometimes set aside memory if I know I'll need a block quickly later on.


Right, but generally you assign a variable to the memory, rather than just allocating a block without a variable for accessing it. Allocated memory that you can't access is pretty pointless. For example:

Code: Select all
int * blah = new int; //Can use later
new int; //Can't use later
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rick Cook
User avatar
centip3de
Moderator
Moderator
 
Posts: 1412
Joined: Fri Aug 20, 2010 5:46 pm
Blog: View Blog (0)


Re: memory leak

Post by Goatboy on Mon Dec 02, 2013 9:31 pm
([msg=78387]see Re: memory leak[/msg])

It's not specific to just when a program ends though. If you have something like a server that is running 24/7 and every request to that server allocates let's say 10 bytes that it never deallocates, then over time that adds up. Kill the server and the OS should take care of it. So we're both talking about different aspects of the same thing.

As for your second point, I misread that. Derp on my part = thought you meant allocating memory without initializing it.
Assume that everything I say is or could be a lie.
1UHQ15HqBRZFykqx7mKHpYroxanLjJcUk
User avatar
Goatboy
Expert
Expert
 
Posts: 2788
Joined: Mon Jul 07, 2008 9:35 pm
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests