Accessing data from another function

Discussion in 'C Programming' started by subhajitdey.m.sc@gmail.com, Sep 19, 2012.

  1. Guest

    In the code below:

    int *check(int a){
    int b,*s;
    b=a;
    s=&b;
    return s;
    }
    int main(){
    int *a,x;
    a=check(10);
    x=*a;
    printf("%d",x);
    }


    shouldn't there be some sort of error (or garbage value) when I access the address of the local variable in 'check'?
    , Sep 19, 2012
    #1
    1. Advertising

  2. Rui Maciel Guest

    wrote:

    > In the code below:
    >
    > int *check(int a){
    > int b,*s;
    > b=a;
    > s=&b;
    > return s;
    > }
    > int main(){
    > int *a,x;
    > a=check(10);
    > x=*a;
    > printf("%d",x);
    > }
    >
    >
    > shouldn't there be some sort of error (or garbage value) when I access the
    > address of the local variable in 'check'?


    Your code refers to an object outside of its lifetime, which is a behaviour
    that was intentionally left undefined in the standard, and regarding which
    the standard imposes no requirements.

    Having said that, it would be nice if compilers at least threw a warning
    about that, but I suspect that catching this sort of problem is no easy
    task.


    Rui Maciel
    Rui Maciel, Sep 19, 2012
    #2
    1. Advertising

  3. James Kuyper Guest

    On 09/19/2012 01:11 PM, wrote:
    > In the code below:
    >
    > int *check(int a){
    > int b,*s;
    > b=a;
    > s=&b;
    > return s;
    > }
    > int main(){
    > int *a,x;
    > a=check(10);
    > x=*a;
    > printf("%d",x);
    > }
    >
    >
    > shouldn't there be some sort of error (or garbage value) when I access the address of the local variable in 'check'?


    No, error messages are required only when there's a syntax error, a
    constraint violation, or one of the few other explicit requirements for
    a diagnostic applies. None of them apply here.

    In this case the behavior of the code is simply undefined, which means
    that there's no wrong way for the compiler to handle it. In particular,
    it's permissible for the above code to print "10", and give no
    indication how seriously flawed it is. It's even fairly plausible that
    it might do so: the life time of 'b' is already over by the time that
    this code executes "x = *a", but it's quite likely that the memory 'b'
    was stored in still exists, and still contains the last value that was
    stored in 'b'.
    James Kuyper, Sep 19, 2012
    #3
  4. Joe Pfeiffer Guest

    writes:

    > In the code below:
    >
    > int *check(int a){
    > int b,*s;
    > b=a;
    > s=&b;
    > return s;
    > }
    > int main(){
    > int *a,x;
    > a=check(10);
    > x=*a;
    > printf("%d",x);
    > }
    >
    >
    > shouldn't there be some sort of error (or garbage value) when I access
    > the address of the local variable in 'check'?


    No. There is no requirement for C to check for this sort of bug, and
    typical implementations don't.
    Joe Pfeiffer, Sep 19, 2012
    #4
  5. writes:

    > In the code below:
    >
    > int *check(int a){
    > int b,*s;
    > b=a;
    > s=&b;
    > return s;
    > }
    > int main(){
    > int *a,x;
    > a=check(10);
    > x=*a;
    > printf("%d",x);
    > }
    >
    >
    > shouldn't there be some sort of error (or garbage value) when I access
    > the address of the local variable in 'check'?


    It would be good if there were a diagnostic form the compiler, but it's
    not obliged to do so and, in general, this is a situation that can't be
    checked for. If you simplify your code, you do get a warning from gcc:

    x.c: In function ‘check’:
    x.c:5:6: warning: function returns address of local variable [enabled by default]

    from:

    #include <stdio.h>

    int *check(int a) {
    int b = a;
    return &b;
    }

    int main(void) {
    printf("%d\n", *check(10));
    }

    but as you can see, even assignig that value to another local makes gcc
    miss it.

    BTW, you need to declare printf. And you are getting a "garbage
    value" -- it just happens to be confusing garbage!

    --
    Ben.
    Ben Bacarisse, Sep 19, 2012
    #5
  6. Kaz Kylheku Guest

    On 2012-09-19, Rui Maciel <> wrote:
    > wrote:
    >
    >> In the code below:
    >>
    >> int *check(int a){
    >> int b,*s;
    >> b=a;
    >> s=&b;
    >> return s;
    >> }
    >> int main(){
    >> int *a,x;
    >> a=check(10);
    >> x=*a;
    >> printf("%d",x);
    >> }
    >>
    >>
    >> shouldn't there be some sort of error (or garbage value) when I access the
    >> address of the local variable in 'check'?

    >
    > Your code refers to an object outside of its lifetime, which is a behaviour
    > that was intentionally left undefined in the standard, and regarding which
    > the standard imposes no requirements.
    >
    > Having said that, it would be nice if compilers at least threw a warning
    > about that, but I suspect that catching this sort of problem is no easy
    > task.


    In terms of run-time efficiency, it is proably cheaper just to make it work
    than to catch violations. That is to say, allow these local objects
    to persists while they are reachable.

    The reasoning is that if you want to be able to detect all abuses like
    this at run time, then for each local object whose address might escape
    from the function, you have to record that address in some permanent "bad
    pointer" database, and not reuse that address for any newly created object.

    So that this database doesn't run you out of memory, you will need garbage
    collection to discover those bad pointers that no longer occur in the program's
    state and can be purged.

    So you have GC, and you have costly run-time checks guarding the uses
    of pointers: two sources of performance hits.

    But once you let those object persist without being reused, and you
    have GC, then you just might as well let those pointers be valid
    and dispense with the run-time checks.

    There is nothing "morally" wrong with using an object that was created in a
    function that returned, and decriminalization of activities that are not
    actually wrong is usually cheaper than enforcement. :)
    Kaz Kylheku, Sep 19, 2012
    #6
  7. Ben Bacarisse <> writes:
    [...]
    > BTW, you need to declare printf.


    And to be clear, the way to do that is to add

    #include <stdio.h>

    to the top of your program.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Sep 19, 2012
    #7
  8. writes:
    > In the code below:
    >
    > int *check(int a){
    > int b,*s;
    > b=a;
    > s=&b;
    > return s;
    > }
    > int main(){
    > int *a,x;
    > a=check(10);
    > x=*a;
    > printf("%d",x);
    > }
    >
    >
    > shouldn't there be some sort of error (or garbage value) when I access
    > the address of the local variable in 'check'?


    You *are* getting a garbage value. It just happens to be 10. (At
    least, I presume that's what you're seeing; you didn't actually say.)

    When I compile your program (after making a couple of small fixes) with
    "gcc", I get 10. When I use "gcc -O1", I get -1075723044. When I use
    "gcc -O2" or "gcc -O3", I get 0.

    A few other things:

    You need to add "#include <stdio.h>" to the top of your program so that
    the declaration of printf is visible. (Your compiler *should* have
    warned you about that; if it didn't, find out how to increase its
    warning level.)

    You should have a newline at the end of your output:

    printf("%d\n", x);

    "int main()" should probably be "int main(void)" (this is a very minor
    point).

    As a matter of style, a bit of whitespace would make your code more
    readable.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Sep 19, 2012
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Siva
    Replies:
    1
    Views:
    448
    =?Utf-8?B?UmljaA==?=
    Apr 17, 2006
  2. Xpyder
    Replies:
    2
    Views:
    432
    Xpyder
    Feb 7, 2005
  3. Replies:
    4
    Views:
    702
    David Hilsee
    Sep 1, 2005
  4. Jorgen Bodde

    function in a function accessing vars

    Jorgen Bodde, Jun 6, 2007, in forum: Python
    Replies:
    3
    Views:
    275
    Dustan
    Jun 6, 2007
  5. Nathan Harmston
    Replies:
    6
    Views:
    247
    Marcin Ciura
    Jun 24, 2007
Loading...

Share This Page