Professional Web Applications Themes

nastiness of C memory management - Mac Programming

I've been experimenting with some fast sorting algorithms in Java on my PowerBook (MacOS 10.3.2), but the algorithms were posted in C by their originators, and I want to port my routines to C for direct comparison. I did this using an old version of CodeWarrior that runs in OS 9, with partial success. The original algorithms run okay, but my versions crash frequently, probably due to memory leaks that I didn't have to worry about in Java. I'm looking for advice on the easiest solution. Is there a garbage collector library that can easily be added to C on ...

  1. #1

    Default nastiness of C memory management

    I've been experimenting with some fast sorting algorithms in Java on my
    PowerBook (MacOS 10.3.2), but the algorithms were posted in C by their
    originators, and I want to port my routines to C for direct comparison.
    I did this using an old version of CodeWarrior that runs in OS 9, with
    partial success. The original algorithms run okay, but my versions
    crash frequently, probably due to memory leaks that I didn't have to
    worry about in Java.

    I'm looking for advice on the easiest solution. Is there a garbage
    collector library that can easily be added to C on a Mac? If so, could
    it be linked with my OS 9 CodeWarrior compiled code, or would I need to
    migrate that to xCode?

    Would migrating to xCode in itself be likely to reduce my memory
    management problems? The program is not set up with any user interface;
    my Java version just uses Terminal for io, and the C version uses a
    SIOUX window.

    If there's a better forum in which to find help on this, please steer me
    to it.

    Thanks,

    Dave Ring

    David Guest

  2. #2

    Default Re: nastiness of C memory management

    I would suggest just using the CodeWarrior debugger to find
    the problems, it works well and is easy to learn. By the
    way, at least some old versions of CodeWarrior run just fine
    in classic mode under OS X.

    Good luck!

    In article <D0gSb.17783$news.prodigy.com>, David B Ring
    <net> wrote:
     
    rob Guest

  3. Moderated Post

    Default Re: nastiness of C memory management

    Removed by Administrator
    Jim Guest
    Moderated Post

  4. #4

    Default Re: nastiness of C memory management

    In article <D0gSb.17783$news.prodigy.com>,
    David B Ring <net> wrote:
     

    You're mischaracterizing the problems. Memory leaks will never cause a
    crash unless you're leaking tons of memory -- hundreds and hundreds of
    megabytes. Crashes come from references to invalid memory. In other
    words, a garbage collector won't help you, but running your code inside
    Xcode's debugger and doing some debugging should help you track the
    problems down.

    -Eric

    --
    Eric Albert stanford.edu
    http://rescomp.stanford.edu/~ejalbert/
    Eric Guest

  5. #5

    Default Re: nastiness of C memory management

    Probably I am mischaracterizing the problem, but I'm also working with a
    lot of strings and a lot of memory. In Java, I was working with a
    character buffer of 100,000,000 bytes and data sets containing up to
    18,000,000 keys, and I had to use the -Xmx384m switch to run Java with
    increased memory. In C, I have the minimum heap size set to 384 mB, but
    I don't seem to be able to run successfully with more than 50,000,000
    chars or 5,000,000 strings.

    It may well be that all the crashes are coming from invalid references,
    but it's also possible that a modest memory leak could get to hundreds
    of mB fairly quickly.

    Anyway, I think you're right that a garbage collector would not solve my
    problem. It's not just the garbage collector in Java that was
    sheltering me, but also the relative safety of Java handles versus C
    pointers.

    Thanks,

    Dave

    Eric Albert wrote: 
    >
    >
    > You're mischaracterizing the problems. Memory leaks will never cause a
    > crash unless you're leaking tons of memory -- hundreds and hundreds of
    > megabytes. Crashes come from references to invalid memory. In other
    > words, a garbage collector won't help you, but running your code inside
    > Xcode's debugger and doing some debugging should help you track the
    > problems down.
    >
    > -Eric
    >[/ref]

    David Guest

  6. #6

    Default Re: nastiness of C memory management

    Yes, my version of CW seems to do quite well in classic mode, and
    booting in OS 9 does not help any of the problems.

    Dave

    rob shaw wrote:
     [/ref]

    David Guest

  7. Moderated Post

    Default Re: nastiness of C memory management

    Removed by Administrator
    David Guest
    Moderated Post

  8. #8

    Default Re: nastiness of C memory management

    In article <PtzSb.19722$news.prodigy.com>,
    David B Ring <net> wrote:
     

    One other thing you should keep in mind, particularly on OS 9, is what
    happens if you run out of memory. In Java an exception is thrown and you
    get a nice message. In C, malloc() or calloc() will simply return NULL.
    You *must* check for this if there is any possibility that you can run
    out of memory. If malloc() returns NULL and you then use that pointer to
    store something, you will crash instantly. On most UNIX systems, it's
    not a worry because of the VM system; if you can't allocate memory, this
    usually means that your program and your computer are hosed, and there's
    no point in a graceful recovery. But on OS 9, with fixed application
    partition sizes, it can happen easily and you'll want to do something
    other than crash.
    Michael Guest

  9. #9

    Default Re: nastiness of C memory management

    David B Ring <net> writes:
     

    I've always had the habbit of assigning null to a pointer after
    freeing it. It is safe to call free on a null pointer. Trying to
    reference it will always fail.

    --
    One Emacs to rule them all. One Emacs to find them,
    One Emacs to take commands and to the keystrokes bind them,

    All other programming languages wish they were Lisp.
    David Guest

Similar Threads

  1. memory management
    By Rastermaster in forum Macromedia Director 3D
    Replies: 2
    Last Post: March 22nd, 10:20 PM
  2. go to movie and memory management
    By David in forum Macromedia Director Lingo
    Replies: 4
    Last Post: January 28th, 09:53 PM
  3. Memory management on mac
    By toberttobert webforumsuser@macromedia.com in forum Macromedia Director Lingo
    Replies: 1
    Last Post: October 18th, 03:52 PM
  4. memory management questions
    By multimediacrazy webforumsuser@macromedia.com in forum Macromedia Director Lingo
    Replies: 2
    Last Post: July 17th, 12:15 PM
  5. perl memory management - does array = () free the memory?
    By Matt Oefinger in forum Perl / CGI
    Replies: 0
    Last Post: June 25th, 09:11 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139