I have a struct
struct request {
int code;
char *message;
};
that I'd like to free properly.
I have the following function to do that:
void free_request(struct request *req) {
if (req->message != NULL) {
free(req->message);
}
free(req);
req = NULL;
}
The problem is that I get an "free(): invalid pointer"/segfault error from the compiler when I try to free a request that has been created using a string literal:
struct request *req;
req = malloc(sizeof(struct request));
req->message = "TEST";
free_request(req);
Since I want to create request structs in different places, once using literals (on the client side) and once using *chars that I read from a socket (on the server side) I was wondering if there is a function to make sure that I don't try to free the literals while still allowing me to free the message I have created using a malloc.