Become a fan of Slashdot on Facebook

 



Forgot your password?
typodupeerror
Programming

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.

This discussion has been archived. No new comments can be posted.

C advice needed

Comments Filter:
  • by GeckoFood ( 585211 ) * <geckofood AT gmail DOT com> on Friday February 15, 2008 @04:42PM (#22439062) Journal

    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.

    • Pretty much guessed that. So, I have to rewrite a bunch of stuff. What I actually did was centralise the error reporting in a function that returns EXIT_FAILURE. I instead of return ( EXIT_FAILURE );, it said return ( generateError( leak ) ); (using the information in char* to report the error at hand) and generateError returns EXIT_FAILURE, but doesn't do any cleanup. So, I'll have to change that everywhere to:

      generateError( leak );
      free( leak );
      return ( ERROR_FAILURE );

      It's just so much more verb

    • by Tet ( 2721 )
      There is no absolute guarantee that subsequent versions of the kernel (or new operating systems) will do garbage collection

      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.

      • 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.

        • by Tet ( 2721 )
          Well, I did look up on wikipedia and AmigaOS didn't clean up

          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.

      • but if you're exiting the program, you can rely on the OS to do it for you.

        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.

        • Well, I know the ultimate target is Solaris.... So, well, that qualifies as "modern operating system". I know about DOS.... I have deployed a FreeDOS installation to control a robot at my father in laws company.
          • by elmegil ( 12001 ) *
            I'm a solaris geek. While good programming practice says always clean up after yourself, I can guarantee you that when your process exits (return whatever from main()) the OS will clean it up. It may not be clean coding but the question in my mind is whether or not you want to obfuscate your code enough to be perfect. That's not really a memory leak unless there's a non-negligible chance your process will become a zombie. As far as it goes, if you want to eventually run on solaris, why aren't you develo
            • As far as it goes, if you want to eventually run on solaris, why aren't you developing on solaris now? The download is free (as in beer).....

              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.

The system was down for backups from 5am to 10am last Saturday.

Working...