Thread: RtlMoveMemory Problem?

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    9

    Question RtlMoveMemory Problem?

    I'm just starting to work with c++(using Dev c++) and have ran into a problem. I was following a tutorial and copied and pasted this function into my program, there where some errors but I managed to fix them so it would compile. Now it silently crashes on this line:
    Code:
    RtlMoveMemory(Bits, pDIB + sizeof(BITMAPINFOHEADER), bih.biSizeImage);
    I have provided the function below and have surrounded the crashing line with stars. I would really appreciate any help.

    Code:
    BOOL CreateFromPackedDIBPointer(LPBYTE pDIB, int iFrame, int framenumber)
    {
        if (pDIB!=NULL);
    {
        
        //Creates a full-color (no palette) DIB from a pointer to a
        //full-color memory DIB
    
        //get the BitmapInfoHeader
        BITMAPINFOHEADER bih;
        
        RtlMoveMemory(&bih.biSize, pDIB, sizeof(BITMAPINFOHEADER));
    
        //now get the bitmap bits
        if (bih.biSizeImage < 1)
        {
            return FALSE;
        }
    
        BYTE* Bits=new BYTE;
    
    //*****************************************************************************************
        RtlMoveMemory(Bits, pDIB + sizeof(BITMAPINFOHEADER), bih.biSizeImage);
    //*****************************************************************************************
    
        //and BitmapInfo variable-length UDT
        BYTE memBitmapInfo[40];
        RtlMoveMemory(memBitmapInfo, &bih, sizeof(bih));
    
        BITMAPFILEHEADER bfh;
        bfh.bfType=19778;    //BM header
        bfh.bfSize=55 + bih.biSizeImage;
        bfh.bfReserved1=0;
        bfh.bfReserved2=0;
        bfh.bfOffBits=sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER); //54
        
        FileName=("Frame-",(char)framenumber,".bmp");
        
        FILE* fp=fopen(FileName, "wb");
        if (fp!=NULL)
        {
            fwrite(&bfh, sizeof(bfh), 1, fp);
            fwrite(&memBitmapInfo, sizeof(memBitmapInfo), 1, fp);
            fwrite(Bits, bih.biSizeImage, 1, fp);
            fclose(fp);
        }
        else
        {
            TRACE0(_T("Error writing the bitmap file"));
            return FALSE;
        }
    
        delete [] Bits;
        return TRUE;
    }
    }
    Thanks for reading!

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You're allocating a single byte and then copying bih.biSizeImage bytes into it. I would presume that bih.biSizeImage is a number greater than one.

    You've also mismatched new and delete[]. I would guess that you meant to use new[] with an appropriate size.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Maybe you should make Bits point somewhere useful (via new) before trying to use it.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    9
    Like I said i'm just getting into c++ and copied and pasted this function but I will try to understand.

    You're allocating a single byte and then copying bih.biSizeImage bytes into it. I would presume that bih.biSizeImage is a number greater than one.
    Ok, that seems to make sense to me. How about this?
    Code:
    BYTE* Bits[sizeof(bih.biSizeImage)];
    You've also mismatched new and delete[]. I would guess that you meant to use new[] with an appropriate size.
    I don't really know why thats there since this function is in a for loop and it seems like it just gets redeclared anyway, but I probably misunderstand somthing.

    Maybe you should make Bits point somewhere useful (via new) before trying to use it.
    I don't really understand what you mean? Once again me being a "noob" at c++.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by DBProgrammer View Post
    Like I said i'm just getting into c++ and copied and pasted this function but I will try to understand.



    Ok, that seems to make sense to me. How about this?
    Code:
    BYTE* Bits[sizeof(bih.biSizeImage)];


    I don't really know why thats there since this function is in a for loop and it seems like it just gets redeclared anyway, but I probably misunderstand somthing.



    I don't really understand what you mean? Once again me being a "noob" at c++.
    I meant exactly what iMalc said, but without all the typing. You need bih.biSizeImage amount of space, as you just said in what I quoted right there, so get it:
    Code:
    BYTE *Bits = new BYTE[bih.biSizeImage];
    And since you are doing new [] to get a bunch of memory, you need delete [] to get rid of it.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    9
    Thanks I got it working.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    When you do:
    T* myvar[size];
    That myvar is of mysize elements and points to... well, that's the problem. You don't know where. Probably nowhere good anyway. Probably to some memory you don't own, so the OS will bark when you try to write or read from there.
    Using the new keyword, on the other hand, asks the OS for some memory for storing n elements of type T and returns that address, so it's valid to read and write to/from there.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM