I need to write a function that takes a delimited string and produces an array of strings (just like string.Split() in C#).
Is there a way to allocate this array on the stack so that the caller does not have to worry about freeing each pointer?
I'm using GCC C and only have access to standard libraries.
I am a novice in C, but I understand that stack space is quite limited. These arrays should be on the short side as they just contain unix paths.
The reason I am trying to do this is
a) because the code would look cleaner without having the free code in the caller
b) i'm doing this for a class, and the professor specified:
parse path – split into array of strings. (if you allocate memory here, be sure to free it before you return)
which is not really a requirement, but made me wonder how this is accomplished because as I understand it to accomplish this you have to use strtok and allocate a copy of each string onto the heap, copying and enlarging your array when you reach capacity.
Is there a way to allocate this array on the stack so that the caller does not have to worry about freeing each pointer?
I'm using GCC C and only have access to standard libraries.
I am a novice in C, but I understand that stack space is quite limited. These arrays should be on the short side as they just contain unix paths.
The reason I am trying to do this is
a) because the code would look cleaner without having the free code in the caller
b) i'm doing this for a class, and the professor specified:
parse path – split into array of strings. (if you allocate memory here, be sure to free it before you return)
which is not really a requirement, but made me wonder how this is accomplished because as I understand it to accomplish this you have to use strtok and allocate a copy of each string onto the heap, copying and enlarging your array when you reach capacity.
Comment