free and coreleft calls

Discussion in 'C Programming' started by Britt Hibbert, Aug 20, 2003.

  1. Hi all

    I am working on a 16 bit DOS program, and using Borland to compile my
    program.


    As a debugging step I have put in the
    long coresize = coreleft();
    printf("Core left in bytes %ld\n", coresize);
    lines of code every time I calloc and free memory. The structs that I
    am dynamically allocating and freeing are stored in a linked list, and
    I have stepped through the memory freeing code with the TD.exe DOS
    debugger. When a node in the linked list is removed, the step through
    with the DOS debugger shows me that pointers are being redirected
    property etc, and that the pointer passed to free points to the
    correct memory location. However, the subsequent call to coreleft does
    not report this extra memory after it has been freed.

    Subsequent calls to calloc result in different reports from coreleft -
    at times after I call calloc, the reported coreleft() value does not
    change - I assume this is because the calloc received a pointer to the
    memory that was earlier freed but not reported freed by coreleft().
    However, at other times, the coreleft value after the calloc IS
    smaller, as a result it looks like my program is leaking memory.
    However, as I have said, I have stepped through the code with a
    debugger numerous times to ensure that pointers were redirected
    properly and that the correct pointer was passed to free()

    any ideas on what is going on? I am wondering if coreleft is not
    reporting the freed memory or if I am doing something wrong.

    thanks
    Britt Hibbert
    Britt Hibbert, Aug 20, 2003
    #1
    1. Advertising

  2. Britt Hibbert

    Eric Sosman Guest

    Britt Hibbert wrote:
    >
    > Hi all
    >
    > I am working on a 16 bit DOS program, and using Borland to compile my
    > program.
    >
    > As a debugging step I have put in the
    > long coresize = coreleft();
    > printf("Core left in bytes %ld\n", coresize);
    > lines of code every time I calloc and free memory. [...]
    > [... and the results are mysterious.]


    coreleft() is not part of the Standard C library, so
    there's no telling what it might report or what the report
    means. Consult your Borland documentation or a Borland
    newsgroup. Also, see Question 7.25 in the comp.lang.c
    Frequently Asked Questions (FAQ) list

    http://www.eskimo.com/~scs/C-faq/top.html

    --
    Eric Sosman, Aug 20, 2003
    #2
    1. Advertising

  3. Britt Hibbert

    Malcolm Guest

    "Britt Hibbert" <> wrote in message
    >
    > As a debugging step I have put in the
    > long coresize = coreleft();
    > printf("Core left in bytes %ld\n", coresize);
    >

    coreleft() isn't an ANSI function. The reason is that, on many systems, it
    is not quite clear what constitutes "free" memory.
    If you are using an old 16-bit DOS compiler on a new machine it wouldn't
    surprise me if funny things are going on with the memory behind the scenes,
    so "coreleft" doesn't do what you expect it to.
    Malcolm, Aug 20, 2003
    #3
  4. Britt Hibbert

    Ben Pfaff Guest

    (Britt Hibbert) writes:

    > ...the subsequent call to coreleft does not report this extra
    > memory after it has been freed.


    This is in the C FAQ:

    7.25: I have a program which mallocs and later frees a lot of memory,
    but I can see from the operating system that memory usage
    doesn't actually go back down.

    A: Most implementations of malloc/free do not return freed memory
    to the operating system, but merely make it available for future
    malloc() calls within the same program.
    --
    "Some programming practices beg for errors;
    this one is like calling an 800 number
    and having errors delivered to your door."
    --Steve McConnell
    Ben Pfaff, Aug 20, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Honne Gowda A
    Replies:
    2
    Views:
    872
    Karl Heinz Buchegger
    Oct 31, 2003
  2. andy6
    Replies:
    2
    Views:
    756
    andy6 via DotNetMonster.com
    Jun 9, 2006
  3. Richard Tobin
    Replies:
    24
    Views:
    783
  4. george
    Replies:
    0
    Views:
    1,106
    george
    Aug 29, 2008
  5. mohammed_a_o
    Replies:
    0
    Views:
    269
    mohammed_a_o
    Nov 30, 2010
Loading...

Share This Page