+ 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?
10 Antworten
+ 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
+ 9
Martin Taylor
Ohh! I misunderstand the question! Thanks for clarifying
+ 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✌️
+ 1
void printName(const string& name)
{
std::cout << name << std::endl;
}
Look at pass by reference and const keyword.
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.
0
Shadow could you answer this? I think you understand c++ better.
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.
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!
- 1
Martin Taylor did you run the code. I think you should read what r-value reference is. Ask any c++ programmer.
- 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;
}