Thread: The function VirtualAlloc( ) is suitable to the following application...?

  1. #1
    Registered User
    Join Date
    Jun 2009

    The function VirtualAlloc( ) is suitable to the following application...?

    LPVOID buffer1 = VirtualAlloc(NULL, 1048576 ,MEM_COMMIT,PAGE_READWRITE); // 1048576 = 1MB

    Here I allocated memory of 1 MB.And in my application my data is 512 bytes blocks....So I've to check some conditions if that satisfies then that data block must copy to buffer.And when the buffer fills full(1 mb totally used ) I will copy that to another buffer and frees the Allocated buffer1.

    I wanna append small small data blocks to the buffer of 1 mb. How can I do this..Is it possible with VirtualAlloc( ) ......?

             Assume   data size =512 bytes
             while( some coditions)
           if( buffer1 is not fully used)
               copy 512 bytes data to buffer1
             LPVOID buffer2 = VirtualAlloc(NULL, 1048576 ,MEM_COMMIT,PAGE_READWRITE); // 1048576 = 1MB
             //copy to buffer2

  2. #2
    Registered User
    Join Date
    Oct 2006
    the code
    does not perform a copy. it only assigns the value of one pointer to another, causing the memory allocated by VirtualAlloc() to be leaked. you go on to free the pointer to buffer1, which is now the same as buffer2, making both pointers invalid. you have to use memcpy or something similar in order to copy the contents of buffer1 into the memory block you allocated.

    LPVOID buffer2 = VirtualAlloc(NULL, 1048576 ,MEM_COMMIT,PAGE_READWRITE);
    memcpy(buffer2, buffer1, 1048576);
    hopefully this was helpful.

  3. #3
    Join Date
    Oct 2007
    Inside my computer
    This begs the question why VirtualAlloc is necessary? Won't new cut it?
    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.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    I too fail to see why VirtualAlloc is needed here. Regular use of new/malloc would be more portable, and support exactly the same things. You are not using any of the advanced features that aren't available in new/malloc (such as setting the memory executable, or changing the read/write attributes).

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  2. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  3. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 09:52 AM
Website Security Test