• MICROSOFT:A very simple question

    Spot The Error :-



    char * favorite_fruit () 
    {
    char deciduous [] = "apple";
    return deciduous;
    }


    The automatic variable deciduous is allocated on the stack when the function is entered; after the
    function is exited, the variable no longer exists, and the stack space can be overwritten at any time.
    Pointers that have lost their validity in this way (by referencing something that is no longer live) are
    known as "dangling pointers"—they don't reference anything useful, just kind of dangle in space. If
    you need to return a pointer to something defined in a function, then define the thing as static.
    This will ensure that space for the variable is allocated in the data segment instead of on the stack.

2 comments:

  1. yar can u pls elaborate it a few more...means wat does return deciduos technically means...

  2. Unknown said...

    brother see deciduous is an array and when we pass an array name in a function its address is passed.
    Like

    call_abc(arr) is equivalent to call_abc(&arr)

    So here too..base address of arrary decidous is returned.
    Hope you got the answer :)

Post a Comment