Thread: VirtualAlloc help

  1. #1
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654

    VirtualAlloc help

    If someone knows how to use VirtualAlloc properly, I could certainly use some help, because I really don't understand why it acts like it does and the doc is of no help.

    Take this for example:
    Code:
    	int* p = (int*)VirtualAlloc(NULL, 1024 * 4, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
    	int* p2 = (int*)VirtualAlloc((void*)((DWORD)p + 1024 *4 + 1), 1024 * 4, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
    	for (int i = 0; i <= 4096; i++) p[i] = 0;
    The first only allocates 1024 bytes, although I ask for 4K.
    The doc clearly says (for dwSize, the second argument):
    Code:
    If the lpAddress parameter is NULL, this value is rounded up to the next page boundary.
    And since a page size is 4K, I should get 4K, should I not? Whatever I specify, I get only 1024 bytes as apparent from the for loop which fails on i = 1024 (so 0 - 1023 works without access violations).
    The second VirtualAlloc simply fails with "an attempt to use an invalid address."
    It's only (or is supposed to be) only 1 byte after the first allocation.
    But I can't get any address that way, and I have no idea why.

    IF anyone could help me understand VirtualAlloc... I want to play a little with it, but the doc is of absolutely no help at all.
    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.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    I'm guessing sizeof(int) is 4 (not 1).

    And <= would make you overrun.
    Last edited by robwhit; 12-09-2007 at 06:53 AM.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Ah... silly bug!
    /slaps self.
    Last edited by Elysia; 12-09-2007 at 06:58 AM.
    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
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Why not use ZeroMemory() or even the portable memset()?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It was merely a test to see if I could access all the allocated memory.
    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.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    As for the second, aren't you ignoring the alignment requirement by trying to go 1 byte after the previous allocation?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Dunno. It's hard to tell usually. Such things are usually found out by debugging. But, in general, I think, maybe it is.
    I'm starting to get the hang of it, but I'm still having trouble with some of the code using it (and the example above isn't it).
    I could post the entire code that I'm working with, but I'm betting it's not going to be that fun to go through it to find mistakes.
    I'm doing an experiment with VirtualAlloc since it can reserve and allocate memory ranges anywhere in the virtual memory, so I don't have to copy data when I expand an array.

    UPDATE:
    It seems that it's paying off!
    Around 200 ms vs. 350 ms in Release for VirtualAlloc vs. Heap for 10 * 7 iterations!
    Wow! Now I just need to perfect this VirtualAlloc code!
    Last edited by Elysia; 12-09-2007 at 09:13 AM.
    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.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Just checked, and it should actually just silently round down.

    Hmm, but what is the allocation granularity on your computer?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    4K - aka 4096 bytes.
    I need more testing, of course. Using a bigger buffer shows performance gains and I haven't been able to make it possible to continue to reserve pages after the allocates one seamlessly for some reason.
    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.

  10. #10
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Maybe this can be of some help to you.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Already saw that example.
    I don't know why people think I still need help, though I don't... really. I think.
    I think I just found my infamous bug that was screwing up my tests.

    UPDATE:
    Code:
    	Stuff::CArray<char> Array;
    	double max = pow(10.0, 8.6);
    	DWORD dwTick1 = GetTickCount();
    
    	for (int i = 0; i < max; i++)
    		Array[i] = 12; //12345678;
    
    	DWORD dwTick2 = GetTickCount();
    	DWORD dwTick3 = dwTick2 - dwTick1;
    With heap functions:
    10687 ms
    10984 ms

    With VirtualAlloc:
    8156 ms
    8094 ms


    Now I just have to try a bigger buffer! The bigger the faster.
    With some optimizations, that time is now 1609 ms!
    Now if anyone could just optimize assembly...
    Last edited by Elysia; 12-09-2007 at 12:35 PM.
    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. Replies: 3
    Last Post: 06-10-2009, 01:55 PM
  2. new invokes VirtualAlloc
    By George2 in forum C++ Programming
    Replies: 12
    Last Post: 01-29-2008, 03:58 AM
  3. VirtualAlloc() and free()?
    By cpjust in forum Windows Programming
    Replies: 2
    Last Post: 01-21-2008, 10:55 AM
  4. new and VirtualAlloc
    By George2 in forum C++ Programming
    Replies: 12
    Last Post: 01-13-2008, 09:13 AM
  5. VirtualAlloc v.s. malloc
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 11-03-2007, 07:50 AM