Advertisement

memory and such

Started by June 17, 2000 03:43 AM
11 comments, last by deakin 24 years, 6 months ago
I''ve been wondering, when a program starts up, does it allocate memory for all of the programs data, then deallocate it all at the end, or does it allocate at the start of the function that the data belongs to, then deallocate it at the end?? I should probably know this, but I tend to avoid memory issues (slack )Thanks, - Daniel
- DanielMy homepage
Right, im no expert but this is how i think it goes:

If you declare the variables globally (not in any functions) then the program allocates the memory on load up and deallocates on exit.

If you declare the variables locally (in a function) then it allocates on calling the function and deallocates once the function finishes. HOWEVER - if you use the "static" keyword in your function prototype the variables are kept in memory and the same values are used if the function is called again, right gurus?

If you declare the variables with the "new" operator then the memory is allocated whenever a new variable is made, and then deallocated whenever it is "delete"ed. This is possibly the best way to keep memory usage at a low.

/home/./~jumble
---------------
jumble-----------
Advertisement
I have to ask, does it really matter as long as it can be used?
Only if you want your games to be restricted to usage on super computers
jumble-----------
Wow! Thanks for the quick reply. I was just wondering because I may need this sort of info in the future for optimizing etc. Like, if you have a 2MB data structure you dont really want it hanging around for the entire program if you only need it for the menu or something...

- Daniel
- DanielMy homepage
>> If you declare the variables with the "new" operator then the memory is allocated whenever a new variable is made, and then deallocated whenever it is "delete"ed. This is possibly the best way to keep memory usage at a low. <<

The same thing goes for "old-skool" malloc:ed and calloc:ed memory btw.

But when you use new or malloc you must declare a pointer to the memory that will be allocated. So for example, if you want a "new"ed integer, then you must do it like this:

int *pInt = new int;

What it does is allocate (sizeof(int*)) bytes of memory for a pointer to an integer (this memory can't be deleted), and then it allocates (sizeof(int)) bytes of memory (that memory can be deleted). So the grand total is (sizeof(int*) + sizeof(int)) bytes of memory allocated. On my computer, that's 8 bytes. If you "delete pInt", then the new:ed memory is deleted, but the pointer-to-int isn't de-allocated until the function is finished (I'm not very good at C++, so please correct me if I'm wrong ).

<update> Thanks Qoy for pointing out that I had written kilobytes instead of bytes... </update>.

/. Muzzafarath
Mad House Software

Edited by - Muzzafarath on June 17, 2000 3:41:26 PM
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
Advertisement
Muzza, I think you meant bytes, not kilobytes
quote: Original post by Qoy

Muzza, I think you meant bytes, not kilobytes


Oh yeah, thanks for pointing that out.

/. Muzzafarath
Mad House Software
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
So the pointer takes up memory, as well as the value that it is pointing to? I always thought that all variables were just turned into pointers during compilation anyway...but maybe I''m wrong.

- Daniel
- DanielMy homepage
Pointers in a 32-bit memory addressing OS take up 32 bits or 4 bytes. Pointers in source code are typically allocated on the stack (like in Muzzafarath''s example), and they are really just another type of variable with some special operators.

Memory that you allocate staticly, via a global or in a function is called stack memory. Memory allocated with new() or delete() is called heap memory. The stack is limited in size (about 1 MB in Windows) but it auto-manages the memory for you so it''s great for small variables. The heap is great for large objects, or memory that you must manage yourself.

Betcha didn''t know this: in C++ there''s a keyword called "auto." auto just means a variable on the stack, but it''s the default so no one ever uses it anymore.

And if pointers are just variables, and all variables are turned into pointers, then you have infinite recursion... The only real "hardware" pointers in a computer are the registers on a processor.




- null_pointer
Sabre Multimedia

This topic is closed to new replies.

Advertisement