• Advertisement
Sign in to follow this  

Memory Madness

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

Hi! I am ashamed to admit it, but words like "heap" and "stack" and "virtual memory" mean nothing to me. I see them used all the time, and I am sad not knowing what they mean. I understand how to use pointers and stuff in C++, but not knowing this sort of stuff is unacceptable. Could someone please give me the low-down, or point me to a good explanation of how memory managment really works? Thanks! ^_^

Share this post


Link to post
Share on other sites
Advertisement
heap = any place in memory ... a "hunk" of memory given acces to by the OS ... it can be almost any size (but less than 4gb for 32 bits environments)

stack = when a process is started, it is given a specific memory location of a specific size. When you enter a function, all the variables of it are located in the stack. Usually, we consider the stack "Faster" than heap because it is almost always in the cache of the CPU and doesn't need to refresh your ram memory.

virtual memory = if you make a "new" of a gig, but you only have 256 mb of ram, the call will succeed, and the OS will give you the memory you wanted. This memory is placed on your hard drive ... and is WAY slower.

Share this post


Link to post
Share on other sites
IMHO, a programmer should always learn how the processor (and probably RAM) works. It's nice and dandy if you how to program, but why bother if you don't even know what's going on behind the scenes. Knowing how the processor manipulates data, and how important the cache really is will give you a better view of what's going on beyond the case.

Share this post


Link to post
Share on other sites
Virtual memory

Quote:

Virtual memory or virtual memory addressing is a memory management technique, used by multitasking computer operating systems wherein non-contiguous memory is presented to a software (aka process) as contiguous memory. This contiguous memory is referred to as the virtual address space.
...


Call Stack

Quote:

In computer science, a call stack is a special Stack (data structure) which stores information about the active subroutines of a computer program. (The active subroutines are those which have been called but have not yet completed execution by returning.) This kind of stack is also known as an execution stack, control stack, or function stack, and is often abbreviated to just "the stack".
...


On Windows, every thread is assigned it's own call stack. The default size is 1 Mb. In a C or C++ program, the call stack is used to pass arguments to functions as well as to provide memory for local variables. It's also used to hold the address that a function is supposed to return to after it's called - which is where it gets the 'call' part of it's name from.

Heap memory, aka dynamic memory allocation, is memory allocated for use during programm runtime. It doesn't necessarily have anything to do with the Heap (data structure). Memory allocated using 'malloc' or 'new' comes from heap memory, in that case, the crt heap. Windows provides other heap allocation functions, HeapAlloc and LocalAlloc and GlobalAlloc. LocalAlloc and GlobalAlloc are holdovers from Win 3.11 and should only be used in conjunction with other functions that specify their use (eg InitializeAcl, InitializeSecurityDescriptor).

Share this post


Link to post
Share on other sites
@crucifer: Thanks alot for the brief overview!

@kevtimc: I agree 100%, and that is why I have made this thread.

@lessbread: The links and quick explanations were first rate, thanks alot! Perhaps I simply missed this in my reading, but where do global variables get allocated?

Thanks again guys! :)

Share this post


Link to post
Share on other sites
Basically ram has a physical address and there is paging mechanism that allows blocks of memory to map to a virtual address. So a page at say 0x0003000 in physical might be 0x10000000 in virtual.

What the processor can do is cause a page fault where an address isn't mapped. It's like a hardware exception. What it can do during this fault is load from the harddrive and put those contents at the address that faulted and give you a swapping method, generally if you are using all of your ram, some other page is unmapped somewhere else. Some OS's swap from a partition, others use a file, etc etc.

Share this post


Link to post
Share on other sites
Quote:
Original post by GodBeastX
Basically ram has a physical address and there is paging mechanism that allows blocks of memory to map to a virtual address. So a page at say 0x0003000 in physical might be 0x10000000 in virtual.

What the processor can do is cause a page fault where an address isn't mapped. It's like a hardware exception. What it can do during this fault is load from the harddrive and put those contents at the address that faulted and give you a swapping method, generally if you are using all of your ram, some other page is unmapped somewhere else. Some OS's swap from a partition, others use a file, etc etc.


It's also good to know how transistors manipulate the data in RAM (along with many other places).

Share this post


Link to post
Share on other sites
Quote:
Original post by guyver23
... where do global variables get allocated?


It depends. On Windows, if the global variable is assigned a value on declaration, for example,

const int g_somevar = 12345;

it becomes part of the .data section of the pe file, that is, the .exe file itself. When a program is launched, the operating system creates a process for it, then sets up the virtual memory space for the program (among other things) and then maps that program (the .exe file itself) into that virtual memory space (aka process address space). The address of this mapping is 0x00400000. Windows programs typically use a HINSTANCE variable to refer to this address. If you check the address of these kinds of global variables, they will usually be close to 0x00400000.

If the global variable is not assigned a value on declaration, for example,

int g_somevar;

it becomes part of the .bss section of the pe file, and is assigned memory during the launch of the program.

Portable Executable File Format

Quote:

...
A few sections are always present in a PE file generated by the Microsoft linker. Other linkers may generate similar sections with different names. A PE file generated with the Microsoft linker has a .text section that contains the code bytes concatenated from all the object files. As for the data, it can be classified into different categories. The .data section contains all the initialized global and static data, while the .bss section contains the uninitialized data. The read-only data, such as string literals and constants, is stored in the .rdata section. This section also contains some other read-only structures, such as the debug directory, the Thread Local Storage (TLS) directory, and so on, which we explain later in this chapter. The .edata section contains information about the functions exported from a DLL, while the .idata section stores information about the functions imported by an executable or a DLL. The .rsrc section contains various resources, such as menus and dialog boxes. The .reloc section stores the information required for relocating the image while loading.

The names of the sections do not have any significance. As mentioned earlier, different linkers may use different names for the sections. Programmers can also create new sections of their own. The #pragma code_seg and #pragma data_seg macros can be used to create new sections while working with Microsoft compiler. The operating system loader locates the required piece of information from the data directories present in the file headers. Shortly, we will present an overview of file headers and then look at them in more detail.
...


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement