mmap and malloc, whats the difference?


 
Thread Tools Search this Thread
Top Forums Programming mmap and malloc, whats the difference?
# 1  
Old 10-11-2010
Lightbulb mmap and malloc, whats the difference?

In what situations one would use malloc instead of mmap and vice versa.
Both return a virtual addr ptr. So whats the difference?
# 2  
Old 10-11-2010
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.
# 3  
Old 10-11-2010
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.
# 4  
Old 10-11-2010
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.
# 5  
Old 10-11-2010
Thnks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Emergency UNIX and Linux Support

mmap

I want to know whether this is possile or ever been tried out. I want to obtain a chuck of memory using mmap() I do it so : n = mmap(0, 8000, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); And hold on to that memory, when a process requests for memory, some memory is... (2 Replies)
Discussion started by: xerox
2 Replies

2. Programming

mmap

hai, How do we map 'n' number of files into memory by using mmap system call?? Thanks in advance...... (5 Replies)
Discussion started by: andrew.paul
5 Replies

3. Homework & Coursework Questions

mmap

Descriptions: Develop a program that uses mmap() to map a file to memory space. Prepare such a file by yourself and do the follows. <LI class=MsoNormal>Display the content of the file after mapping; <LI class=MsoNormal>Output how many digits included in the file; <LI class=MsoNormal>Replace... (1 Reply)
Discussion started by: gokult
1 Replies

4. Programming

mmap()

how to use mmap() to map a file to memory space. Do you have any simple program???? Because I have to implement lot of concepts into it. (5 Replies)
Discussion started by: gokult
5 Replies

5. UNIX for Dummies Questions & Answers

Whats the difference between $status and $?

Hi, In linux we have exit status variable ($?) and status which tells whether last command was successfull or not. Can someone please tell me what is difference between both. Both tells whether command was successful or not, Any particular difference between them? Thanks in Advance. Thanks... (3 Replies)
Discussion started by: sarbjit
3 Replies

6. UNIX for Dummies Questions & Answers

whats the difference between zombie orpha and defunct processes

can some one please explain zombie orphan defunct and how they r related (3 Replies)
Discussion started by: pbsrinivas
3 Replies

7. Shell Programming and Scripting

whats the difference between $* and $@

Hi, whats the difference between $* and $@ in command line arguments to a shell scripts (3 Replies)
Discussion started by: pbsrinivas
3 Replies

8. UNIX for Dummies Questions & Answers

Linux OR Unix Whats The Difference!

What is the difference bettween linux and unix? Sorry but I am really new to this! :confused: Also are they BOTH free :-D (1 Reply)
Discussion started by: jamesthemagicia
1 Replies

9. UNIX for Dummies Questions & Answers

Whats the difference between...

the various distros of free Linux and other *nix OSes? I'm curious. (1 Reply)
Discussion started by: hype.it
1 Replies

10. Filesystems, Disks and Memory

mmap

Hello. I'm writing some random access i/o software on Solaris 8 using mmap64 to memory map large files (my test file is ~25 GB). The abbreviated code fragment is: fd = open(cbuf,O_RDONLY|O_LARGEFILE); struct stat statbuf; fstat(fd,&statbuf); off_t len =... (0 Replies)
Discussion started by: gusm
0 Replies
Login or Register to Ask a Question