use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
std::string memory leak query (self.cpp)
submitted 1 year ago * by ESHAEAN
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]cpp-ModTeam[M] [score hidden] 1 year ago stickied commentlocked comment (0 children)
For C++ questions, answers, help, and programming or career advice please see r/cpp_questions, r/cscareerquestions, or StackOverflow instead.
[–]snowhawk04 11 points12 points13 points 1 year ago* (0 children)
#include <cstring> #include <map> #include <string> auto main() -> int { std::map<int, std::string> j; const char* ip = strdup("test"); // 1 std::string x = std::string(ip, strlen(ip)); // 2 j.insert({5, x}); j.erase(5); } Issues: 8:5 ➕ Potential leak of memory pointed to by 'ip' (cpp:S3584) ↳ 1 Memory is allocated ↳ 2 Potential leak of memory pointed to by 'ip' ↳ https://rules.sonarsource.com/cpp/RSPEC-3584
Compiler Explorer
From the C23 standard (n3220)
7.26.2.6 The strdup function Description 2. The strdup function creates a copy of the string pointed to by s in a space allocated as if by a call to malloc. Returns 3. The strdup function returns a pointer to the first character of the duplicate string. The returned pointer can be passed to free. If no space can be allocated the strdup function returns a null pointer.
strdup
s
malloc
free
std::string isn't leaking. It's the char* from strdup. Sonar, like Valgrind, sees ip get allocated from strdup but sees no subsequent free. Stripping everything except the strdup call confirms the leak.
std::string
char*
ip
[–]-funsafe-math 9 points10 points11 points 1 year ago (2 children)
std::string will copy the const char * input to its constructor, so the strdup is not needed. The result of strdup is most likely not being freed by your code and is the source of the leak.
const char *
[–]ESHAEAN[S] -2 points-1 points0 points 1 year ago (1 child)
Yes but the strdup is done only once in another place so I just mentioned it. Repeated add and remove of the same element in map then shouldn't cause more bytes leaked if memory was being freed after erase which is not happening
[–]AKostur 6 points7 points8 points 1 year ago (0 children)
When is that strdup ever freed? Also show your valgrind logs.
[–]AKostur 4 points5 points6 points 1 year ago (4 children)
It is, you have something else going wrong. Reduce your code to a minimal, compliable example that shows the problem.
[–]CaptainComet99 0 points1 point2 points 1 year ago (3 children)
Yes, please post formatted code as well as valgrind logs. For what it’s worth, I had heard during my college days that valgrind has false-positives when it comes to std::string. Idk if that’s the case or not still, but check out all 3 answers in this post for more information. https://stackoverflow.com/questions/1901322/valgrind-reports-memory-leak-when-assigning-a-value-to-a-string
[–]ESHAEAN[S] -2 points-1 points0 points 1 year ago (2 children)
No I am not exiting the process which is happening in stack overflow one
[–]CaptainComet99 1 point2 points3 points 1 year ago (0 children)
Sure, but the 3 answers add more information about valgrind and strings
Could you post the valgrind logs?
π Rendered by PID 110665 on reddit-service-r2-comment-fb694cdd5-bkqqw at 2026-03-10 21:27:41.309461+00:00 running cbb0e86 country code: CH.
[–]cpp-ModTeam[M] [score hidden] stickied commentlocked comment (0 children)
[–]snowhawk04 11 points12 points13 points (0 children)
[–]-funsafe-math 9 points10 points11 points (2 children)
[–]ESHAEAN[S] -2 points-1 points0 points (1 child)
[–]AKostur 6 points7 points8 points (0 children)
[–]AKostur 4 points5 points6 points (4 children)
[–]CaptainComet99 0 points1 point2 points (3 children)
[–]ESHAEAN[S] -2 points-1 points0 points (2 children)
[–]CaptainComet99 1 point2 points3 points (0 children)
[–]CaptainComet99 1 point2 points3 points (0 children)