简体   繁体   中英

Free for consistent dynamically allocated char pointers?

I have a struct containing a bunch of char pointers whose values are used throughout the program's lifetime. Most are overwritten every iteration.

Should these values be freed at any point besides program exit? Should the value in the char pointer be freed before overwriting it with strdup("new value") ?

@some-programmer-dude is right, there will be memory leak without free if your program works like a server.

BTW, all memory will be released after the program exited no matter whether you call free() or not.

You already got the answer. I'm just providing more information and reference to it.

Man page of POSIX standard strdup .

Here we go,

  The strdup() function returns a pointer to a new string which is a duplicate of the string s. Memory for the new string is obtained with malloc(3), and can be freed with free(3). 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM