[comp.lang.c] MSC 6.0 malloc question

kdq@demott.COM (Kevin D. Quitt) (06/05/90)

In article <36163@sequent.UUCP> brett@sequent.UUCP (Brett Leichner) writes:
>
>I'm writing a part C/part assembly routine that needs to be able to allocate and
>use a full segment (65535 bytes).  Here are the specifics:
>
>The problem is that if "num_bytes" is greater than about 65550 then _fmalloc
>returns null trying to allocate the first buffer.

>1.  Does DOS use these extra bytes for house-keeping?

    When DOS allocates memory, it requires 1 paragraph (16 bytes out of your
63356 (not 65535) bytes.



>2.  Is it possible to allocate all 65535 bytes of a segment for use? (dynamic
>    allocation only).

    Yes - declare your pointer as huge or use the huge model to compile


>3.  If DOS needs some of the memory for house-keeping, is it possible to
>    use the huge model, and allocate say 66000 bytes in such a way that the
>    house-keeping stuff will be pushed over into another segment thereby
>    giving me at least one full segment?

    Every paragraph in DOS starts a new segment, so the DOS house-keeping
will be at NNNm:0 and your memory will be at NNNn:0 where n = m + 1

>4.  On a side note, using the hugh model and declaring arrays that span multiple
>    segments is there a way to guarantee that the array will be composed of
>    segments physically adjacent?

    It shouldn't matter in the huge model - the compiler does all the extra
work for you. (can you say slow? I knew you could).


-- 
 _
Kevin D. Quitt         Manager, Software Development    34 12 N  118 27 W
DeMott Electronics Co. 14707 Keswick St.   Van Nuys, CA 91405-1266
VOICE (818) 988-4975   FAX (818) 997-1190  
MODEM (818) 997-4496 Telebit PEP last      demott!kdq   kdq@demott.com

      96.37% of the statistics used in arguments are made up.

jta@locus.com (JT Anderson) (06/07/90)

You cannot allocate 65535 bytes using malloc or its brethren in Microsoft
C.  The largest chunk you can allocate is somewhere in the neighborhood
of 65520 bytes, and you can be sure the pointer will NOT have a 0 offset.

If you need to allocate a full segment there are a couple of choices.  You
can use halloc, which will return a pointer with a zero offset under most
circumstances.  The offset will certainly be 0 if you pass a size parameter
of 1.  However, this is not guaranteed by the function definition.

Another choice is to use the _dos_allocmem function.  This function will
provide you with a segment value, which you must use to construct a far
pointer.