_fmemset , it crashes, why?

This is a discussion on _fmemset , it crashes, why? within the C++ Programming forums, part of the General Programming Boards category; offscreen= (char *) farmalloc(64000u); _fmemset(offscreen,0,64000u); i've tried to debug and after executing the _fmemset statement it crashes...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    11

    _fmemset , it crashes, why?

    offscreen= (char *) farmalloc(64000u);
    _fmemset(offscreen,0,64000u);

    i've tried to debug and after executing the _fmemset statement it crashes

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    11

    farmalloc

    why does farmalloc always return NULL in my comp ?

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    1. have you prototyped it by including the right .h file
    2. remove the cast - a) its broken, b) its not needed
    (char far*) would be better

    3. is offscreen also declared as a far pointer

    4. why are you still messing around in the stone age?

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    11

    Talking any solution ?

    Salem, do u have a better solution ?, what i intend to is to allocate memory above 64K , and the only thing i know is using farmalloc

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    First of all you need to check to make sure that farmalloc did in fact allocate memory. If it did not, then your fmemset will be trying to set memory via a null pointer which will crash because the pointer could be pointing at anything.


    I don't use the *allocs.
    Code:
      unsigned char far *Buffer=new unsigned char[64000L];
    
      memset(Buffer,0,64000L);
    This should work.

    A faster method in 16-bit DOS though is this. This is inline assembly.

    Code:
    //16-bit memset
    void memset16(unsigned char far *Buffer,unsigned int length)
    {
      asm  {
         cld                           //set to  increment for DI                            
         les    di,Buffer          //load full pointer in es:di
         mov  cx,length        //set for length
         shr cx,1                   //convert from bytes to integers
         mov  al,0                //set al to 0
         mov  ah,0               // set ah to 0 ->  mov ax,0000h
         rep   stosw             //store ax in ES:DI for CX length, inc DI
       }
    }
    But it will not work within the IDE for DOS programs, only in Borland for Windows. The IDE in DOS takes up too much RAM so this will always return a null pointer. This means that your code must be tested from the command line and not from within the IDE. Borland C/C++ for Windows does not suffer from this problem.

    The asm code will transfer words instead of bytes. So it will transfer 16 bits of information per iteration instead of 8. A faster method would be stosd, but you would have to do some operand overrides to get it to work. I left it out because it would only confuse you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program crashes and memory leaks
    By ulillillia in forum Tech Board
    Replies: 1
    Last Post: 05-15-2007, 10:54 PM
  2. DEV-C++ made program crashes when run from outside the ide
    By rainmanddw in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2006, 09:27 PM
  3. Win2k Explorer crashes
    By Spectrum48k in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 05-27-2002, 04:22 PM
  4. program crashes
    By Strut in forum Linux Programming
    Replies: 3
    Last Post: 02-10-2002, 09:49 AM
  5. Program crashes and I can't figure out why
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 09-19-2001, 05:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21