+ 2

How to pass a string type to a function without allocating any heap memory and use it just for read only purposes in safer way?

4th Dec 2020, 2:29 AM
Lucas
14 Réponses
+ 6
I wouldn't think that, but I feel there is some lack of context: Are you trying to pass a dynamic string, or a string literal? In the first case, the previous answers are indeed correct. The allocation you see in your program is made internally by the std::string class, as it needs to handle strings of arbitrary length. So the memory comes from the fact that a std::string is constructed, not from passing it by reference. Try running the following instead in your main(): string s( "Hello" ); print( s ); print( s ); If the pass by reference allocated memory on the heap, you would see multiple entries by new(). However, if your question is about passing a string literal, then that changes a lot, because a literal is a const char* and can be placed on the stack, since it is known at compile time. In that case, you could favor std::string_view over std::string, which doesn't allocate memory on the heap, as far as I know: https://en.cppreference.com/w/cpp/string/basic_string_view
4th Dec 2020, 5:55 PM
Shadow
Shadow - avatar
+ 9
Martin Taylor Ohh! I misunderstand the question! Thanks for clarifying
4th Dec 2020, 3:49 PM
Piyush
Piyush - avatar
+ 7
Lucas Till far I understand your query I can say that You can not return temporary from a function and unless you use a "malloc" your character array defined in the function will be a temporary. An alterantive solution is to pass a character array as parameter to the function and use it as output parameter! Hope this is what you was asking for✌️
4th Dec 2020, 6:07 AM
Piyush
Piyush - avatar
+ 1
void printName(const string& name) { std::cout << name << std::endl; } Look at pass by reference and const keyword.
4th Dec 2020, 3:39 AM
Akib
Akib - avatar
0
Akib Reza this will create a temp value, which allocates memory. Piyush[21 Dec❤️] const char* is a common way, which some c++ programmers that i know won't like it, since it is considered unsafe. and it's a void function.
4th Dec 2020, 6:50 AM
Lucas
0
Shadow could you answer this? I think you understand c++ better.
4th Dec 2020, 5:38 PM
Lucas
0
Shadow thanks. I know if a string is l-value then it is okay to pass it as const/ref. But I needed to pass it as read only without any heap memory and not using const char*. std::string_view is a safer solution. I just need c++17. Thanks again. But some people need to understand c++ better before claming something as fact.
4th Dec 2020, 6:04 PM
Lucas
0
Akib Reza I don't have anything against you. You gave a reasonable solution. My question was probably not clear enough. So, sorry about that. Some people do act like experts here on this/ or any other platform. And some don't even know why they up vote or down vote. they just do. I saw codes got 15+ up votes; however, the code didn't even compile. Knowing the compiler and its documentation is a good idea. You should how your best friend (compiler) works, if you wanna make a living out of C++. Anyway, I got my answer. Thanks for your feedback. Good luck!
4th Dec 2020, 8:03 PM
Lucas
- 1
Martin Taylor did you run the code. I think you should read what r-value reference is. Ask any c++ programmer.
4th Dec 2020, 5:18 PM
Lucas
- 2
Martin Taylor I think you are wrong. Akib's solution does create a std::string in the heap. check this out: https://onlinegdb.com/BJE8ZJuiv this won't run on soloLearn. #include <iostream> #include <new> #include <string> void* operator new(size_t size) { std::cout << "memory -> " << size << '\n'; return malloc(size); } void print(const std::string& s) { std::cout << s << '\n'; } int main() { print("hello"); return 0; }
4th Dec 2020, 4:21 PM
Lucas