Returning an Array through a function

I want to create an Bi-Dimensional Array of type char in a function, and then return that value.
Is there a way to do so?
It's a bit tricky.

1. You could dynamically allocate the array, but then the caller of the function has to remember to delete it.
2. You could have a variable in the function: static char array[put an integral size here], but there is a problem here too, since if you call the function twice, it will change the array that both calls of the function received.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
char* function()
{
   static char buffer[64];
   // do stuff to buffer
   // ...
   return buffer;
}

int main()
{
   // etc
   char* array1 = function();
   char* array2 = function(); // as array1 and array2 point to the same place, this second call will change array1 as well
   // etc
}


If you are dealing specifically with character arrays,, I would recommend that you return std::string by value. You don't need to worry about the overhead of return a large string due to Return Value Optimisation (RVO). And as you've returned by value, you don't need to worry about the array's lifetime ending.
Last edited on
What does static mean?

I tried allocating the array dynamically, but this is what I get:
error C2440: '=' : cannot convert from 'char (*)[5]' to 'char *'


The function is:
1
2
3
4
5
Dimension(int a)
{
	char*board;
	board = new char[a][5];		//Error on this line.
}


The function won't be called for different arrays. I will be looping through a section of the program, so the array will be called multiple times, but not specifically the function.


If you are dealing specifically with character arrays,, I would recommend that you return std::string by value. You don't need to worry about the overhead of return a large string due to Return Value Optimisation (RVO). And as you've returned by value, you don't need to worry about the array's lifetime ending.


I don't get what do you mean. The array I am creating is for the board of a game (specifically Dungeon Crawl from Beginner Exercises article in this site). I was going to use Dots(.) to show each cell of the board.
Sorry, I missed the word "bidimensional" in your original post.
That compiler error is caused by the fact that your are allocating a two dimensional array:
new char[a][5],
so it is not a pointer to a char, but a pointer to a pointer to a char, i.e. it is char** rather than char*.

Making a variable inside a function static means that it will be created (and possibly initialized) the first time the function is called, and on subsequent calls of the function, it will retain the value it had previously.

The reason I suggested it here is the following. If you make this variable:
char buffer[64];
and then return
return buffer;,
then the variable's life ends and it's memory is returned to the stack, so even though the caller has an address of the memory, it no longer contains the content it was given inside the function.

By making the variable static:
static char buffer[64],
we allow it to be returned in this way:
return buffer;.
A static variable is alive for the entire length of the program (roughly) after it has been defined.

However, as I said, if you call the function more than once, and then try to use the char* you got from the first call, it will now contain the same value as that which you got from the second call, as they point to the same place.

As for returning by value an std::string, I simply suggesting this because std::string is basically an array of characters, but it looks after it's own data and can be copied so you can return it and thus avoid all the lifetime issues I have mentioned.

If your array is bidimensional, you can't return an std::string. However, perhaps you shouldn't use a bidimensional array, but a single dimensional array. Believe me, you can achieve the same results if you do it correctly. See here:
http://cplusplus.com/articles/G8hv0pDG/
OK, I will wry it with a uni-dimensional array.
Topic archived. No new replies allowed.