1

I just want to read the system memory, not the memory allocated for other processes, just free memory. I tried allocating huge memory using malloc:

char *ptr;
ptr = (char*)malloc((1024*1024*700)*sizeof(char));

I tried to print it, but it is printing the null character (i.e. nothing).

printf("%c",ptr[i]);

So I casted its type to int, thinking that it may print the ascii of it.

printf("",(int)ptr[i]);

All the values printed are 0.

malloc doesn't initialize the memory allocated, does it? I couldn't print the memory.

I tried it in linux, fedora 14, with latest gcc and windows xp with dev-c++

I just want to access the memory. Is it possible through some kernel pgm in linux by using the address and get the date out of it? Can you suggest some tools for reading the ram?

2
  • By system memory you mean RAM?
    – Naveen
    Nov 11, 2010 at 5:48
  • Sketchiness aside, are you working in userland or kernel space? Nov 11, 2010 at 5:59

2 Answers 2

7

It's true that malloc() itself doesn't initialise the memory, but the operating system does typically sanitise the memory it hands your process when it requests more. This is a security measure.

If you wish to directly read physical memory, you can do so by opening /dev/mem and mapping the portion of interest using mmap(). The permissions on /dev/mem usually restrict this access to root.

1
2

Regarding:

the system memory , not the memory allocated for other processes, just free memory

On a modern operating system, this has no real meaning, as all memory is virtual. As caf has said you could try scanning /proc/mem (as root), but that would give you all physical memory (including physical memory that currently happens to contain other processes' data) and would miss data which has been swapped to disk or kept by some other backing than physical memory. Perhaps if you clarify what you want to achive, you'll get better answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.