#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    13
    Rep Power
    0

    Passing local array to a function


    If I declare an array locally and then pass it to a function, can i use the values of the array in that function? Or is not guaranteed? If so should i declare the array as global or static? Thanks.
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,413
    Rep Power
    1871
    There should be no problem with that.

    Overrunning local arrays is usually more immediately catastrophic though, as that usually trashes the call stack.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    13
    Rep Power
    0
    Originally Posted by salem
    There should be no problem with that.

    Overrunning local arrays is usually more immediately catastrophic though, as that usually trashes the call stack.
    Why is it not a problem? It's going out of scope. Can't any other function etc write over it? Or you say, it is on the stack and it is not overwritten until the stack is full?
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,413
    Rep Power
    1871
    > If I declare an array locally and then pass it to a function
    Be careful with your English semantics.

    This is pass an array TO a function.
    Code:
    void printMsg ( void ) {
        char msg[]="hello world");
        printf("%s\n",msg);
    }
    There is no scope issue, since the array remains a valid object whilst printf is doing it's thing.
    When printMsg() itself returns, then msg goes out of scope.


    This is returning an array FROM a function, which is broken.
    Code:
    char *getMsg ( void ) {
        char msg[]="hello world");
        return msg;
    }
    msg goes out of scope with the function return, so anything doing p = getMsg(); is going to find p point nowhere useful.

    Edit: clarify
    Last edited by salem; November 7th, 2012 at 11:00 AM.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,253
    Rep Power
    2222
    Seraphon, in salem's example:
    Code:
    char *getMsg ( void ) {
        char msg[]="hello world");
        return msg;
    }
    the msg array, being an auto class local array, is created on the stack and goes out of existence when you return from the function, so the pointer that's returned is pointing to memory that's about to get reused by other functions. Won't work.

    However, if you declare that local array as static:
    Code:
    char *getMsg ( void ) {
        static char msg[]="hello world");
        return msg;
    }
    then it is created in static memory along with the global variables and persists (ie, continues to exist) after the function call. Of course, its name is only known inside the function, but since you have a pointer to it that provides you with access to it.

    And as salem advises, please review the difference between the English language concepts of "to" and "from".
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    13
    Rep Power
    0
    Originally Posted by dwise1_aol
    And as salem advises, please review the difference between the English language concepts of "to" and "from".
    actually there is no problem with my telling, i confused the scope. Because i thought that, if I call a function from a function I'll be out of scope until I return from the function that I'd called. Sorry and thanks for the clarification.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0
    Hi,

    I think its possible, you can declare array in function. Its properly work.

    Thnaks!

IMN logo majestic logo threadwatch logo seochat tools logo