The solidity docs state the following:
"Solidity manages memory in a very simple way: There is a “free memory pointer” at position 0x40 in memory. If you want to allocate memory, just use the memory from that point on and update the pointer accordingly."
Reference: https://solidity.readthedocs.io/en/v0.4.21/assembly.html
edit
Example from the docs:
mstore(0x40, 0x60) // store the "free memory pointer"
// ...
// memory allocator
function $allocate(size) -> pos {
pos := mload(0x40)
mstore(0x40, add(pos, size))
}
First, it stores the address 0x60 as the current memory pointer by using mstore(adr, value) to store 32 bytes.
Then it creates a function, allocate(size).
The function allocate(size) is functioning in the following way:
Given the argument size the function caller can specify how much memory the program requires. The function then loads the current "free memory pointer" at address 0x40 (which is 0x60 before calling this function the first time in this case) and adds size to it. After this the function returns the new "free memory pointer" and the programmer can simply use the size bytes the program has allocated.
edit 2: concrete example
I am very inexperienced in coding assembly code for the EVM, so please take in account that the following example could be wrong. I'll try to give an example that shows how to allocate space (32 bytes) for a variable, store a value, read the value and finally free the memory.
// prepare our variables
// x = variable used to store the contents of the allocated memory
// p = current "free memory pointer"
let x := 0
let p := mload(0x40)
// allocate 32 bytes of space
mstore(0x40, add(p, 0x20))
// store up to 32 bytes in the allocated space
mstore(p, 0xDEADBEEF)
// load the value we just stored
x := mload(p)
// We don't need the allocated space anymore. Free it.
mstore(0x40, p)