Jump to content

function return array in c++

- - - - -

  • Please log in to reply
6 replies to this topic

#1
eman ahmed

eman ahmed

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
how can I write a function return array in c++??

#2
zoranh

zoranh

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
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:
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
Ruel

Ruel

    Newbie

  • Members
  • PipPip
  • 14 posts
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
dbug

dbug

    Programmer

  • Members
  • PipPipPipPip
  • 155 posts
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:

Quote

test.c: In function ‘pf’:
test.c:7:5: warning: function returns address of local variable
and if I run the program, I get:

# ./test

0, 0
When I should get 1, 2.

#5
Ruel

Ruel

    Newbie

  • Members
  • PipPip
  • 14 posts
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
zoranh

zoranh

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
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.

#7
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
You would do better to wrap the array inside a class and return the class.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users