
Journal jawtheshark's Journal: C advice needed 12
As readers of my troll account may have noticed, I'm currently doing some C on Linux for a client. Now, that's not really a problem, even though I haven't done C in ages. After a week doing just that, I feel quite comfy and my vi, a few terminals and the man pages make me happy.
One thing that worries me are memory leaks. Now, I have been quite diligent in avoiding them but I
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
int main( int argc, char **argv ) {char* leak = malloc( 255 );
leak[0] = (char)NULL;
if ( ( argv[1] != NULL ) && ( strcmp( argv[1], "-t" ) ) ) {printf( "FAILURE\n" );
return ( EXIT_FAILURE );}
free( leak );
printf( "SUCCESS\n" );
return ( EXIT_SUCCESS );}
Evidently, this is just to illustrate something. (Never mind that I don't check if malloc returns NULL for example, I normally do check that stuff) As you can see, a memory leak would occur at return( EXIT_FAILURE ); if this were a normal function. However, it isn't: it's main and doing a return( EXIT_FAILURE ); means the program will stop and to my understanding, the operating system will reclaim all used memory in the first place.
What do you think about this construct? Bad? Acceptable? Just plain wrong? Perhaps, I'm not even right that the operating system will reclaim all memory (I doubt that though).
As said, I've been doing Java for ages. Things like this escape the mind if you haven't done it in a long time.
Good practice to clean up your own memory (Score:3, Informative)
Even though the OS will clean up after you, it's still a good idea to clean up after yourself. There is no absolute guarantee that subsequent versions of the kernel (or new operating systems) will do garbage collection, so the developer should either clean up the memory allocated or use a C/C++ garbage collector library to take care of such things.
HTH.
Re: (Score:1)
It's just so much more verb
Re: (Score:2)
Not necessarily... Here is a link to a garbage collection library for C [hp.com]. I have not used it outside of Digital Mars C/C++, but it had a decent reputation and it's free. Hopefully this will save you a bunch of work.
Re: (Score:1)
Re: (Score:2)
False. An OS that doesn't clean up after a dead process won't last long. I'm not saying that cleaning up after yourself isn't a good idea anyway, but if you're exiting the program, you can rely on the OS to do it for you.
Oh, and Jorg... use Valgrind [valgrind.org]. It's an invaluable tool when writing code in C.
Re: (Score:1)
Well, I did look up on wikipedia and AmigaOS didn't clean up. ;-)
I knew about Valgrind, I just don't know if I'll have enough time to learn it and investigate problems. I like to be very thorough, but this client has very strict deadlines.
Re: (Score:2)
Link please. I'm fairly sure it did...
I knew about Valgrind, I just don't know if I'll have enough time to learn it and investigate problems.
The only thing you need to know is valgrind --leak-check=full your_application. Then just fix any problems it tells you about.
Re: (Score:1)
Re: (Score:2)
You'd be surprised, actually. And, there's a good number of DOS machines stilol doing embedded stuff out there, and these are not very robust for stuff like that. While I agree on a general level, my point is that you really cannot depend on the OS all the time. You should be able to, especially with modern OSes, but that's just not always the case.
Re: (Score:1)
Re: (Score:2)
Re: (Score:1)
Because, as always, when I came there, no information was available on the project except it would be "Unix". So, I just picked one and hoped for the best. If I've got time spare, I'll install Solaris. Currently, I run Windows and have a Linux install in a VMWare. It's the company machine, I haven't as much freedoms as at home.