Yes, there is a some confusion in the code.
You are allocating space for a temp string at the start, but then overwriting the pointer (which will cause a memory leak).
Code:
char* temp = new char[strlen((char *)str)];
temp = (char *)str;
Then later on, you are copying the partial strings to a lineHolder buffer that has only been allocated on the stack.
Code:
char lineHolder[_IO_TEXT_ALLOCATION_LINE_SIZE];
...
_textLines.add(lineHolder);
When the function exits, not only will the pointers in _textLines all be the same, but they will be pointing to memory that will be overwritten as the stack is used.
Instead, use the new[] to allocate the lineHolder buffer each time around the loop (after calculating the size), so that each line buffer is on the heap.
Code:
char *lineHolder = new char[i-k+1];
Better still, take
exscape's suggestion and use the standard template library strings, which will hide a lot of the issues to do with memory allocation.
If you intend to write a significant amount of C++ code, it is worth also being aware of the
boost libraries, which can handle some of these basic algorithms. For example, splitting up the string into a list container becomes:
Code:
#include <string>
#include <list>
#include <boost/algorithm/string.hpp>
...
std::string str("This is a line\nThis is a second line\nThis is the third line");
std::list<boost::iterator_range<std::string::iterator> > list;
boost::split(list, str, boost::is_any_of("\n"));