-
October 9th, 2018, 10:42 AM
#1
Dynamic Allocated String to std::string
Code:
std::string new_str = data;
free(data);
If data is a string with a null terminate character, will std::string copy all the characters from data?
So am i safe to free the data pointer?
Thank you.
-
October 9th, 2018, 11:14 AM
#2
Re: Dynamic Allocated String to std::string
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 9th, 2018, 11:33 AM
#3
Re: Dynamic Allocated String to std::string
-
October 10th, 2018, 01:41 AM
#4
Re: Dynamic Allocated String to std::string
Originally Posted by babaliaris
So am i safe to free the data pointer?
Yes, but only if you malloced it!
Victor Nijegorodov
-
October 10th, 2018, 05:32 AM
#5
Re: Dynamic Allocated String to std::string
Originally Posted by VictorN
Yes, but only if you malloced it!
I used malloc.
-
October 28th, 2018, 08:53 AM
#6
Re: Dynamic Allocated String to std::string
Question is, why are you using malloc() and free()?
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|