[C] Reusing pointer for new string

I was curious to know a good method to reuse a char pointer:

1
2
3
4
5
char* strfunc (char *sp, int length) {
    if(!sp) free(sp);
    sp= (char *)calloc (length, sizeof(char));
    return sp;
}


If this suffices, say I were to initialize a string without using strfunc:

1
2
char *str= "a String";
strfunc(str);


would this be an issue with free() as I have not allocated the mem
using either malloc or calloc?
Last edited on
Yes, it would be an issue (causes undefined behavior). Don't free memory that you didn't malloc/calloc.

Furthermore, char *str= "a String"; is not valid modern C or C++. (Edit: Well, okay, it might still compile, but it's bad practice!
It would need to be const char *str = "string literal";

It should be const char *str = "string literal"; since you are not modifying the data you are pointing to
https://stackoverflow.com/questions/9834067/difference-between-char-and-const-char
Last edited on
just reuse it? You appear to be overthinking something.
step 1) allocate a char* to a good size that will hold the data you want it to hold. eg char* cp = calloc(100,1);
step 2) initialize it. eg cp[0] = 0; strcpy(cp, "some text");
step 3) code
step 4) reuse the string. cp[0] = 0; strcpy(cp, "different words");
step 5) end of program or logical scope/block, free the memory.

note how the above approach avoids extra memory management. Unless you are on a tiny embedded machine or dealing with huge data, don't sweat the memory so much, just allocate as much as you will need (if you know, which you should have a sense of max size for string variables) once and reuse it and free it at program end.
Topic archived. No new replies allowed.