Jump to content
  • Advertisement
Sign in to follow this  
kitkit

Allocation memory

This topic is 3618 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. I have a problem: after creation of the window, I cannot do any allocation in the memory. I do such a way: mMainWnd = CreateWindowEx( style_ex, (LPCTSTR)RegisterClassEx(&wcex), NULL, style, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, mInstance, NULL); char* tt = new char[1000000000]; // in this string I have an exception. hear is the output: First-chance exception at 0x7c812aeb in test_appD.exe: Microsoft C++ exception: std::bad_alloc @ 0x00dcf214. Unhandled exception at 0x7c812aeb in test_appD.exe: Microsoft C++ exception: std::bad_alloc @ 0x00dcf214. The program '[3188] test_appD.exe: Native' has exited with code -529697949 (0xe06d7363). error code is: 8 ERROR_NOT_ENOUGH_MEMORY 8 Not enough storage is available to process this command. This says that have no memory, but I have 2gb of the memory. I can say that if my code will be such: char* tt = new char[1000000000]; // in this string I have an exception. mMainWnd = CreateWindowEx( style_ex, (LPCTSTR)RegisterClassEx(&wcex), NULL, style, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, mInstance, NULL); I have no problems!! Help me please.

Share this post


Link to post
Share on other sites
Advertisement
Allocating 1GB of memory is very likely to fail - you don't need 1GB of free memory, you need 1GB of contiguous address space, the OS will handle swapping pages in and out of RAM as needed. The problem is that calling CreateWindow() probably causes some DLLs to be loaded, and they fragment your address space.

The only sensible things to do would be to allocate a much smaller chunk of memory (No more than a couple of hundred MB), use memory mapped files with a smaller view (if you need to load in massive files), or switch to a 64-bit build (And OS, and CPU).

Share this post


Link to post
Share on other sites
Why in the blazes would ever need to allocate one GIGABYTE of chars?

Anyway, the error occurs because you either don't have one gigabyte of free memory (check your task manager or equivalent), or because you don't have one gigabyte of contiguous free memory. Either case will cause your allocation to fail.

But you almost certainly don't need to allocate a gigabyte of chars. What exactly are you trying to accomplish? Maybe we can propose a better solution.

Share this post


Link to post
Share on other sites
My real problem was, that I couldn't allocate mem in runtime, I have analogous exception. But in this time I allocated about 40 mb of mem in the heap. So I tryied to manually allocate a huge amount of mem, from begining of the program and I see the place where I have such an exception

Share this post


Link to post
Share on other sites
You are not right. I HAVE 1,5 gb of memory and I say that if i write sych code, everything will be ok:


char* tt = new char[1000000000]; // in this string I have an exception.

mMainWnd = CreateWindowEx(
style_ex,
(LPCTSTR)RegisterClassEx(&wcex),
NULL,
style,
rect.left,
rect.top,
rect.right - rect.left,
rect.bottom - rect.top,
NULL,
NULL,
mInstance,
NULL);

Share this post


Link to post
Share on other sites
I don't whant to allocate 1gb of the memory in me app :) I'm not crazy. :) it's for test ..

Share this post


Link to post
Share on other sites
If you have a sufficient amount of free memory, then you don't have a sufficient amount of free contiguous memory as Evil Steve and myself previously pointed out. As Steve said, loading a window probably loads certain DLL's which fragment your memory.

Share this post


Link to post
Share on other sites
Quote:
Original post by kitkit
You are not right. I HAVE 1,5 gb of memory and I say that if i write sych code, everything will be ok:


char* tt = new char[1000000000]; // in this string I have an exception.

mMainWnd = CreateWindowEx(
style_ex,
(LPCTSTR)RegisterClassEx(&wcex),
NULL,
style,
rect.left,
rect.top,
rect.right - rect.left,
rect.bottom - rect.top,
NULL,
NULL,
mInstance,
NULL);
Yes, because the DLLs can't load at their preferred address because the address is in use. Windows can relocate DLLs to a specific address, which is what will be happening here.

As a test, look at your debug output and see what DLLs are loaded when you call CreateWindow(). Then use GetModuleHandle() on each of those DLLs in turn and print out the return value (Which is the base address for that DLL). You'll see that if you allocate 1GB of memory before calling CreateWindow, the addresses are different because of relocation.

Share this post


Link to post
Share on other sites
Also, just because you have 1.5 GB of memory does not mean that all of that is available for user space applications. A portion of your memory is devoted to kernel space applications.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!