April 29, 2024, 03:04:49 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


ALLOCMEM

Started by REDEBOLT, September 24, 2007, 09:55:47 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

REDEBOLT

I attempted to test the allocmem program from the help file, but it fails to work.
I don't understand why.

$Main
autodefine "off"
openconsole

DEF mymem as MEMORY
IF ALLOCMEM(mymem,100,10)
    PRINT "Allocated 1000 bytes of memory"
    FREEMEM mymem
else
    print "Error in allocation"
ENDIF

DO:UNTIL INKEY$ <> ""
closeconsole
END
Regards,
Bob

Barney

Read the manual. Here's the important part about MEMALLOC function:

Return value
Returns 0 on success or -1 on failure. Function may fail if the size requested is larger than the amount of free memory available.

So, what you need is either change the test like this:

IF ALLOCMEM(mymem,100,10)=0

or change the position of messages like this:


IF ALLOCMEM(mymem,100,10)
    print "Error in allocation"
else
    PRINT "Allocated 1000 bytes of memory"
    FREEMEM mymem
ENDIF


Barney

REDEBOLT

 :-[
Thanks Barney, I should RTFM.  I took the example at face value and it is apparently wrong and should be corrected.
Regards,
Bob