Archived

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

How exactly does RAM work?

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

ok... i know wut ram is. just not too sure about the technical stuff. when i run a single file program that has only 1 exe, does that whole program get loaded into the ram? the main question is... if i make a tetris game that has a 2 player mode, i''m thinking of making 2 display/control classes, one for each player. (each will have the blocks, the movements, the scores etc. ) is there some way to only declare the second class if you are in a 2 player mode or do you have to declare both and just not use one? (one will be active and the other sits in the RAM?)

Share this post


Link to post
Share on other sites
[EDIT: Statements retracted]

In any case, here's the deal. RAM usage is not much of an issue in programming. It does get to be in big projects, but usually more with the graphics. But to answer your question:

The best way to do this is through pointers. Pointers allow us to be able to create the class, but the class is not automatically allocated memory. We need to create it ourselves.
    
CPlayer* Player1 = NULL;
CPlayer* Player2 = NULL;

void InitPlayers( BOOL TwoPlayer )
{
Player1 = new CPlayer();
if( TwoPlayer )
Player2 = new CPlayer();
}


-----------------------------
The sad thing about artificial intelligence is that it lacks artifice and therefore intelligence.

Edited by - Promit on February 18, 2002 5:43:46 PM

Share this post


Link to post
Share on other sites
You will always have to create both classes - classes exist in code and are like blueprints for building objects.
//class
class chicken {
...
}

However you can use the classes to create as many or as few objects as you wish.

//object
chicken myChicken;
chicken yourChicken;

if you want to dynamically allocate objects youl need an object pointer

chicken *chickenPtr;
...
chickenPtr = new chicken();

what is in RAM and what isn''t is controlled by the operating system, things will be swapped to the hard drive according to the algorythm the operating system is using - the code and data that are currently being used will be in RAM (usally in blocks) but like Promit says who cares unless your are making one hell of a game and given the question I''d doubt it (no offence intended).


Sephwrath.

Share this post


Link to post
Share on other sites
quote:
Original post by Promit
2) You are one of those people that acts like thye know a whole lot and actually know nothing.



Where do you get off suggesting something like this? It''s completely unneeded and pointless. Not to mention you''re in no position to patronize.

Share this post


Link to post
Share on other sites
quote:
Original post by Promit
I didnt imply either one as being more correct, and if hes just veyr confused, im cool with that. Notice i said "or".

I like to poke at anyone who is #2, even if the poster is not #2...no offence to anotherfantasy.



There was still no reason for you to even have said it in the first place...

It''s like me finding some random post by a kid who''s seeking help and saying

"Well you''re either looking for an answer, or you''re actually plotting to overthrow the government. Now, getting to your question..".

It''s completely random, irrelevant, and pointless.

Share this post


Link to post
Share on other sites
look down kids, as soon as a program loads its in ram or if the ram is too full then it goes on the stack (or virtual memory)

there is no difference between

ClassX a;

AND

ClassX* a;
a = new ClassX;

they are both implying the same memory allocation, both are dynamic at execution but one will load in a particular region of memory wich may be the local execution of a function or global to a program, i suggest to people interrested into learning about this look into these sample keywords :

call stack
stack pointer
memory allocation
virtual memory

or go to school in computer science, these are covered in the begining of object oriented programming classes (well in quebec at least ).

Share this post


Link to post
Share on other sites
quote:
Original post by hotdot
as soon as a program loads its in ram


Not necessarily.
quote:

or if the ram is too full then it goes on the stack


No it doesn''t.
quote:

there is no difference between

ClassX a;

AND

ClassX* a;
a = new ClassX;


... except that the first one is created on the stack, and the second one is created on the free store, and will persist beyond its local scope. Seems like a pretty big difference to me.

Share this post


Link to post
Share on other sites
"A memory unit is a group of storage cells along with asociated circuits needed to transfer information inside and outside the dispositive. It is posible to have access to the memory cells to transfer information to or from any random location desired; hence the name Random Access Memory.

A memory unit stores information in bit groups called words.A word in memory is an entity of bits that enters and go out of the storage space as a unit. A memory wordis a group of ones (1) and zeros (0) and can represent a number, an instruction, one or more alphanumeric characters or any other information in binary code.A group of 8 bits is known as a byte. Most computer memories use words with a length that is a multiple of 8. So, a 16 bit word contains 2 bytes, and one of 32 will have 4 bytes, the memory capasity is usually in terms of how many bytes it can store."

not 100% acurate since I had to transale from my Spanish copy but its from Computer Engineering: Hardware design,1991, M. Morris Mano.


Share this post


Link to post
Share on other sites
quote:
Original post by Martee
[quote]Original post by hotdot
as soon as a program loads its in ram


Not necessarily.

==>yes it does actually on the windows operating system that is, where do you think the call stack is ? certainly not in the cpu cache...well yes in a sense where it is actually executed. I''d like you to extend your opinion on this as to state afirmations like this...

quote:

or if the ram is too full then it goes on the stack


No it doesn''t.

==>mmm, you sound awfully sure of yourself, thats what they told me at school, i would like you to refine your point of view rather than stating bold statements like this.


quote:

there is no difference between

ClassX a;

AND

ClassX* a;
a = new ClassX;


... except that the first one is created on the stack, and the second one is created on the free store, and will persist beyond its local scope. Seems like a pretty big difference to me.

...they are both implying the same memory allocation, both are dynamic at execution but one will load in a particular region of memory wich may be the local execution of a function or global to a program...

read until the end before quoting like this, again help people learn instead of throwing away statement destruction without any facts or explanations stated. I am highly anticipating your explanation ( my teacher has been teaching comp sci for 20 years) and i am not a newbie, please show me right if im wrong here i am very open minded.

Share this post


Link to post
Share on other sites
How exactly does RAM work?

It is MAGIC ! You have a crew of demons driving carts full of handkerchiefs (knotted for one, unknotted for zero) !

My apologies to J.P. Petit.

Edited by - Fruny on February 18, 2002 4:20:00 PM

Share this post


Link to post
Share on other sites
quote:
Original post by hotdot
or if the ram is too full then it goes on the stack

reply by Martee:
=>No it doesn''t.

reply by hotdot:
==>mmm, you sound awfully sure of yourself, thats what they told me at school, i would like you to refine your point of view rather than stating bold statements like this.


Well, a stack is a data-structure not a type of memory, so saying that a program is moved to the stack when RAM is low is a really weird statement. Perhaps by ''stack'' you mean ''hard drive''?

Share this post


Link to post
Share on other sites
There are ASM commands PUSH and POP that add and remove data from the STACK. The stack behaves like a stack data structure; however the stack is system-dependent. A heap is also a data structure; however things allocated with new are added to the heap, aka the "free store."

Share this post


Link to post
Share on other sites
Try the following two programs:

  
int main()
{
char bigarray[1024 * 1024 * 20]; //20 megabytes

return 0;
}

  
int main()
{
char * bigarray = new char[1024 * 1024 * 20]; //20 megabytes

delete [] bigarray;
return 0;
}


One will run without any errors. The other won''t.

Share this post


Link to post
Share on other sites
yes indeed i meant the "heap" sorry for not finding the right term in french its "le tas global" or the global heap or the virtual memory maybe, but still from what i understood if the ram is full then the heap is used.

Share this post


Link to post
Share on other sites
if my judgement is right the reason it wont work is simply because when the function is called a new stack or function context is created in the "stack" (i hope this is the right english term) is it right to say that a 20 MB function context is likely too large for the call stack (in french its "la pile d''appel") and that a 20 MB heap storage is ok. (though 20 MB is alot of data...). by call stack i refer to where the local variables are stacked and where the return pointer is located.

Share this post


Link to post
Share on other sites
quote:
Original post by hotdot
where do you think the call stack is ? certainly not in the cpu cache...well yes in a sense where it is actually executed. I''d like you to extend your opinion on this as to state afirmations like this...


The entire executable does not have to be loaded into memory in order to execute the program. Certainly, some of it is in RAM (hence the call stack), but the entire program is not necessarily in RAM.
quote:

==>mmm, you sound awfully sure of yourself, thats what they told me at school, i would like you to refine your point of view rather than stating bold statements like this.


I think this one has already been covered.
quote:

read until the end before quoting like this, again help people learn instead of throwing away statement destruction without any facts or explanations stated. I am highly anticipating your explanation ( my teacher has been teaching comp sci for 20 years) and i am not a newbie, please show me right if im wrong here i am very open minded.

I did read until the end. The reason I quoted that portion is that I''m not sure what you are trying to say here:
...they are both implying the same memory allocation, both are dynamic at execution but one will load in a particular region of memory wich may be the local execution of a function or global to a program.... Regardless, I think that saying that there is no difference between heap allocation and stack allocation is trivializing the issue.
quote:

from what i understood if the ram is full then the heap is used.


The heap is in RAM. If the RAM is full, then the heap is full too.

Share this post


Link to post
Share on other sites
Everything you ever wanted to know about RAM:

http://arstechnica.com/paedia/storage.html


If builders built buildings the way programmers wrote programs, then the first woodpecker that came along would destroy civilization.

Share this post


Link to post
Share on other sites
quote:
Original post by Martee
The heap is in RAM. If the RAM is full, then the heap is full too.

The heap would be in virtual memory - not necessarily RAM.



The world holds two classes of men -- intelligent men without religion, and religious men without intelligence. Abu''l-Ala-Al-Ma''arri (973-1057; Syrian poet)

Share this post


Link to post
Share on other sites
Touché. For some reason, that slipped my mind.
However, it is still inaccurate to say that the heap is used when RAM is full, because that implies that the heap isn''t used when RAM isn''t full, which is completely false.

Share this post


Link to post
Share on other sites
Martee, that's not correct. Saying that the heap is used when the RAM is full only means the heap is used when the RAM is full. It does not mean the heap isn't used when the RAM isn't full.

Going to rephrase the sentence.

"heap is used" = "I take a taxi"
"RAM is full" = "my car is broken"
Rephrased: "I take a taxi when my car is broken."
You said: "I don't take a taxi when my car isn't broken."

If the car is stolen, you take a taxi. So, you can still take a taxi even though the car isn't broken.

The heap can still be used even if the isn't RAM is full. The sentence only says the heap IS used when the RAM IS full.

If the sentance would be "The heap is used if, and only if, the RAM is full", then you are correct.

[edit: typos]

Edited by - Brother Bob on February 19, 2002 5:05:44 AM

Share this post


Link to post
Share on other sites
quote:
Original post by Martee
because that implies that the heap isn't used when RAM isn't full, which is completely false.

quote:
Original post by Brother Bob
It does not mean the heap isn't used when the RAM isn't full.

Brother Bob, did you even READ Martee's post?

Edited by - MarkyD on February 19, 2002 7:59:18 AM

Share this post


Link to post
Share on other sites