Thread: Variable length strings

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    198

    Question Variable length strings

    I wanted to make a text editor program in C using ncurses, but I need a way to store the text. I cannot use a fixed array because there is no way to if the file has a few characters or hundreds of lines.

    I tried using a char pointer and realloc() every time a character is added or removed, it seems to kind of work but it is a bit quirky and will randomly cause errors. I wonder if you have any ideas or know some sites where I can learn about this?

    This is my progress:

    Code:
    char* str_new(char* data)
    {
    	char* ptr;
    	ptr = (char*) malloc((sizeof(char) * (strlen(data) + 1)));
    	strcpy(ptr, data);
    	return ptr;
    }
    
    void str_cat(char* str, char* sub)
    {
    	str = (char*) realloc(str, sizeof(str) + (sizeof(char) * (strlen(sub) + 1)));
    	strcat(str, sub);
    }
    
    void str_ins_char(char* str, char sub, int index)
    {
    	str = (char*) realloc(str, (strlen(str)+2)*sizeof(char));
    	int i = strlen(str);
    	while(i >= index) {
    		str[i+1] = str[i];
    		i--;
    	}
    	str[index] = sub;
    }

  2. #2
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    I did not read through the code, but idea of reallocing for each character is terrible. Realloc is quite a heavy call, it is better to allocate some chunck of memory, and realloc only when bigger chunck is needed. (Eg alloc 1kb - if it's too little, realloc for another 1kb).

    Also, remember that realloc may move the contents of memory to some other place, so it is easy to mess things when you assume the enlarged block is starting from same location where smaller block was. Ensure you use enough ** in your function calls, to get the changed startingpoint for allocated block out of the function. (Your str_cat() seems to fail in that).

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    Figuring out how to make it reallocate every 1024 characters or so does seem like a good idea.

    Somehow I can't find anything about the "**" operator, though.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I think he means that the dereference operator * can be applied many times, depending on the type.

    Anyway that's really neither here nor there, it just depends on how you want to do it. And this sort of thing is a FAQ.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by MTK View Post
    Figuring out how to make it reallocate every 1024 characters or so does seem like a good idea.

    Somehow I can't find anything about the "**" operator, though.
    That's because there isn't one. But not only are there char * variables, there are char ** variables, and char *** variables, and .... Since if you want to change a pointer, one star is not enough, you will need to use a char ** variable instead.

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    You mean that I am only changing a local copy of the pointer and if realloc() chooses the change the location, the pointer isn't changed outside the function?

  7. #7
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    Basically yes.

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    So will this change the address of the pointer in main():

    Code:
    void changeptr(int** ptr) //So int** is a pointer to a pointer to an int?
    {
      *ptr = NEW_MEM_ADDRS;
    }
    
    int main()
    {
      int* ptr;
      changeptr(ptr);
      return 0;
    }

  9. #9
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    yes.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    changeptr(&ptr);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 04:13 PM
  2. Help with returning length of char variable.
    By .: Kimochi :. in forum C++ Programming
    Replies: 2
    Last Post: 05-01-2006, 10:56 AM
  3. Variable length
    By 182 in forum C++ Programming
    Replies: 9
    Last Post: 02-14-2006, 08:56 PM
  4. Use of variable
    By alice in forum C Programming
    Replies: 8
    Last Post: 06-05-2004, 07:32 AM
  5. variable length records for strings
    By teja22 in forum C Programming
    Replies: 1
    Last Post: 02-08-2002, 07:49 PM