In what situations one would use malloc instead of mmap and vice versa.
Both return a virtual addr ptr. So whats the difference?
A lot. mmap reads a file from disk, loads it into memory, and presents a memory mapped file. The memory section can be readonly or writable. Writing will update the underlying file on disk, with some limitations.
malloc allocates heap memory and returns a pointer to the freshly available memory.
It does not point to any valid data. You have to zero it out, then put your own data in there, you do not malloc and then use that result for mmap.
My current implementation of mmap is:
fd = open("/dev/mem",O_RDWR);
BUFFER =(unsigned char *) mmap(NULL, RANGE, PROT_READ | PROT_WRITE, MAP_SHARED,fd, ADDR);
here ADDR is the physical address in the RAM.
I believe here am mapping virtual memory from fd or /dev/mem and not a disk file.
You can open /dev/mem directly; it is already memory, you do not need mmap.
/dev/kmem is kernel, /dev/port ports. /dev/[anything] is a virtual file, kernel resident & linked ( as in ln ) via a psuedo-device that is part of active memory, like /proc.
DDD and similar tools are probably easier to use than just writing code in C. In order not to crash your box you should open any of those read-only until you learn more about it.
Thnks!