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? | SoloLearn: Learn to code for FREE!


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?


12/4/2020 2:29:48 AM


14 Answers

New Answer


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:


Martin Taylor Ohh! I misunderstand the question! Thanks for clarifying


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✌️


Piyush[21 Dec❤️], Lucas was not asking about returning a string from a function. He was asking about passing a read only string to a function.


I see where you're coming from now. In your main routine you are calling print with a literal print("hello"); This is not a string object. Since your print function takes a reference to a string object... void print(const std::string& s) the compiler needs to create a string object first before it can pass a reference to it to the function. The object has to exist before a reference to it can be used. When a string object already exists another object is not created when we pass by reference.


void printName(const string& name) { std::cout << name << std::endl; } Look at pass by reference and const keyword.


Lucas, the solution presented by Akib Reza is absolutely correct. The const keyword means that the string is read only. The & means it is passed by reference. No new copy of the string is created. The reference is passed on the stack not the heap. This is the safest way to pass a read only string.


Lucas you do not understand pass by reference. No new operator is used when passing by reference.


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.


Shadow could you answer this? I think you understand c++ better.


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.


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!


Martin Taylor did you run the code. I think you should read what r-value reference is. Ask any c++ programmer.


Martin Taylor I think you are wrong. Akib's solution does create a std::string in the heap. check this out: 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; }