Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

cruZ

Memory Management in VC

This topic is 6415 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

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.

Share this post


Link to post
Share on other sites
Advertisement
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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
"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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Heeheheh

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

Share this post


Link to post
Share on other sites

  • 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!