C++: the Function returns an array

Please tell me which method is preferable in order for the function return the array?

For example, there is an array char ch[6]={'hello'}.

How to make it possible to return this array?
October 8th 19 at 00:21
5 answers
October 8th 19 at 00:23
Solution
First of all cannot be initialized, remove single quote, roughly speaking, the code symbol. For example
int a = '2';

put the value '50' — ASCII code of two.

You can do this:
char * MyFunc()
{
 char ch[6]="hello";
 return ch;
}


Note that some compilers will give you warning like "return address of local or temporary variable."

Generally, if you have to do, for example, the processing of the array, and you write this function, you can just pass it a pointer to the array that you want to work, for example:

void MyFunc(char * ch)
{
 //here you can do something with your ch.
}
thank you, it works. the problem was that the function was char MyFunc(). Without *. - Concepcion26 commented on October 8th 19 at 00:26
Uh, and as you so return the address of a local variable and not have to worry about? No one told you in these circumstances does not guarantee anything, because the local a variable can be allocated on the stack. - luciano.Schmi commented on October 8th 19 at 00:29
Return char * to the local variable — you with oak collapsed? You give advice to losers?

alexrublev, in C you cannot return pointer to local variable. If you need to return a string, an array, a structure, a piece of memory, doing differently (there are 2 options):

1) the caller allocates a buffer in memory and passes to the function a pointer and its length. Function fills it with data
2) function dynamically allocates an array on the heap via new and returns a pointer to it. It's a stupid way, as one function allocates memory, the other releases and it's easy to get confused. - antonette.Gislas commented on October 8th 19 at 00:32
> Note that some compilers will give you warning like "return address of local or temporary variable."

Believe me, in si GTA V on garage door these are issued for a reason. These are the people who ignore GTA V on garage door worked at Microsoft as a result of wind consists of a continuous sieve. - antonette.Gislas commented on October 8th 19 at 00:35
Can you a question: if I want to use "nebraski" method, but the calling function does not know how long the buffer needs called? For example, I have a function
char*format(char*str,...);
Otherwise how can I handle this situation "not stupid"?
With the stock taking is not an option. I don't know how it will be used, maybe someone wants the whole MB line adformatie, do not deny me this. - luciano.Schmi commented on October 8th 19 at 00:38
> > Can you a question: if I want to use "nebraski" method, but the calling function does not know how long the buffer needs called?

In General, a good question. I'm honestly at a loss. I think adoratskim way in C without objects and without crutches to make this impossible.

But in C++, thank Stroustrup, with the help of crutches and templates, you can attach a reference count so that the buffer allocated in the function will automatically get rid of uselessness. That is, we pass to the function an empty object or MyString MyArray, and the function fills it with data.

What profit from the transfer of an empty container object to the function instead of dynamically generate and return from the function? That you can pass a local object (allocated on stack) and save CPU time on me-e-edleno dynamic memory allocation.

Ha, and then there are people who refuse to accept the benefits of OOP and write on si. They interesting, such problems are solved? Make a container from a structure with functions? - antonette.Gislas commented on October 8th 19 at 00:41
> > Can you a question: if I want to use "nebraski" method, but the calling function does not know how long the buffer needs called? For example, I have a function
char*format(char*str,...);
In this case, use dynamic structures like stack, queue and so on - Modesto.Conroy60 commented on October 8th 19 at 00:44
Ha, and then there are people who refuse to accept the benefits of OOP and write on si. They interesting, such problems are solved? Make a container from a structure with functions?
Try to Oprogramowanie brains of controllers and processors with the PLO - Modesto.Conroy60 commented on October 8th 19 at 00:47
2 anarsoul: You can't feel the difference between const char *ptr = "my cool string" and char ch[6]="my cool string"? - giles_Dick commented on October 8th 19 at 00:50
> > Can you a question: if I want to use "nebraski" method, but the calling function does not know how long the buffer needs called? For example, I have a function
char*format(char*str,...);

On one side is C-style, C++ has std::stringstream, on the other hand there are no standard methods for determining long, but many have or a solution. - florencio.Abernathy commented on October 8th 19 at 00:53
But in C++, thank Stroustrup, with the help of crutches and templates, you can attach a reference count so that the buffer allocated in the function will automatically get rid of uselessness. That is, we pass to the function an empty object or MyString MyArray, and the function fills it with data.

And it is called shared pointer, and have it in the new standard, as well as in tr1. - florencio.Abernathy commented on October 8th 19 at 00:56
October 8th 19 at 00:25
In General, I'm sorry, I didn't see "C++" in the title. Me the above method is C-way.
In C++ an array is returned as std::vector or in your case, as std::string. It's additionally much safer, and overhead though, but in most cases minor and safety is more important.
October 8th 19 at 00:27
One of the basic rules: never return dynamically allocated memory from a function. Use stl containers, there's a lot you can choose for the occasion. Either std::string.
October 8th 19 at 00:29
I also think that a pointer to an array it is better to pass to the function, not return it. Because if a function allocates memory, then there is the ambiguity of who this memory then needs to be removed? Well, or use "smart pointers" type scoped_array, and return them to the end of their term of life, the allocated memory is removed automatically.
Although, in C++11 can now move constructors that are much more effective in this case.
October 8th 19 at 00:31
And that so does not go:

char*fun()
{
char res[6] = "hello";
char*str = malloc(sizeof(char)*6);
strcpy(str,res);
return res;
}
char * str = (char *)malloc( sizeof(char) * 6 );
In General, if you have not Unicode, the size of a char is 1 byte, so you can just malloc(6). - Concepcion26 commented on October 8th 19 at 00:34
About (char*) right, but the second is really bad. Moreover, the readability falls, and hop — and suddenly Unicode! - luciano.Schmi commented on October 8th 19 at 00:37
sizeof(char) == 1 always, Unicode, nothing to do with. - antonette.Gislas commented on October 8th 19 at 00:40
you heard about strdup? - antonette.Gislas commented on October 8th 19 at 00:43

Find more questions by tags C++