1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Rep Power

    Getting memory address of a File in HardDisk

    I made a text file.
    I can do all File I/O functions in c. no problem!
    except that "I want to get the memory address of the begining of that File", so that I can access each character of the file by incrementing memory address.
    How to?

    Just a Learning Experiment
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Rep Power
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. Contributing User

    Join Date
    Aug 2003
    Rep Power
    Files are not "in memory" and have no direct address. However in modern operating systems and processors with virtual memory capability it is possible to memory-map a file such that it appears to be in addressable memory.

    Once memory mapped, you can read and write a file with random access as if it were memory, under the hood the OS handles the transfer of data and mapping between the filesystem and RAM. Your systems swap-file works like this, but you can create your own memory mapped file as well. Like the system swap file, you can have a larger virtual space that you have physical RAM available, and like the system swap-file doing so will be considerably slower than physical RAM.

    Memory mapped files can also be used as a means of sharing data between multiple separate processes.

    The API for memory-mapped files varies between operating systems.

    The basic Win32 API and a detailed explanation can be found here though the article is quite old, it still stands. More or less the same informaton, but with perhaps a poorer description of the details can be found here. The .NET framework provides a more modern API described here

    In POSIX operating systems such as Linux or OSX you can use mmap()

    For an OS independent memory mapped file API you can use boost.

IMN logo majestic logo threadwatch logo seochat tools logo