Like Tree2Likes

returning int array from a function

This is a discussion on returning int array from a function within the C Programming forums, part of the General Programming Boards category; All...I have following code snippet...I am trying to return a int array from the function.....I am new to C. This ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    10

    returning int array from a function

    All...I have following code snippet...I am trying to return a int array from the function.....I am new to C. This causes compile errors could someone please provide me some help.....

    Thanks much in advance....

    Code:
    int * getArray()
    {
    
    int* srvChanged[11]
    memset(srvChanged,0,(11 * sizeof(int)));
    
    for (rowNumber = 0; rowNumber < 11; rowNumber++)
    {
       srvChanged[rowNumber] = serviceRegT[rowNumber].id // This is another array structure
    }
    
       return srvChanged;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by rubu View Post
    I am trying to return a int array from the function.....
    That's a shame. You really shouldn't do that. The usual method is to pass the array into the function instead.

    If, for whatever reason, you actually do need to return an array (hint: you don't), then you would need to acquire permanent memory for the array using malloc and friends, as declaring an array inside the function means the array goes away when the function ends, leaving you with a pointer to nowhere.
    themightyadam likes this.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,624
    srvChanged is a static array, therefore when the function returns its memory is returned to free storage. Using the array after that you yield undefined behaviour.
    Devoted my life to programming...

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Sipher View Post
    srvChanged is a static array, therefore when the function returns its memory is returned to free storage. Using the array after that you yield undefined behaviour.
    You mean it isn't static.

    It's also an array of pointers to integers, not an array of integers.

    They could however allocate some memory dynamically and return it and treat it like an array:
    Code:
    int *getarray( size_t s )
    {
        int *array = NULL;
        if( s )
            array = malloc( s * sizeof *array );
        return array;
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    10
    Quzah,

    Thanks for being patient......

    I do not see much difference between what you suggest and what I am doing....why can't I use memset instead of malloc?

    Quote Originally Posted by quzah View Post
    You mean it isn't static.

    It's also an array of pointers to integers, not an array of integers.

    They could however allocate some memory dynamically and return it and treat it like an array:
    Code:
    int *getarray( size_t s )
    {
        int *array = NULL;
        if( s )
            array = malloc( s * sizeof *array );
        return array;
    }

    Quzah.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What is it you are trying to do? (In words, not in actual code, explain what you want to do.)

    memset doesn't allocate memory. It sets memory to a specific value (memory you have already allocated some place). malloc actually allocates memory. Also, as I explained, you have an array of pointers, which is different than what I did.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rubu View Post
    All...I have following code snippet...I am trying to return a int array from the function.....I am new to C. This causes compile errors could someone please provide me some help.....

    Thanks much in advance....

    Code:
    int * getArray()
    {
    
    int* srvChanged[11]
    memset(srvChanged,0,(11 * sizeof(int)));
    
    for (rowNumber = 0; rowNumber < 11; rowNumber++)
    {
       srvChanged[rowNumber] = serviceRegT[rowNumber].id // This is another array structure
    }
    
       return srvChanged;
    }
    Ok, here's what happens...
    1) you enter the function.
    2) it creates your array of pointers to integers on the program's stack
    3) you assign integer values to pointers from a global array (error #1 and 2)
    4) You come to the end of the function
    5) The function returns a pointer to your array of pointers to integers (error #3)
    6) Now the function cleanup, invoked at the closing brace destroys your array from the stack
    7) You go back to your main function and try to use the pointer (error #4)
    8) The next function call comes along and overwrites the memory.
    9) Now your "returned array" is filled with garbage values from another function (error #5)

    Basically trying to return an array from a function like that leaves you with a "pointer to nothing"... and all the problems that hints at.

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    10
    OK I was in the understanding of memset also allocate memory while setting to specific value....

    So in your way.....

    I believe when I call the function....in some where in the main....I need to free the allocated memory? is that correct.

    Quote Originally Posted by quzah View Post
    What is it you are trying to do? (In words, not in actual code, explain what you want to do.)

    memset doesn't allocate memory. It sets memory to a specific value (memory you have already allocated some place). malloc actually allocates memory. Also, as I explained, you have an array of pointers, which is different than what I did.


    Quzah.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by rubu View Post
    OK I was in the understanding of memset also allocate memory while setting to specific value....
    Maybe you were thinking of calloc.
    Quote Originally Posted by rubu View Post
    I believe when I call the function....in some where in the main....I need to free the allocated memory? is that correct.
    Yes. Every time you allocate memory, you should be the one to free it when you no longer need it.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    May 2011
    Posts
    10
    Quote Originally Posted by quzah View Post
    Maybe you were thinking of calloc.Yes. Every time you allocate memory, you should be the one to free it when you no longer need it.

    Quzah.
    So when returning the pointer to array.....how important is doing a malloc? is it really needed?

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by rubu View Post
    So when returning the pointer to array.....how important is doing a malloc? is it really needed?
    If you want to return pointers to memory, then that memory has to exist after the function call ends. (The only other method to do that, other than malloc, is to maintain a static array and return a pointer to that. However, in that case, you only have one chunk of memory no matter how often you call the function -- that is, calling the function again will reuse the same memory, not get you more memory.)

  12. #12
    Registered User
    Join Date
    May 2011
    Posts
    10
    Quote Originally Posted by tabstop View Post
    If you want to return pointers to memory, then that memory has to exist after the function call ends. (The only other method to do that, other than malloc, is to maintain a static array and return a pointer to that. However, in that case, you only have one chunk of memory no matter how often you call the function -- that is, calling the function again will reuse the same memory, not get you more memory.)
    Got it....Thanks so much....again thanks so much for being patient...

  13. #13
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,624
    Quote Originally Posted by quzah View Post
    You mean it isn't static.
    Ops, you caught me there! I forgot "n't" after "is".
    Devoted my life to programming...

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Why you should never try to return arrays from functions...

    Code:
    #include <stdio.h>
    
    int* MyFunction(int a, int b, int c)
      {  static int array[3];
         array[0] = a;
         array[1] = b;
         array[2] = c;
         return array;  } // return a pointer.
    
    
    int main (void)
      { int *a1, *a2;  // int pointers
    
        printf("calling a1 = MyFunction(10,20,30);\t");
        a1 = MyFunction(10,20,30);
        printf("a1 has %d %d %d\n",a1[0],a1[1],a1[2]);
    
        printf("calling a2 = MyFunction(100,200,300);\t");
        a2 = MyFunction(100,200,300);
        printf("a2 has %d %d %d\n",a2[0],a2[1],a2[2]);
    
        printf("\nLooks good, except...\t"); 
        printf("a1 now has %d %d %d\n",a1[0],a1[1],a1[2]);
    
        getchar();
        return 0; }
    mike65535 likes this.

  15. #15
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,624
    Well, you could allocate them on the heap, but that could easily result in a memory leak.
    Devoted my life to programming...

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. returning a 2D array from a function
    By sauwen in forum C Programming
    Replies: 5
    Last Post: 11-19-2010, 04:51 PM
  2. c++ -- returning an array from function
    By p4plus2 in forum C++ Programming
    Replies: 25
    Last Post: 08-18-2008, 01:48 PM
  3. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  4. returning an array from a function
    By BubbleBoy in forum C Programming
    Replies: 1
    Last Post: 02-20-2003, 11:41 AM
  5. returning a 2D array in a function
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 01-05-2002, 07:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21