CMemFile Class

The CFile-derived class that supports memory files.

class CMemFile : public CFile

Members

Public Constructors

Name

Description

CMemFile::CMemFile

Constructs a memory file object.

Public Methods

Name

Description

CMemFile::Attach

Attaches a block of memory to CMemFile.

CMemFile::Detach

Detaches the block of memory from CMemFile and returns a pointer to the block of memory detached.

Protected Methods

Name

Description

CMemFile::Alloc

Override to modify memory allocation behavior.

CMemFile::Free

Override to modify memory deallocation behavior.

CMemFile::GrowFile

Override to modify behavior when growing a file.

CMemFile::Memcpy

Override to modify memory copy behavior when reading and writing files.

CMemFile::Realloc

Override to modify memory reallocation behavior.

Remarks

These memory files behave like disk files except that the file is stored in RAM rather than on disk. A memory file is useful for fast temporary storage or for transferring raw bytes or serialized objects between independent processes.

CMemFile objects can automatically allocate their own memory or you can attach your own memory block to the CMemFile object by calling Attach. In either case, memory for growing the memory file automatically is allocated in nGrowBytes-sized increments if nGrowBytes is not zero.

The memory block will automatically be deleted upon destruction of the CMemFile object if the memory was originally allocated by the CMemFile object; otherwise, you are responsible for deallocating the memory you attached to the object.

You can access the memory block through the pointer supplied when you detach it from the CMemFile object by calling Detach.

The most common use of CMemFile is to create a CMemFile object and use it by calling CFile member functions. Note that creating a CMemFile automatically opens it: you do not call CFile::Open, which is only used for disk files. Because CMemFile doesn't use a disk file, the data member CFile::m_hFile is not used and has no meaning.

The CFile member functions Duplicate, LockRange, and UnlockRange are not implemented for CMemFile. If you call these functions on a CMemFile object, you will get a CNotSupportedException.

CMemFile uses the run-time library functions malloc, realloc, and free to allocate, reallocate, and deallocate memory; and the intrinsic memcpy to block copy memory when reading and writing. If you'd like to change this behavior or the behavior when CMemFile grows a file, derive your own class from CMemFile and override the appropriate functions.

For more information on CMemFile, see the articles Files in MFC and Memory Management (MFC) and see File Handling in the Run-Time Library Reference.

Inheritance Hierarchy

CObject

CFile

CMemFile

Requirements

Header: afx.h

See Also

Reference

CFile Class

Hierarchy Chart