im trying to write a simple program which will ckeck through all data addresses in ram memory and tell us which addresses are free, which are not. if i have 1gb ram and it is used by operating system+other software, i will be able to get a sort of a map of free shelves inside memory.
ok, we know that if the memory data address (0x000001 for example ) is free, it doesnt contain anything. nor NULL nor 0 nor any random number. its just empty.
so how the hell shall i tell to'if statement' to check whether or not is a shelf free.
1 2 3 4 5 6 7 8 9 10 11 12
int *ptr;
for (int ds=0;ds<=4000;ds++){
ptr=(int*)ds;
if(!ptr){cout<<"not free"<<ptr;};
};
return 0;
}
how will i say 'do it if address is empty inside' ?
as i understand, the (!ptr) means 'if this ptr is 0'.but if the empty address doesnt contain anything then how will whis work? it is an interesting fact that this program kind of works, because address 0x000000 is automatically equal of 0. so "not free" does shows up once.however if i start searching from 0x000001, 'if staitment' never works ofcourse when all of addresses really are empty.
and one more question, how do i call a shelf in a RAM? a ram address? is there any other name? people use name stack, but i dont believe that stack i correct word, because stack comes in action only ufter a program function compiles and resrves a stack for himself. i guess there is a name like RAM slot or shelf or room or something. thanks for help.
Your assumption is incorrect. Free or reserved or used RAM will always contain something. You need the OS' help for this. In Windows, you can use the IsBadXXXXPtr() functions (found listed here http://msdn.microsoft.com/en-us/library/aa366781(VS.85).aspx ) to determine whether a particular address is accessible in a certain way.
Sorry, but your question does not make total sense to me. Apart from anything else, this is a far from a simple problem as possible.
#1 An unused memory address can have any random value in it, as can be seen if you new a buffer and look to see what it contains.
(With VC++, the debug version of the CRT fills the memory with a pattern so you can spot newly allocated memory, recently freed memory, etc when debugging. So you need to try this with the release build. Not sure if other CRTs, like GCC, do the same)
#2 Some memory is used for the program stack. Other memory is owned by heaps, which allocate block when you use malloc, new, etc. Even other memory might be a mapped file.
#3 With Windows, not all memory address are even valid. First of all, for 32-bit Windows, only 2Gb of the virtual memory is accessible to user-mode applications. On top of that, only pages of memory that have been committed are availble for use.
Now it might be possible to analyse the stack and heap, but coming up with a general solution -- to analyze all the memory -- would be a bit of a tall order!
webJose yeah iv heard about IsBadXXXXPtr() people say scary things about this guy heh. i have a question, if all used and unused and free memory already containes some numbers inside it, then why does this program gives me error when it runs?
1 2 3 4 5
int main (){
int *ptr;
ptr = (int*)0x00000E;
cout<<*ptr;
if i have 1gb ram and it is used by operating system+other software, i will be able to get a sort of a map of free shelves inside memory.
Another of your incorrect assumptions is assuming that the OS and other programs exist in the same address space as your program. In fact, even if you could iterate through the entire address space without the program crashing, you'd see that most of it is empty. In modern OSs, different programs exist in different address spaces. For example, if in your program address 0x12345678 was being used by an array that contains a copy of a disk file, in another program that very same address could be being used to store an integer; the OS translates the virtual addresses to physical addresses. Before you ask, no, you can't access physical memory.
Before you ask, no, you can't access physical memory.
Technically you could do that, but that would require running your program in the ring 0 mode. In Linux this would require compiling your code as a kernel module. In Windows this can be done by providing your own device driver (SYS file) with a proper wrapper library (DLL). Definitely not a thing for a beginner.
ok thanks for all the support. i know i may give foolish questions but some people dont have money for education and they try to learn on their own. ill go and read all about virtual memory
thanks.