Archived

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

memory and such

This topic is 6387 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''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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
null_pointer: auto means automatically decide to put it in a register or on the stack. The register keyword means put it in a register, but it doesn''t work with the new compilers. And the stack size can be changed in windows.

Also with memory management it gets more complicated if you go down to a lower level, since the compiler does some optimizations on what you tell it to do.

For a good time hit Alt-F4! Go ahead try it, all the cool people are doing it.

Share this post


Link to post
Share on other sites
quote:
Original post by blue-lightning

null_pointer: auto means automatically decide to put it in a register or on the stack. The register keyword means put it in a register, but it doesn''t work with the new compilers. And the stack size can be changed in windows.

Also with memory management it gets more complicated if you go down to a lower level, since the compiler does some optimizations on what you tell it to do.

For a good time hit Alt-F4! Go ahead try it, all the cool people are doing it.


My C book states that the register keyword simply _tries_ to put the variable in a CPU register. Right or wrong?

/. Muzzafarath
Mad House Software

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by jumble

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?




Is there any equivalent of the static directive in Pascal (Delphi)?
I don’t think there is one, but I’m just checking to make sure, because a static directive could be very useful…

Share this post


Link to post
Share on other sites