Pointers and functions

Discussion in 'C++' started by Doug Haber, Sep 14, 2006.

  1. Doug Haber

    Doug Haber Guest

    Hi All,

    The following code make sense to me:

    void x() {
    int *a = NULL;
    int b;
    b = 4;
    a = &b;
    printf("a: %d",*a); // prints "a: 4"
    }

    Can someone please explain why this blows up?

    void x() {
    int *a = NULL;
    y(a);
    printf("a: %d",a); // Dies
    }

    void y(int* i){
    int b;
    b = 4;
    i = &b;
    }

    Thanks again for the help!
    Doug
     
    Doug Haber, Sep 14, 2006
    #1
    1. Advertising

  2. Doug Haber wrote:

    [...]

    > Can someone please explain why this blows up?
    >
    > void x() {
    > int *a = NULL;
    > y(a);
    > printf("a: %d",a); // Dies
    > }
    >
    > void y(int* i){
    > int b;
    > b = 4;
    > i = &b;
    > }


    It doesn't blow up. What made you think it should? Are you sure the code
    you posted is the one on which you ran your tests? To begin with, x()
    and y() should to be swapped or a forward declaration of y() introduced.
    Please, try to post minimal, compilable code; that will make it easier
    for people to assist you.

    Regards,

    --
    Ney André de Mello Zunino
     
    =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=, Sep 14, 2006
    #2
    1. Advertising

  3. Doug Haber wrote:
    > Hi All,
    >
    > The following code make sense to me:
    >
    > void x() {

    Doug Haber wrote:
    > Hi All,
    >
    > The following code make sense to me:
    >
    > void x() {
    > int *a = NULL;
    > int b;
    > b = 4;
    > a = &b;
    > printf("a: %d",*a); // prints "a: 4"
    > }
    >
    > Can someone please explain why this blows up?
    >
    > void x() {
    > int *a = NULL;
    > y(a);
    > printf("a: %d",a); // Dies
    > }
    >
    > void y(int* i){
    > int b;
    > b = 4;
    > i = &b;
    > }
    >


    I am sure the function y() will blow up. I will lead to run time
    exception.

    reason is simple. you have declared a local varialble b in function
    y().
    Variable b exists in function y()'s scope only.
    You are trying to refer the address of b outside the function. which
    will not exists outside y().
    after call to y(a), a points to b of function y().
    This causes the exception.

    -- Murali Krishna
    > int *a = NULL;
    > int b;
    > b = 4;
    > a = &b;
    > printf("a: %d",*a); // prints "a: 4"
    > }
    >
    > Can someone please explain why this blows up?
    >
    > void x() {
    > int *a = NULL;
    > y(a);
    > printf("a: %d",a); // Dies
    > }
    >
    > void y(int* i){
    > int b;
    > b = 4;
    > i = &b;
    > }
    >
    > Thanks again for the help!
    > Doug
     
    Murali Krishna, Sep 14, 2006
    #3
  4. Doug Haber wrote:
    > Hi All,
    >
    > The following code make sense to me:
    >
    > void x() {
    > int *a = NULL;
    > int b;
    > b = 4;
    > a = &b;
    > printf("a: %d",*a); // prints "a: 4"
    > }
    >
    > Can someone please explain why this blows up?
    >
    > void x() {
    > int *a = NULL;
    > y(a);
    > printf("a: %d",a); // Dies
    > }
    >
    > void y(int* i){
    > int b;
    > b = 4;
    > i = &b;
    > }
    >


    I am sure the function y() will blow up. It will lead to run time
    exception.

    reason is simple. you have declared a local varialble b in function
    y().
    Variable b exists in function y()'s scope only.
    You are trying to refer the address of b outside the function. which
    will not exists outside y().
    after call to y(a), a points to b of function y().
    This causes the exception.

    -- Murali Krishna
     
    Murali Krishna, Sep 14, 2006
    #4
  5. Doug Haber

    Guest

    Ney André de Mello Zunino wrote:

    > Doug Haber wrote:
    >
    > [...]
    >
    >> Can someone please explain why this blows up?
    >>
    >> void x() {
    >> int *a = NULL;
    >> y(a);
    >> printf("a: %d",a); // Dies
    >> }
    >>
    >> void y(int* i){
    >> int b;
    >> b = 4;
    >> i = &b;
    >> }

    >
    >
    > It doesn't blow up. What made you think it should? Are you sure the
    > code you posted is the one on which you ran your tests? To begin with,
    > x() and y() should to be swapped or a forward declaration of y()
    > introduced. Please, try to post minimal, compilable code; that will
    > make it easier for people to assist you.
    >
    > Regards,
    >

    1 - In y(...) you assign to i &b which is a local variable that only
    exist during y(...) life.
    2- Your printf prints incorrect value because you want to print a and
    not *a.
    3- Furthemore you will have crash if you write printf like this:
    printf("a: %d",*a);

    1 bad possible solution
    void y(int* i){
    static int b;
    b = 4;
    i = &b;
    }

    Stef
     
    , Sep 14, 2006
    #5
  6. Murali Krishna schrieb:
    > Doug Haber wrote:
    >> Hi All,
    >>
    >> The following code make sense to me:
    >>
    >> void x() {

    > Doug Haber wrote:
    >> Hi All,
    >>
    >> The following code make sense to me:
    >>
    >> void x() {
    >> int *a = NULL;
    >> int b;
    >> b = 4;
    >> a = &b;
    >> printf("a: %d",*a); // prints "a: 4"
    >> }
    >>
    >> Can someone please explain why this blows up?
    >>
    >> void x() {
    >> int *a = NULL;
    >> y(a);
    >> printf("a: %d",a); // Dies
    >> }
    >>
    >> void y(int* i){
    >> int b;
    >> b = 4;
    >> i = &b;
    >> }
    >>

    >
    > I am sure the function y() will blow up. I will lead to run time
    > exception.
    >
    > reason is simple. you have declared a local varialble b in function
    > y().
    > Variable b exists in function y()'s scope only.
    > You are trying to refer the address of b outside the function. which
    > will not exists outside y().
    > after call to y(a), a points to b of function y().
    > This causes the exception.
    >


    Additionally, a will still be nullptr after y(a), because the integer
    pointer is passed by value, not by reference, therefore x() never sees
    the value &b;

    To the OP:
    If you really want to access variables of local visibility through a
    returned reference (which is actually one quite convenient way for
    implementing a singleton), the locally visible variable must be static.

    For instance:

    MyClass* getSingletonInstance()
    {
    static MyClass hooray; // the static makes the difference
    return &hooray;
    }

    void theCaller()
    {
    MyClass* iWantTheInstance= getSingletonInstance();
    }

    Now you can safely access static MyClass hooray from within theCaller.


    > -- Murali Krishna
    >> int *a = NULL;
    >> int b;
    >> b = 4;
    >> a = &b;
    >> printf("a: %d",*a); // prints "a: 4"
    >> }
    >>
    >> Can someone please explain why this blows up?
    >>
    >> void x() {
    >> int *a = NULL;
    >> y(a);
    >> printf("a: %d",a); // Dies
    >> }
    >>
    >> void y(int* i){
    >> int b;
    >> b = 4;
    >> i = &b;
    >> }
    >>
    >> Thanks again for the help!
    >> Doug

    >
     
    Markus Grueneis, Sep 14, 2006
    #6
  7. Doug Haber

    Doug Haber Guest

    Hi All,

    Sorry for the confusing post. Here is code that compiles:

    void doIt(int* x){
    int b = 2;
    x = &b;
    }

    int _tmain(int argc, _TCHAR* argv[])
    {
    int* a = NULL;
    doIt(a);
    printf("a = %d", *a); // blows up
    return 0;
    }

    I think I have two issues:
    1. int b in doIt() goes out of scope when the function returns, so its
    address is no longer valid when we get back to _tmain(). However even
    if I rewrite doIt() as:

    void doIt(int* x){
    x = new int;
    *x = 2;
    }
    It still fails which brings me to:
    2. I think what I really want is for doIt() to take an int**. I think
    the reason is that if it takes int *, it's actually getting a copy of
    the pointer in _tmain, so allocating space and assigning a value to
    this new pointer doesn't do me any good. However, if I rewrite the
    program as below it works. Does this make sense?

    void doIt(int** x){
    *x = new int;
    int b = 2;
    **x = b;
    }

    int _tmain(int argc, _TCHAR* argv[])
    {
    int* a = NULL;
    doIt(&a);
    printf("a = %d", *a); // prints "a = 2"
    return 0;
    }

    Finally, the below also works, but I think it's *bad* because I think I
    shouldn't use an address from another function. Do you agree?

    void doIt(int** x){
    *x = new int;
    int b = 2;
    *x = &b;
    }


    Thanks again!
    Doug


    Doug Haber wrote:
    > Hi All,
    >
    > The following code make sense to me:
    >
    > void x() {
    > int *a = NULL;
    > int b;
    > b = 4;
    > a = &b;
    > printf("a: %d",*a); // prints "a: 4"
    > }
    >
    > Can someone please explain why this blows up?
    >
    > void x() {
    > int *a = NULL;
    > y(a);
    > printf("a: %d",a); // Dies
    > }
    >
    > void y(int* i){
    > int b;
    > b = 4;
    > i = &b;
    > }
    >
    > Thanks again for the help!
    > Doug
     
    Doug Haber, Sep 14, 2006
    #7
  8. First, let us not top-post in this group. Luckly I never got a warning
    regarding this till now. :)

    Doug Haber wrote:
    > Hi All,
    >
    > Sorry for the confusing post. Here is code that compiles:
    >
    > void doIt(int* x){
    > int b = 2;
    > x = &b;
    > }
    >
    > int _tmain(int argc, _TCHAR* argv[])
    > {
    > int* a = NULL;
    > doIt(a);
    > printf("a = %d", *a); // blows up
    > return 0;
    > }


    > I think I have two issues:
    > 1. int b in doIt() goes out of scope when the function returns, so its
    > address is no longer valid when we get back to _tmain().


    OK, we have already discussed about local varialbles. We should not
    return the address of the local variable. because it dies after the
    function execution. but a doesn't get the address of b. it still is
    zero.

    > However even
    > if I rewrite doIt() as:
    >
    > void doIt(int* x){
    > x = new int;
    > *x = 2;
    > }
    > It still fails which brings me to:
    > 2. I think what I really want is for doIt() to take an int**. I think
    > the reason is that if it takes int *, it's actually getting a copy of
    > the pointer in _tmain, so allocating space and assigning a value to
    > this new pointer doesn't do me any good.


    you are almost right. In main, we have declared pointer a and assigned
    it to NULL (zero).
    a's work is to point to some address and a is also created with some
    address (&a).

    so &a will have some address (system created)
    our a points to zero. we assigned.

    now in function doIt(int *x), x takes zero. not &a. and ofcourse x will
    have it's own address (&x), that is no where related to &a. now &x and
    &a are different.

    in x = new int; x gets a value which is no where related to a. So when
    doIt() completes it's execution, a will still point to zero. That is
    why it fails.

    > However, if I rewrite the
    > program as below it works. Does this make sense?
    >
    > void doIt(int** x){
    > *x = new int;
    > int b = 2;
    > **x = b;
    > }
    >
    > int _tmain(int argc, _TCHAR* argv[])
    > {
    > int* a = NULL;
    > doIt(&a);
    > printf("a = %d", *a); // prints "a = 2"
    > return 0;
    > }


    it makes sense. it should take the address of a, where a is a pointer
    so we have to take pointer to pointer.
    with the above explanation again. "new int" allocates memory. The point
    is where it is allocating memory. For sure, it is allocating memory for
    a. It is not local to function doIt().

    > Finally, the below also works, but I think it's *bad* because I think I
    > shouldn't use an address from another function. Do you agree?
    >
    > void doIt(int** x){
    > *x = new int;
    > int b = 2;
    > *x = &b;
    > }


    It worked because you are not manupulating any data in &b after the
    function execution. but are you getting 2 in the result?
    in my knowledge the results are undefined.
    This is program logic error.

    HTH.

    -- Murali Krishna.
     
    Murali Krishna, Sep 15, 2006
    #8
  9. Murali Krishna wrote:
    >> Can someone please explain why this blows up?
    >>
    >> void x() {
    >> int *a = NULL;
    >> y(a);
    >> printf("a: %d",a); // Dies
    >> }
    >>
    >> void y(int* i){
    >> int b;
    >> b = 4;
    >> i = &b;
    >> }
    >>

    >
    > I am sure the function y() will blow up. I will lead to run time
    > exception.


    Not really. Function y() is well defined yet somewhat useless since it
    has no observable effect.

    > reason is simple. you have declared a local varialble b in function
    > y().
    > Variable b exists in function y()'s scope only.
    > You are trying to refer the address of b outside the function. which
    > will not exists outside y().
    > after call to y(a), a points to b of function y().
    > This causes the exception.


    What you describe would be true if argument i would be of type int *&.

    --
    Markus
     
    Markus Schoder, Sep 15, 2006
    #9
    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. S?ren Gammelmark
    Replies:
    1
    Views:
    1,898
    Eric Sosman
    Jan 7, 2005
  2. Marc Thrun
    Replies:
    15
    Views:
    863
    Tim Rentsch
    Oct 4, 2005
  3. newbie

    Pointers to char pointers in functions

    newbie, Sep 18, 2006, in forum: C Programming
    Replies:
    9
    Views:
    308
    August Karlstrom
    Sep 24, 2006
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    683
  5. ajaybgr
    Replies:
    18
    Views:
    984
    Philip Lantz
    Sep 7, 2012
Loading...

Share This Page