I'm trying to make a small text editor and efficient (space/time wise). I also want to save changes to the text; I would save changes and the main text in two lists, both made of nodes like this:
struct node{
int startingLine;
int endingLine;
char *line;
char *newLine;
};
My idea is to use this struct both for the text list and the changes list;line
and newLine
are to point at char arrays (the lines of text)
when these are text nodes the newLine
array is empty or points at null, and when the line gets changed this node is just moved to the changes list and the newLine
array gets filled with the new line that replaced the first in the text;
this way I would not have to free
the node from the text list and malloc
another node in the changes list and also copy all the information; but:
when I try to set an array to
NULL
i get an error; I wonder why, I thought array names were just pointers?also, to use the heap I only know
malloc(sizeof(struct node))
, does it allocate space for the second pointer too even if i don't immediately need it?
So in conclusion I ask if this is a good idea or how to work around it or if it can be polished somehow; maybe immediately after allocating a node I should set newLine
to NULL
? A NULL
pointer occupies no memory at all or still something compared to not putting any pointer in the struct? Because as is said the idea would be to have the text list made of nodes but with all their "useless" newLine
pointers hanging there.