The answer has already been asked, but since you marked the question as C ++, not C, this is how you probably want to do it in C ++ (of course, there may be other reasons not to do this, but there’s little chance).
vector<char> (int ran){ vector<char> ret(char);
And to call him:
for(int i = 0; i < 100000000; i++){ vector<char> str = test(rand()%10000000+10000000);
There are no new ones, therefore it is not deleted, therefore there is no memory leak.
Actually, you also probably want to use std :: string instead of char * (I used a vector to give a more general example).
Do not worry about the data to be backed up. The compiler optimizes it. Edit : okay, this may not optimize it :) however there are great chances. And until there are performance issues, go to the simplest version.
Tristram gräbener
source share