C Tutorial: avoid mallocs and string copies
Avoid excessive string copying
Much of the work related to things like reading data from users or reading configuration files involves parsing that data to extract strings of interest. In these cases, it is tempting to allocate new buffers to hold these strings and copy the appropriate characters into them.
This is a perfectly valid approach. However, in certain cases it is not necessary to allocate space and copy strings. If all the data that you need is in the original string and you will not be overwriting that original buffer before you make use of the data, it is quite possible to simply terminate the strings you need (store a 0 at the end of the string) and keep track of pointers to the strings instead of copying the data.
The advantages of doing this are increased performance and reduced bugs: increased performance because of less copying and dealing with managing memory and reduced bugs in forgetting to free memory when no longer needed.
Example
Below is a simple example that
shows how we can parse a string that contains
tokens separated with /
characters
into those individual tokens.
This particular example could have been done with the strtok library function but the same technique, with a bit of extra code, can be used to handle things that strtok cannot handle, such as scanning through quoted strings or handling escape characters. For more complex tasks, you're better off implementing a state machine or using a formal lexical analyzer.
Save this file by control-clicking or right clicking the download link and then saving it as nomalloc.c.
Compile this program via:
If you don't have gcc, You may need to substitute the gcc command with cc or another name of your compiler.
Run the program: