Archived

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

My memory is gone, help

This topic is 5623 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''m supposed to have 128 MB of Ram but the little game I created will not even start if I try to make another int variable, or an array or anything. It''s at max memory usage! I created a new Visual C== project just to test how many char arrays I can make... I made one char A[1000000] and 3 char B[10000] which is 1,030,000 and then I can make no more variables. So my question is what do I do? Is there something wrong with my computer or with Visual C++? Can I start saving to files? Or should I start using stuff like: ClassA *p=new ClassA; and then delete *p; p=NULL; when I''m done using an instance of this class. Or what do I do? What do people do? Howcome my game has 1 executable and a few bitmaps while commercial games have like 100''s of files! hehe

Share this post


Link to post
Share on other sites
quote:
Original post by GiantPaul
and then I can make no more variables.

what exactly do you mean by this?

the stack size is generally limited to 1 mb, so you should avoid having large arrays as automatic variables. use globals or dynamic memory, or if you need really big chunks (measured in megabytes) VirtualAlloc will probably be a better choice.

---
Come to #directxdev IRC channel on AfterNET

Share this post


Link to post
Share on other sites
Ahhh So globals are not on the stack?

So the Stack has a limit of 1 MB
What is the limit on globals?
What is the limit on the dynamic memory?

What is RAm made up of?

Share this post


Link to post
Share on other sites
quote:
Original post by GiantPaul
What is the limit on globals?
= Physical_RAM + Virtual_RAM - RAM_in_use;
quote:
What is the limit on the dynamic memory?
= Physical_RAM + Virtual_RAM - RAM_in_use;
quote:
What is RAm made up of?
flip-flops

EDIT: RAM_in_use = Win32 ? (Physical_RAM + Virtual_RAM - 1 + ) : ;

[edited by - CWizard on July 25, 2002 7:49:34 PM]

Share this post


Link to post
Share on other sites
Just two clarifications:

1) The size of the stack defaults to 1MB for MSVC++, but it CAN be changed.

2) I''m not sure what advantage there is to using VirtualAlloc() over new/malloc. One reason, I might NOT choose to use VirtualAlloc() is that it is OS dependent. I thought new/malloc was a wrapper for VirtualAlloc()/HeapAlloc()/GlobalAlloc()? And I think each of those 3 is a wrapper for one of the others, I''m not sure on the order.

----------------

If you need to allocate large blocks of memory use new/malloc. Don''t use globals, unless your design orignally called for the memory block to be global (because globals are considered a bad programming practice).

The only advantage to using the stack is its a lot faster than new/malloc. Try to preallocate your memory in a section of the code where speed is not critical (eg. not in tight inner loops).

Share this post


Link to post
Share on other sites
quote:
Original post by Z01
2) I''m not sure what advantage there is to using VirtualAlloc() over new/malloc.


there are a couple. first, you can tell it to reserve memory without actually committing it, which allows for c-style growable arrays and is generally useful when you think you "might* need a lot of memory, but you are not sure that you will need it all at once or at all. it''s especially effective when combined with structured exception handling for the access violations. you might want to read up on it here.

second, VirtualAlloc has much less overhead than new. it has some constraints, for instance the memory blocks are aligned consistently with resource granularity (64 kb for win32), but for large memory blocks it might be faster because it (i suppose) doesn''t have to do that many housekeeping tasks as heap code needs to do. of course, only benchmarks will show any performance advantage, i''m just making a reasonable guess.
quote:

One reason, I might NOT choose to use VirtualAlloc() is that it is OS dependent.


face it, os-independent crt code isn''t the fastest around. talking directly to the os gives you the highest performance. also consider that the c run-time doesn''t have a VirtualAlloc equivalent at all.
quote:

I thought new/malloc was a wrapper for VirtualAlloc()/HeapAlloc()/GlobalAlloc()?


new/malloc call HeapAlloc, but they have their own code on top of it, so be careful with the term "wrapper". it''s quite similar to saying that stdio file streams (and iostreams, if you take it one level up) are wrappers around CreateFile/ReadFile/WriteFile functions. yes, they call win32 in the end, but they have quite a bit of code of their own being executed along.
quote:

And I think each of those 3 is a wrapper for one of the others, I''m not sure on the order.


see my point above regarding wrappers. those three functions are designed for different things (as in different requirements/different access patterns) and it wouldn''t be right to call them the same thing, because they are not. HeapAlloc is roughly equivalent to malloc/new. GlobalAlloc is often used for dde/clipboard data sharing. VirtualAlloc is suitable for large memory requests and for reserving memory without committing it. there''s also LocalAlloc that is used by some win32 functions, its only advantage being it doesn''t require a heap handle to work. generally, you''d use HeapAlloc for small blocks and VirtualAlloc for large ones, and the remaining two for whatever other win32 functions require them.

---
Come to #directxdev IRC channel on AfterNET

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
what''s the problem? You said yourself that you were just testing to see how big of an array you could make. You did that and found out. Ok what''s the problem? I mean, you would never make an array that huge in a real program, right?

Share this post


Link to post
Share on other sites
I''ve got some BIG-ASS arrays in my game code, only I create them on the heap rather than the stack (The PS2 stack of 64Kb soon lets you know when you''ve forgotten to dynamically allocate arrays in functions )

Share this post


Link to post
Share on other sites
quote:
[quote]Original post by CWizard
[quote]Original post by GiantPaul
What is the limit on globals?
= Physical_RAM + Virtual_RAM - RAM_in_use;
quote:
What is the limit on the dynamic memory?
= Physical_RAM + Virtual_RAM - RAM_in_use;
quote:
What is RAm made up of?
flip-flops

EDIT: RAM_in_use = Win32 ? (Physical_RAM + Virtual_RAM - 1 + ) : ;

[edited by - CWizard on July 25, 2002 7:49:34 PM]

------------------------------
The quoting got totally messed up...

The main memory is DRAM. It uses capacitors to store the information. Flip-flops are used in SRAM. The cache is SRAM.

Joakim Asplund
http://megajocke.cjb.net

[edited by - megajocke on July 26, 2002 7:10:38 AM]

[edited by - megajocke on July 26, 2002 7:12:41 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by megajocke
The main memory is DRAM. It uses capacitors to store the information. Flip-flops are used in SRAM. The cache is SRAM.
Yeah, I know. Although, that depends. My old good Amiga has all static ram, always two cycles per mem access ! Didn''t thought it relevant to the original (main) question.

Share this post


Link to post
Share on other sites