I've been getting into data structures in C lately, and I got the idea of making a char buffer which grows dinamically has you type by adding each character into a Linked List, and whose contents can be copied into a char array. Here is the code I ended up with:
#include<stdio.h>
#include<stdlib.h>
//Here is the base structure of the LinkedList.
typedef struct linked_list_char {
size_t size; //Number of characters (only known by the head)
char item; //Character.
struct linked_list_char* next; //Next node.
} DavidLinkedListChar;
typedef DavidLinkedListChar* LinkedListChar; //This is how the head will be declared.
//Adds each node.
void addChar(LinkedListChar* head, char newItem) {
LinkedListChar tracer = *head; //This will go through the list.
//If the head is NULL, the head will be accessed directly
if(*head == NULL) {
*head = malloc(sizeof(DavidLinkedListChar));
(*head)->item = newItem;
(*head)->next = NULL;
(*head)->size = 1; //The size will be one.
//If it isn't, the tracer will find the end of the list and insert a node.
} else {
while(tracer->next != NULL) tracer = tracer->next;
tracer->next = malloc(sizeof(DavidLinkedListChar));
tracer->next->item = newItem;
tracer->next->next = NULL;
(*head)->size++; //The size will increase by one.
}
//I nullify all the pointers used, although I don't know if it is necessary.
tracer = NULL;
head = NULL;
}
//Takes a char and allocates each character in the list, plus '\0'.
void convertIntoStaticString(char* destination, const LinkedListChar* source) {
LinkedListChar tracer = *source; //This will go through the list.
int i = 0; //This will find the index in which '\0' needs to be.
//This loop doesn't count the last member of the list, which is '\n'.
while(tracer->next != NULL){
destination[i] = tracer->item;
tracer = tracer->next;
i++;
}
destination[i] = '\0'; //Now the char array is null terminated.
//Again: I don't know if this is necessary.
tracer = NULL;
destination = NULL;
}
//This function frees the entire list.
void deleteListChar(LinkedListChar* lChar) {
LinkedListChar tracer = *lChar; //This will go through the entire list
LinkedListChar prev = NULL; //This will contain the memory to be freed.
//tracer will go down the list while every previous node pointed will be destroyed.
while(tracer->next != NULL) {
prev = tracer;
tracer = tracer->next;
free(prev);
prev = NULL;
}
free(tracer); //Frees the last node.
free(*lChar); //Frees the head (although it was probably freed already, but just in case it wasn't)
//Nullifying everything.
(*lChar) = NULL;
lChar = NULL;
tracer = NULL;
}
int main() {
LinkedListChar cbuffer = NULL;
char key = 'n';
//While the key isn't '\n', it will keep adding characters to the list.
while(key != '\n') {
key = fgetc(stdin);
addChar(&cbuffer, key);
}
//Flexible char array of the same size as the buffer(counting the later ignored '\n' node).
char name[cbuffer->size];
//Copies the chars from the Linked List to the char array.
convertIntoStaticString(name, &cbuffer);
//Deletes the buffer.
deleteListChar(&cbuffer);
//Prints the char array.
printf("%s", name);
//Pauses until enter is pressed.
getc(stdin);
return 0;
}
This seems to work just fine; however I would like to know what more experienced programmers think of this. Plus, there's a considerable increase in memory for something like my fullname (which is 33 characters long, including blankspaces) of 20 KB, according to the Windows program manager. Do you guys think this was worth trying? Does my code contain any potential memory leak that might hinder performance? If one of you can try this code on the Linux Shell and tell me how well or badly it performed, it would be of great help!