delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/03/11/16:03:37

From: gpt20 AT thor DOT cam DOT ac DOT uk (G.P. Tootell)
Newsgroups: comp.os.msdos.djgpp
Subject: problem with malloc in djgpp?
Date: 11 Mar 1997 18:42:03 GMT
Organization: University of Cambridge, England
Lines: 65
Sender: gpt20 AT hammer DOT thor DOT cam DOT ac DOT uk (G.P. Tootell)
Message-ID: <5g491r$mbg@lyra.csx.cam.ac.uk>
NNTP-Posting-Host: hammer.thor.cam.ac.uk
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

i remember seeing something just recently about memory fragmentation problems
with djgpp malloc and wondered if it was decided whose fault this was :)
i have a program which has weird malloc problems and wondered if fragmentation
could be the problem.
on startup the program claims (under pure dos) that it has 0.7meg real and 121meg
virtual memory available. it now allocates some blocks of memory but in doing so
causes a page fault. probably due to the incredibly low amount of real memory i
thought, so i tried to run this under win3.1
now i get 7.1meg real and 120meg virtual available. this should be enough to 
allocate the 14meg buffer that i require though. 
dpmi_free_memory, or whatever the hugely long name for it is, says it has
127.1meg total therefore. but malloc returns a null pointer when i try to dim the
block. why on earth should this be the case? the program works fine on a 32meg
machine incidentally. the problem only occurs on 8 and 16 meg machines. the
program is not locked in memory, it uses cwsdpmi.exe so should have access to the
virtual memory right?

this is the code i use : (long lines got wrapped with ...'s)

it reports the memory as described above then says malloc failed. despite telling
me it had at least 14meg and me only malloc'ing 6 meg of it!

thanks.
nik

---

void main()
{
        printf("%.1fMB (+%.1fMB) of memory available.\n",
        ((float)_go32_dpmi_remaining_physical_memory())/0x100000,
        ((float)_go32_dpmi_remaining_virtual_memory())/0x100000);
        // we need 14 meg just to decode these files
        needmem(6L*1024*1024+1024L*1024*8);
        if ((ydata=(unsigned char *)malloc(6L*1024*1024))==NULL) {
		// ydata is defined in a header file as unsigned char *ydata
                // a really bad error which should never have happened.
                closedown();
                printf("Insufficient memory to run. malloc failed.\n");
                printf("a truly bad thing happened and should have been...
		caught by needmem.\n");
                exit(0);
        }
}

unsigned int needmem(unsigned int amount)
{
        if (_go32_dpmi_remaining_physical_memory()+_go32_dpmi_remaining_...
		virtual_memory()<amount) {
                // not enough memory for upcoming mallocs
                closedown();            // shut down system
                printf("Insufficient memory to run\n");
                printf("(Request for %.1fMB but only %.1fMB of memory...
		available)\n",(float)amount/0x100000,((float)_go32_dpmi_...
		remaining_physical_memory()+_go32_dpmi_remaining_virtual_...
		memory())/0x100000);
                exit(0);
        }
        return(_go32_dpmi_remaining_physical_memory());
	// the above line is deliberate.
}



-- 

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019