how can I write a function return array in c++??
6 replies to this topic
#1
Posted 22 September 2010 - 11:59 PM
|
|
|
#2
Posted 23 September 2010 - 01:05 AM
You can't return array from the function because that array would be declared inside the function, and its location would then be the stack frame. However, stack frame is erased when function exits. Functions must copy return value from stack frame to return location, and that's not possible with arrays.
You can pass array as input parameter of the function and then make the function modify it:
Clean way to do this is to pass the pointer to the function, and then make the function return the same pointer (if caller wants to use it further):
You can pass array as input parameter of the function and then make the function modify it:
void f(char c, char array[])
{
array[0] = c;
array[1] = '\0';
}Another way is to return pointer from the function and to allocate array dynamically inside the function:
char* g(char c)
{
char *array = new char[2];
array[0] = c;
array[1] = '\0';
return array;
}The second method is not advised because function g allocates memory on heap but does not deallocate it, which creates a memory leak hazard. It is advised that allocation is made by the same entity which deallocates the memory.Clean way to do this is to pass the pointer to the function, and then make the function return the same pointer (if caller wants to use it further):
char *h(char c, char *array)
{
array[0] = c;
array[1] = '\0';
return array;
}
This function can be called either with dynamically allocated array on the heap, or with statically allocated array. For example:
char array[2];
printf("%s\n", h('x', array));
#3
Posted 25 September 2010 - 03:16 AM
You can. But that would be in the form of a pointer.
#include <stdio.h>
#define ASIZE 2
int *pf(int i, int j) {
int k[ASIZE];
k[0] = i; k[1] = j;
return k;
}
int main () {
int *p;
p = pf(1, 2);
printf("%d, %d", p[0], p[1]);
return 0;
}
#4
Posted 25 September 2010 - 07:18 AM
This is incorrect. You can't return arrays this way. The most probable consequence of that in a standard program is a memory corruption and a possible crash.
When you declare k as an array inside pf, it is a local variable available only inside that function. When this function terminates, the memory reserved to that variable is released. You return it's address through return k, but it is no longer reserved, so its contents are not guaranteed to be preserved.
In fact, if I try to compile this program with gcc, I get a warning:
and if I run the program, I get:
When you declare k as an array inside pf, it is a local variable available only inside that function. When this function terminates, the memory reserved to that variable is released. You return it's address through return k, but it is no longer reserved, so its contents are not guaranteed to be preserved.
In fact, if I try to compile this program with gcc, I get a warning:
Quote
test.c: In function ‘pf’:
test.c:7:5: warning: function returns address of local variable
test.c:7:5: warning: function returns address of local variable
# ./test 0, 0When I should get 1, 2.
#5
Posted 25 September 2010 - 07:26 AM
I actually tested this on gcc (mingw) as well. All seems fine for me. Except for that warning. But oh well, I guess I learned something bout that. Thanks!
#6
Posted 25 September 2010 - 09:15 AM
You cannot return locally allocated array because it is placed on stack, as part of the current stack frame, which is removed from stack as soon as return statement is reached. Those locations would then be probably overwritten if any other function is called, so array returned in such way would contain garbage.
Examples given in my previous post are correct. You either allocate array dynamically using new[] operator, or allocate it statically in the caller's context and then pass it to the function.
Examples given in my previous post are correct. You either allocate array dynamically using new[] operator, or allocate it statically in the caller's context and then pass it to the function.
#7
Posted 25 September 2010 - 02:03 PM
You would do better to wrap the array inside a class and return the class.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









