Returning the Address of a Local Variable (Array)

This is a discussion on Returning the Address of a Local Variable (Array) within the C Programming forums, part of the General Programming Boards category; Code: warning: function returns address of local variable I personally prefer to return a value rather than modify it by ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    129

    Returning the Address of a Local Variable (Array)

    Code:
    warning: function returns address of local variable
    I personally prefer to return a value rather than modify it by reference, but the compiler doesn't like this idea for arrays. What are the hazards involved and can I avoid them without passing a reference? Thanks!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The main hazard is that the array ceases to exist once the function ends, so holding a pointer to it does you No Good. Arrays pretty much have to be passed to the function in the parameter list.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    129
    I'm surprised to get that response... Maybe it's specific to the compiler or environment?
    Code:
    #include <stdio.h>
    #include <string.h>
    
    char* readLine(char* message, int maxSize, short pad){
        char string[maxSize];
        int i;
        printf(message);
        fgets(string, maxSize, stdin);
        for (i = 0; i < maxSize; i++ ){
            if ( string[i] == '\n' ){
                string[i] = '\0';
                if(pad){
                    return string;
                }else{
                    char temp[i + 1];
                    strcpy(temp, string);
                    return temp;
                }
            }
        }
        return string;
    }
    
    int main(){
        char* input;
    
        input = readLine("Please enter a number: ", 256, 1);
        printf( "You entered &#37;s.\n", input);
        return 0;
    }
    On Linux, this outputs:
    Code:
    Please enter a number: 2
    You entered 2.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    It did what you wanted, but what's your point?

    If you put a single bullet in a revolver, put it to your head, pulled the trigger, and lived, would you conclude that such an activity is therefore safe?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    129
    No, but my point is whether there really is a bullet in the revolver. Is that result portable? If so, I don't see any difference between the effects of the two approaches.

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    Quote Originally Posted by Jesdisciple View Post
    No, but my point is whether there really is a bullet in the revolver. Is that result portable? If so, I don't see any difference between the effects of the two approaches.
    The result is not portable. It is simply wrong code. Usually it will not crash but there are circumstances where it might.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And what does this main do for you?
    Code:
    int main(){
        char* input;
        char* input2;
        input = readLine("Please enter a number: ", 256, 1);
        input2 = readLine("Please enter a number: ", 256, 1);
        printf( "You entered &#37;s.\n", input);
        printf( "You entered %s.\n", input2);
        return 0;
    }
    Edit: Of course it doesn't have to be a call to the same function to reuse the memory; try this:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    char* readLine(char* message, int maxSize, short pad){
        char string[maxSize];
        int i;
        printf(message);
        fgets(string, maxSize, stdin);
        for (i = 0; i < maxSize; i++ ){
            if ( string[i] == '\n' ){
                string[i] = '\0';
                if(pad){
                    return string;
                }else{
                    char temp[i + 1];
                    strcpy(temp, string);
                    return temp;
                }
            }
        }
        return string;
    }
    
    int silly(void) {
        int bob[64] = {0x41424344, 0x45464748, 0x49000000};
        return bob[2];
    }
    
    
    int main(){
        char* input;
        int foo;
        input = readLine("Please enter a number: ", 256, 1);
        foo = silly();
        printf( "You entered %s.\n", input);
        printf( "I entered %d.\n", foo);
        return 0;
    }
    Last edited by tabstop; 08-19-2008 at 10:23 PM.

  8. #8
    Registered User
    Join Date
    Aug 2008
    Posts
    129
    tabstop, your test put gibberish in my variable.

    It seems I've been spoiled by the bells & whistles in other languages. Returning an array (or object) is one of the most basic operations to me.

    Thanks!

  9. #9
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Like tabstop said, in C you will usually pass a refernce to you array in as a parameter so the changes made in your function persist.

    Another approach would be to allocate space using malloc inside the function and return the pointer. This issue with that is that you have to remember to free the memory when you are done.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by mike_g View Post
    Like tabstop said, in C you will usually pass a refernce to you array in as a parameter so the changes made in your function persist.

    Another approach would be to allocate space using malloc inside the function and return the pointer. This issue with that is that you have to remember to free the memory when you are done.
    Or, if you are absolutely sure that a case like tabstops first example, you can use a static variable (essentially a global that isn't visible outside of the function). This however does mean that you will have exactly the same problem as tabstops example - if you call readline twice it will overwrite the previous answer.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What does this do (Windows API)?
    By EVOEx in forum Windows Programming
    Replies: 4
    Last Post: 12-19-2008, 09:48 AM
  2. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 06:14 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  5. Returning an Array of Pointers to Objects
    By randomalias in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 02:45 PM

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