I agree but however you need to free that memory allocated by alpha_val now which is difficult since you need to return alpha_val. How do you free alpha_val in this case.
The srlen(str) + 1 is not the problem . How do we deallocate memory in this case since in the main there is a while loop that will continuously call tokenize and if you dont deallocate the memory, the program might just crash or memory leakage...
Although alpha_val is local to the function you return it at the end. So before the local alpha_val is destroyed a copy of it is returned to the caller. You can use delete[] on the pointers stored in the returned copy.
k so u r saying i can do a return alpha_val and and then delete alpha_val from the calling function. how can you do that when alpha_val from the perspective of the calling function does not exist as it is local only to tokenize func().
so what i did , i created a class and created an object but allow the object to continously go out of scope...but i find this weird.And now i have a problem as i push the values in a list but since the objects go out of scope, the alpha_val is destroyed via destructor and the list prints some garbage values. How do i copy memory from the heap to another region and add it to the list.
When you put a return var; instruction in your code, you are telling the compiler to make a copy of the variable var. So even though your variable in the function goes out of scope, a new variable is created in the scope of the caller that is a copy of the one that went out of scope.