Sign in to follow this  

memory and text editors

This topic is 4811 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to make a text editor in C but I'm having a problem deceding how I should handle memory. I'm thinking that when the user enters some text it should get stored in a buffer. My questions are: How should I allocate memory for this? When should I flush the buffers? If someone deletes a letter or two do I "free" the allocated space?

Share this post


Link to post
Share on other sites
Well I don't think you would clear the buffer if they deleted character. Perhaps they should be deleted from a primary buffer, but added to a secondary buffer. This will allow for an undo method. Otherwise I think it is fair to delete from the buffer.

Share this post


Link to post
Share on other sites
Let me try to rephrase my question. If I make a pointer X size using malloc how can I later make the pointer a bigger size but not touching the memory I already allocated for it. I kinda just want to add more memory to a pointer.

Share this post


Link to post
Share on other sites
Look up "realloc()" if you currently use "malloc()".

However, beware that if the user opens up a file of size a few megabytes, and then starts typing at the beginning of the file, you would have to memmove() all those megabytes per character the user types. This gets slow pretty quickly :-)

What some editors do is break the text into manageable chunks, say 4 kB each. When a chunk wants to grow bigger than 4k, it gets split in two chunks of 2k text each, with 2k of padding, which will allow you to memmove() only a small amount of the text to make space for your newly inserted character. This also means you'll never have to worry about fragmentation. There's a list or vector of these chunks that you can walk to find the Nth character, etc.

Another method is to allocate a kilobyte or four, more than the size of the text. Call this extra space "the gap." When you move the cursor somewhere, you memmove() data such that the "gap" moves to where you're typing. When you insert, you replace the first byte of the gap with the new character, and shrink the gap by one character, increasing the starting offset of the gap by one character. When the gap is empty, you reallocate your buffer so it's bigger, and create a new gap at the insertion point.

Share this post


Link to post
Share on other sites
Another technique I've heard of for text editors is allocating seperate memory for each line. The C++ equivilant (yes I know you said you're working with C) would be:

class text_file_representation
{
std::vector< std::string > data;
// if this was a GUI widget:
// size_t line;
// size_t insert;
public:
//...
};


Pros: makes seeking up/down lines a snap.
Cons: probably increased memory useage, extreemly long lines can make computer go boom, and joining/seperating lines is slower than it needs to be.

You should also investiage rope (a type of string implementation - here is some information about SGI's C++ class "rope".) Features include forms of memory compression, much faster substring/concatenation, and other stuff that's mentioned on that page.

Share this post


Link to post
Share on other sites

This topic is 4811 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this