Memory Management in VC
I just moved over from delphi to VC and I was a little confused when I realized that VC does not provide automatic mem-management as Delphi does. It seems to me like you have to deal with the same memory limitations as in DOS.
memtest array[0..1000000] works in delphi
char memtest[1000000] doesn''t work in VC.
As I''m used to delphi I thought windows does all the memory management for you!?
My question: How do I allocate Memoryblocks (in VC) 1MB or even 100MB in size? For storing Leveldata, Bitmaps and so on... in memory.
use void *malloc(size_t);
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
Actually, using "new" is the preferred method (though using new might actually just call malloc, depending on the compiler implementation.)
So it would be:
char *memtest = new char[1000000];
When you are done, make sure to delete memtest with:
delete [] memtest;
to check and make sure new succeeded, you should do the following:
if (memtest == NULL)
{
printf ("No memory left to allocate.");
return 0;
}
So it would be:
char *memtest = new char[1000000];
When you are done, make sure to delete memtest with:
delete [] memtest;
to check and make sure new succeeded, you should do the following:
if (memtest == NULL)
{
printf ("No memory left to allocate.");
return 0;
}
Alexander "DmGoober" Jhinalexjh@online.microsoft.com[Warning! This email account is not attended. All comments are the opinions of an individual employee and are not representative of Microsoft Corporation.]
quote: Original post by DmGoober
Actually, using "new" is the preferred method (though using new might actually just call malloc, depending on the compiler implementation.)
Well, strictly speaking ''new/delete'' is C++ and ''malloc/free'' is C.
quote:
char memtest[1000000] doesn''t work in VC.
What exactly do you mean? How does it ''not work''? It should.
jumble-----------
"new" is the preferred method of allocating memory. SAYS WHO. there are about 10 different ways i can think of to allocate memory, doesn''t mean i have to tell everyone about my personal opinions. the person asked how to allocate memory in C/C++. they didn''t ask "what is your preferred method of allocating memory?" how do you know they don''t want to use GlobalAlloc. and what makes "new" the preferred method. i know, it must be so hard to type cast.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
Thanks for the quick postings. The allocation now works perfectly, even for memoryblocks >200 MB. I''ll also check some other methodes, and deside which one works best for me.
Heeheheh
If I'm not mistaken you just declared an array the size of [0..1000000] or [0..200000000], but don't actually create an array that big in Delphi, right? This is for dynamically sized array's? or I'm I mistaken?
C & C++ actually support dynamic arrays, so you don't have to trick the compiler by declaring more space than you'll ever use.
and char memetest[1000000] should work in VC 4.x and later.
and Windows does some funky memory paging stuff, so if you declare 200MB of memeory, it isn't garuntted to be allocated until you try to use it - that doesn't mean the OS won't try if it gets bored before then. (I can hear the harddrive now....)
Edited by - Magmai Kai Holmlor on November 25, 2000 1:49:19 AM
quote:
memtest array[0..1000000] works in delphi
char memtest[1000000] doesn't work in VC.
If I'm not mistaken you just declared an array the size of [0..1000000] or [0..200000000], but don't actually create an array that big in Delphi, right? This is for dynamically sized array's? or I'm I mistaken?
C & C++ actually support dynamic arrays, so you don't have to trick the compiler by declaring more space than you'll ever use.
and char memetest[1000000] should work in VC 4.x and later.
and Windows does some funky memory paging stuff, so if you declare 200MB of memeory, it isn't garuntted to be allocated until you try to use it - that doesn't mean the OS won't try if it gets bored before then. (I can hear the harddrive now....)
Edited by - Magmai Kai Holmlor on November 25, 2000 1:49:19 AM
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement