Returning Array from function in C

前端 未结 5 401
醉话见心
醉话见心 2021-01-24 14:44

For some reason, my function is only returning the first element in my array and I cannot figure out why the rest of the array goes out of scope. The function takes two integer

相关标签:
5条回答
  • 2021-01-24 15:00

    You are doing stack allocation for your array, and stack-allocated storage goes away when a function returns. Use heap allocation instead.

    Change

    int total[10];

    to

    int* total = malloc(10*sizeof(int)).

    of course, that means you must also free the memory after you are done with it. In this case, before the return 0; in main, you need to

    free(x);
    
    0 讨论(0)
  • 2021-01-24 15:01

    I agree with every other answer regarding automatic (stack) and heap memory. All good approaches, however global variables on the stack are also an option. It is important to note that it was not that you chose to use stack memory, but that it was also local in scope. Locally scoped automatic variables die when the function that created them returns. However, globally scoped variables, also stored using stack memory, live for the duration of your program, and therefore provide an alternative approach to solve your problem...

    A one line change will result in your code running, change your local copy of total (automatic scope) to a global:

    int total[10];//Put this here,  outside of any function block in same file, i.e. (will give it global scope)
    //note, commented this in function below
    
    int *sumarrays(int arr1[], size_t arr1len, int arr2[], size_t arr2len) {
        int i;
        //int total[10];//move this to global scope
    
    
        for (i = 0; i < 10; i++) {
            total[i] = *(arr1 + i) + *(arr2 + i);
    
        }
        for (i = 0; i < 10; i++) {
            printf("%d\t", total[i]);
        }
    
        printf("\n");
        return total;
    }
    

    With this approach, global scope of variable allows your return to be successful. The array total keeps it's existence on stack until program exits.

    0 讨论(0)
  • 2021-01-24 15:09

    The problem is that when sumarrays returns, the total array ceases to exist1, so the pointer value that main receives is no longer valid. In short, you cannot return arrays from a function like this.

    You have several options.

    The first (and IMO the preferred option) is to pass the total array as one of your parameters, and make the caller responsible for setting aside enough memory:

    void sumarrays(int arr1[], size_t arr1len, int arr2[], size_t arr2len, int *total, size_t totallen) 
    {
      ...
      for (i = 0; i < totalLen; i++) {
          total[i] = *(arr1 + i) + *(arr2 + i);
      ...
    }
    
    #define ARRLEN 10
    
    int main( void )
    {
      ... 
      int x[ARRLEN];
      ...
      sumarrays( arr1, ARRLEN, arr2, ARRLEN, x, ARRLEN);
    

    In this method, the caller (main) is responsible for knowing how big the target array needs to be and setting aside the memory for it. The helps decouple the sumarrays function from main because it doesn't have to rely on information that isn't explicitly specified by the parameter list (such as the array sizes). With this approach, you're not limited to arrays of size 10.

    A second option is to declare total such that it doesn't go away after sumarrays exits. You can do this by either declaring it at file scope (as in ryyker's answer), or by declaring it static within the sumarrays function:

    int *sumarrays(int arr1[], size_t arr1len, int arr2[], size_t arr2len) {
        int i;
        static int total[10];
        ...
    

    With this approach, the total array will be allocated at program startup and held until the program terminates. What makes this approach less desireable is that you have only a single instance of that total array, which is shared across all calls to sumarrays. The function is no longer re-entrant; if sumarrays called another function that called sumarrays, then whatever the first call had written to total would be clobbered by the second call (the strtok library function has this problem, which has caused much heartburn in the past). Your code obviously doesn't have this problem, but it's something to be aware of. Don't declare things static if you don't have to.

    The final option is for sumarrays to allocate the memory for total dynamically (as in haccks' and merlin2011's answers). That avoids the re-entrancy issue, but now you have to deal with memory management. C isn't your mother and won't clean up after you, so any memory you allocate with malloc or calloc you have to release with free2.


    1. Logically speaking, anyway; the memory is obviously still there, but it's been made available for the rest of the system to use, and has been overwritten in the time it took for your program to return to the main function.

    2. Most platforms will reclaim dynamically allocated memory when the program exits, but a few (admittedly older, oddball platforms) may not. Also, for long-running programs (servers, daemons, etc.), you have to be very careful to release any memory you allocated and no longer need, otherwise you will eventually run out.

    0 讨论(0)
  • 2021-01-24 15:15

    You can't return a pointer to an automatic local variable. total is an automatic local variable and it doesn't exist after function body executed.

    Pointer to static local variable or dynamically allocated variable can be returned. Change
    int total[10]; to

    int *total = malloc(10 * sizeof(int));
    
    0 讨论(0)
  • 2021-01-24 15:23

    my function is only returning the first element in my array and I cannot figure out why the rest of the array goes out of scope.

    No, it returns a pointer to the first element - and the entire array goes out of scope. Any data you see that happens to remain as it was in the function is by luck rather than judgement, and you cannot use it as it is released for use by other functions.

    Arrays are not first class data types in C - you cannot return an array by copy.

    The normal solution is to have the array owned by the caller and to pass its address and size to the function.

    int* func( int* arr, int arrlen )
    {
        // write to arr[0] to arr[arrlen-1] ;
    
        return arr ;
    }
    
    int main()
    {
        int array[256] ;
        func( array, sizeof(arr)/sizeof*(*arr) ) ;
    }
    

    Other less common and generally ill-advised possibilities are either to return a struct containing the array (a struct is a first class data type), but to do that is somewhat inefficient in many cases, or to dynamically allocate the array within the function, but it is not a particularly good idea since the caller has the responsibility of freeing that memory and may not be aware of that responsibility - it is a recipe for a memory leak.

    0 讨论(0)
提交回复
热议问题