My first thought here is to use a dynamic array, but I am looking for something better.

Currently I have the text files open into "chunks". Every word or group of spaces makes up a "chunk". Then I have a line number in this structure and a chunk number for ordering on a line.

typedef struct TextData {
    char* chunk;
    int   index, line;

I am not sure if this is a good idea as this could lead to allocating to much dynamic memory and is prone to memory leaks. At least to my knowledge.

What I am looking for is a better way to implement this.

This is being used for a text editor/console combination program that is going to be made into a dynamic lib for reuse in other programs/games. A small finger print is a big part of this implementation.

Text is placed into a load buffer than a undo buffer tracks only changed chunks the last buffer is a display buffer that is used by an application or interface to display the unsaved edits of the complete file.

Needs to be thread safe as well.

  • 1
    Do you mean "footprint"? Commented Mar 3, 2016 at 10:10
  • 1
    avoiding leaks is just a matter of knowing when you stop using the resource so it can be returned for reuse. Commented Mar 3, 2016 at 10:11
  • If this is for a text editor, don't you need only the lines displayed on the screen? Commented Mar 3, 2016 at 10:11
  • foot print finger print same concept, but refers to memory
    – Joe
    Commented Mar 3, 2016 at 10:11
  • 2
    @Joe: en.wikipedia.org/wiki/Memory_footprint // en.wikipedia.org/wiki/Fingerprint_(computing) - the terms are not interchangeable at all.
    – Mat
    Commented Mar 3, 2016 at 11:06

1 Answer 1


Use the hardware - most OSes have a mechanism to map a large file into a 'window' of memory and the OS (and MMU) manages the mapping for you. As long as ytou have enough virtual address space, you can handle a file of any size as if it were all loaded into memory.

You'll want to look up Memory Mapped Files.

As for an undo feature, you can happily store changes to a list, and apply them to the file on save if you don't want to save them immediately and provide an undo feature to restore the original data.

  • 1
    How would memory mapped file and editing go together?
    – Murphy
    Commented Mar 3, 2016 at 10:37
  • 1
    @Murphy you read the entire file and save the edited bits elsewhere, or you update the file directly and save the undo buffer elsewhere. Its no different to having it loaded into memory - you still need an undo or redo buffer to restore the right bit of edited file to its original state.
    – gbjbaanb
    Commented Mar 3, 2016 at 10:57
  • is there a way to do this in a portable way? Like a lib that takes care of this between windows/linux/mac?
    – Joe
    Commented Mar 3, 2016 at 11:40
  • 1
    @Joe Try boost (always try boost :-) ), but if it has to be plain C, you'll have to search.
    – gbjbaanb
    Commented Mar 3, 2016 at 11:44
  • @Joe try this GPL wrapper for Windows so it looks like Linux's mmap. Mac possibly uses mmap too.
    – gbjbaanb
    Commented Mar 3, 2016 at 14:56

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.