free and coreleft calls

B

Britt Hibbert

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
 
E

Eric Sosman

Britt said:
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
 
M

Malcolm

Britt Hibbert said:
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.
 
B

Ben Pfaff

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top