Pointer assigned by a function problem...

Discussion in 'C Programming' started by jamaj, Sep 3, 2008.

  1. jamaj

    jamaj Guest

    #include <stdlib.h>
    #include <stdio.h>

    void foo(double *ptr)
    {
    printf("foo ptr=%p\n",ptr);
    ptr = malloc(sizeof(*ptr));
    printf("foo ptr=%p\n",ptr);
    ptr[0] = 12;
    printf("foo value=%f\n",*ptr);
    }


    int main(int argc, char **argv)
    {
    double *ptr=NULL;
    printf("main ptr=%p\n",ptr);
    foo(ptr);
    printf("main ptr=%p\n",ptr);
    }


    Returns:

    main ptr=(nil)
    foo ptr=(nil)
    foo ptr=0x804a008
    foo value=12.000000
    main ptr=(nil)

    Why the pointer in the main function is not updated?

    Thnaks in advance.

    jamaj
     
    jamaj, Sep 3, 2008
    #1
    1. Advertising

  2. jamaj

    Ian Collins Guest

    jamaj wrote:
    > #include <stdlib.h>
    > #include <stdio.h>
    >
    > void foo(double *ptr)
    > {
    > printf("foo ptr=%p\n",ptr);
    > ptr = malloc(sizeof(*ptr));
    > printf("foo ptr=%p\n",ptr);
    > ptr[0] = 12;
    > printf("foo value=%f\n",*ptr);
    > }
    >
    >
    > int main(int argc, char **argv)
    > {
    > double *ptr=NULL;
    > printf("main ptr=%p\n",ptr);
    > foo(ptr);
    > printf("main ptr=%p\n",ptr);
    > }
    >
    >
    > Returns:
    >
    > main ptr=(nil)
    > foo ptr=(nil)
    > foo ptr=0x804a008
    > foo value=12.000000
    > main ptr=(nil)
    >
    > Why the pointer in the main function is not updated?
    >

    Because you are updating the local copy in foo. ptr in foo is copy of
    the one in main.

    You have to write

    void foo(double **ptr)
    {
    *ptr = malloc(sizeof(*ptr));
    }

    --
    Ian Collins.
     
    Ian Collins, Sep 3, 2008
    #2
    1. Advertising

  3. jamaj

    jamaj Guest

    I have altered for this version, and it worked. But is this the unique
    and right way?

    void foo(double **ptr)
    {
    printf("foo ptr=%p\n",*ptr);
    *ptr = malloc(sizeof(**ptr));
    printf("foo ptr=%p\n",*ptr);
    *ptr[0] = 12;
    printf("foo value=%f\n",**ptr);
    }
    int main(int argc, char **argv)
    {
    double *ptr=NULL;
    printf("main ptr=%p\n",ptr);
    foo(&ptr);
    printf("main ptr=%p\n",ptr);
    printf("main value=%f\n",*ptr);
    }

    Now the results are:

    main ptr=(nil)
    foo ptr=(nil)
    foo ptr=0x804a008
    foo value=12.000000
    main ptr=0x804a008
    main value=12.000000


    On 2 set, 21:53, jamaj <> wrote:
    > #include <stdlib.h>
    > #include <stdio.h>
    >
    > void foo(double *ptr)
    > {
    >         printf("foo ptr=%p\n",ptr);
    >         ptr = malloc(sizeof(*ptr));
    >         printf("foo ptr=%p\n",ptr);
    >         ptr[0] = 12;
    >         printf("foo value=%f\n",*ptr);
    >
    > }
    >
    > int main(int argc, char **argv)
    > {
    >         double *ptr=NULL;
    >         printf("main ptr=%p\n",ptr);
    >         foo(ptr);
    >         printf("main ptr=%p\n",ptr);
    >
    > }
    >
    > Returns:
    >
    > main ptr=(nil)
    > foo ptr=(nil)
    > foo ptr=0x804a008
    > foo value=12.000000
    > main ptr=(nil)
    >
    > Why the pointer in the main function is not updated?
    >
    > Thnaks in advance.
    >
    > jamaj
     
    jamaj, Sep 3, 2008
    #3
  4. jamaj

    Ian Collins Guest

    jamaj wrote:

    [please don't top-post]

    > I have altered for this version, and it worked. But is this the unique
    > and right way?
    >

    Yes.

    > void foo(double **ptr)
    > {
    > printf("foo ptr=%p\n",*ptr);
    > *ptr = malloc(sizeof(**ptr));
    > printf("foo ptr=%p\n",*ptr);
    > *ptr[0] = 12;
    > printf("foo value=%f\n",**ptr);
    > }


    --
    Ian Collins.
     
    Ian Collins, Sep 3, 2008
    #4
  5. jamaj

    Guest

    On Sep 3, 4:02 am, jamaj <> wrote:
    > I have altered for this version, and it worked. But is this the unique
    > and right way?
    >
    > void foo(double **ptr)
    > {
    > printf("foo ptr=%p\n",*ptr);


    *ptr is of type (double *). `p' expects (void *).
    Change to:

    printf("foo ptr=%p\n", (void *)*ptr);

    > *ptr = malloc(sizeof(**ptr));


    Don't forget that it's possible for malloc to return NULL.

    > printf("foo ptr=%p\n",*ptr);


    Same here; *ptr is (double *), `p' expects (void *).

    > *ptr[0] = 12;


    So here you have a problem, since you don't check if *p is indeed non-
    null, and you just dereference it.
    Change this to:

    if(*ptr == NULL) return;
    *ptr[0] = 12;

    > printf("foo value=%f\n",**ptr);}
    >
    > int main(int argc, char **argv)
    > {
    > double *ptr=NULL;
    > printf("main ptr=%p\n",ptr);


    And here

    > foo(&ptr);
    > printf("main ptr=%p\n",ptr);


    and here.

    > printf("main value=%f\n",*ptr);
    >
    > }
    >
    > Now the results are:
    >
    > main ptr=(nil)
    > foo ptr=(nil)
    > foo ptr=0x804a008
    > foo value=12.000000
    > main ptr=0x804a008
    > main value=12.000000


    Please don't top post.
    http://www.catb.org/jargon/html/T/top-post.html
    http://www.cs.tut.fi/~jkorpela/usenet/brox.html
    http://www.caliburn.nl/topposting.html
    http://www.html-faq.com/etiquette/?toppost
    etc.
     
    , Sep 3, 2008
    #5
  6. jamaj

    Guest

    On Sep 3, 4:11 am, wrote:
    > On Sep 3, 4:02 am, jamaj <> wrote:


    <snip>

    Ah yes, also there's a memory leak, since you never free *p.
    So add a free(*p); after the last printf, and then add a return 0;
     
    , Sep 3, 2008
    #6
  7. jamaj

    jamaj Guest

    jamaj, Sep 3, 2008
    #7
  8. jamaj

    jamaj Guest

    On 2 set, 22:19, wrote:
    > On Sep 3, 4:11 am, wrote:
    >
    > > On Sep 3, 4:02 am, jamaj <> wrote:

    >
    > <snip>


    > Ah yes, also there's a memory leak, since you never free *p.
    > So add a free(*p); after the last printf, and then add a return 0;


    I think that you already know that these little snippets of code have
    only an illustrative purpose, and they did the work - with or without
    the memory leaks that you correctly showed. So you can rest, cause my
    doubt was kindly removed by Ian Collins.

    Thanks again.

    jamaj
     
    jamaj, Sep 3, 2008
    #8
  9. jamaj

    Guest

    On Sep 3, 4:26 am, jamaj <> wrote:
    > On 2 set, 22:19, wrote:
    >
    > > On Sep 3, 4:11 am, wrote:

    >
    > > > On Sep 3, 4:02 am, jamaj <> wrote:

    >
    > > <snip>
    > > Ah yes, also there's a memory leak, since you never free *p.
    > > So add a free(*p); after the last printf, and then add a return 0;

    >
    > I think that you already know that these little snippets of code have
    > only an illustrative purpose, and they did the work


    No they did not.

    > - with or without
    > the memory leaks that you correctly showed. So you can rest, cause my
    > doubt was kindly removed by Ian Collins.
     
    , Sep 3, 2008
    #9
  10. jamaj

    Guest

    On Sep 3, 4:20 am, jamaj <> wrote:
    > > Please don't top post.

    >
    > Ok. I won't do it anymore.


    Thanks, now please leave the attributes untouched.

    (original message was by me)
     
    , Sep 3, 2008
    #10
  11. jamaj

    jamaj Guest

    On 2 set, 22:31, wrote:
    > On Sep 3, 4:26 am, jamaj <> wrote:
    >
    > > On 2 set, 22:19, wrote:

    >
    > > > On Sep 3, 4:11 am, wrote:

    >
    > > > > On Sep 3, 4:02 am, jamaj <> wrote:

    >
    >
    > No they did not.
    >


    But they are working in my machine!!! Maybe a bug of gcc... I'm
    wasting my time with you. Thanks a lot...

    The real program i'm building is now working, and what is important
    for me.

    Thanks Ian Collins...
    Regards,

    jamaj
     
    jamaj, Sep 3, 2008
    #11
  12. On Tue, 2 Sep 2008 18:19:06 -0700 (PDT), wrote:

    >On Sep 3, 4:11 am, wrote:
    >> On Sep 3, 4:02 am, jamaj <> wrote:

    >
    ><snip>
    >
    >Ah yes, also there's a memory leak, since you never free *p.
    >So add a free(*p); after the last printf, and then add a return 0;


    In main, *p is a double. You meant
    free(p);

    --
    Remove del for email
     
    Barry Schwarz, Sep 3, 2008
    #12
  13. On Tue, 2 Sep 2008 18:49:09 -0700 (PDT), jamaj <>
    wrote:

    >On 2 set, 22:31, wrote:
    >> On Sep 3, 4:26 am, jamaj <> wrote:
    >>
    >> > On 2 set, 22:19, wrote:

    >>
    >> > > On Sep 3, 4:11 am, wrote:

    >>
    >> > > > On Sep 3, 4:02 am, jamaj <> wrote:

    >>
    >>
    >> No they did not.
    >>

    >
    >But they are working in my machine!!! Maybe a bug of gcc... I'm
    >wasting my time with you. Thanks a lot...
    >


    It is possible you are wasting your time but if so it is because you
    are learning bad habits and rejecting efforts to correct them.

    >The real program i'm building is now working, and what is important
    >for me.


    Refusing to correct undefined behavior just because the program
    appears to work is a recipe for disaster.

    --
    Remove del for email
     
    Barry Schwarz, Sep 3, 2008
    #13
  14. jamaj

    Guest

    On Sep 3, 6:35 am, Barry Schwarz <> wrote:
    > On Tue, 2 Sep 2008 18:19:06 -0700 (PDT), wrote:
    > >On Sep 3, 4:11 am, wrote:
    > >> On Sep 3, 4:02 am, jamaj <> wrote:

    >
    > ><snip>

    >
    > >Ah yes, also there's a memory leak, since you never free *p.
    > >So add a free(*p); after the last printf, and then add a return 0;

    >
    > In main, *p is a double. You meant
    > free(p);


    Right, thanks for that correction.
     
    , Sep 3, 2008
    #14
    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. =?Utf-8?B?dnZlbms=?=
    Replies:
    1
    Views:
    454
  2. Replies:
    2
    Views:
    475
  3. Vijai Kalyan
    Replies:
    4
    Views:
    315
    Jim Langston
    Sep 7, 2005
  4. Replies:
    8
    Views:
    508
    Gavin Deane
    Apr 19, 2006
  5. George Oliver
    Replies:
    12
    Views:
    523
    George Oliver
    May 7, 2009
Loading...

Share This Page