Allocation memory

Started by
27 comments, last by kitkit 15 years, 10 months ago
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.
----------------------

kit

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).
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.
-------------Please rate this post if it was useful.
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
----------------------

kit

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);
----------------------

kit

I don't whant to allocate 1gb of the memory in me app :) I'm not crazy. :) it's for test ..
----------------------

kit

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.
-------------Please rate this post if it was useful.
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.
I see, I'll check.
----------------------

kit

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.

This topic is closed to new replies.

Advertisement