Basic Pointer Question...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shan_rish@yahoo.com

    Basic Pointer Question...

    Hi CLCers,
    I coded a function to allocate memory and i am passing a pointer to the
    function. The code is compiling but throws error and closes while
    executing. The program is as below:
    #include<stdio. h>
    #include<stdlib .h>
    int main()
    {
    int *p;
    void mem_fun(int *i);
    mem_fun(p);
    printf("%d\n",* p);
    getch();
    return 0;
    }

    void mem_fun(int *i)
    {

    printf("%u\n",i );
    i=malloc(sizeof (int));
    *i=10;
    printf("%d\n",* i);
    }

    Please help in understanding the problem. Advanced thanks.
    Cheers
    Shan

  • bwaichu@yahoo.com

    #2
    Re: Basic Pointer Question...


    shan_r...@yahoo .com wrote:[color=blue]
    > Hi CLCers,
    > I coded a function to allocate memory and i am passing a pointer to the
    > function. The code is compiling but throws error and closes while
    > executing. The program is as below:
    > #include<stdio. h>
    > #include<stdlib .h>
    > int main()
    > {
    > int *p;
    > void mem_fun(int *i);
    > mem_fun(p);
    > printf("%d\n",* p);
    > getch();
    > return 0;
    > }
    >
    > void mem_fun(int *i)
    > {
    >
    > printf("%u\n",i );
    > i=malloc(sizeof (int));
    > *i=10;
    > printf("%d\n",* i);
    > }
    >
    > Please help in understanding the problem. Advanced thanks.
    > Cheers
    > Shan[/color]

    You are not using pointers correctly. Here's a good website with an
    easy to understand explanation of pointers:



    I removed your first printf from the mem_fun() since I had no idea what
    you were trying to do.

    Here's your code corrected:

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

    void mem_fun(int **);

    int
    main() {

    int *p;
    mem_fun(&p);
    (void)printf("% d\n",*p);
    exit(0);
    }

    void
    mem_fun(int **i)
    {

    *i = malloc(sizeof(i nt));
    if(*i == NULL)
    return;
    **i = 10;
    (void)printf("% d\n",**i);
    return;
    }

    Comment

    • Michael Mair

      #3
      Re: Basic Pointer Question...

      shan_rish@yahoo .com wrote:[color=blue]
      > Hi CLCers,
      > I coded a function to allocate memory and i am passing a pointer to the
      > function. The code is compiling but throws error and closes while
      > executing. The program is as below:
      > #include<stdio. h>
      > #include<stdlib .h>
      > int main()
      > {
      > int *p;
      > void mem_fun(int *i);[/color]

      Do not hide your function declarations within other functions.
      [color=blue]
      > mem_fun(p);[/color]

      Let us recapitulate: C passes arguments by value.
      You have given no value to p, so making a copy of p for mem_fun
      invokes undefined behaviour. From now on, anything can happen,
      including your harddisk being formatted.
      If you want to change something using a function, you have
      to pass its _address_, not the object itself (which will only
      copied but not changed).
      Note: The address is also passed by value, but this value is
      sufficient to do something.
      [color=blue]
      > printf("%d\n",* p);
      > getch();[/color]

      This function is not defined in standard C.
      [color=blue]
      > return 0;
      > }
      >
      > void mem_fun(int *i)
      > {
      >
      > printf("%u\n",i );[/color]

      The format specifier for pointers is %p.
      If pointers and integers have different sizes, the above may
      go wrong.
      [color=blue]
      > i=malloc(sizeof (int));[/color]

      Better:
      i = malloc(sizeof *i);
      This works even if you change the type to i.

      You forgot to check whether malloc() succeeded.
      [color=blue]
      > *i=10;
      > printf("%d\n",* i);[/color]

      As you work on a copy of a pointer value which expires at the
      end of the function, you have produced a memory leak.
      [color=blue]
      > }
      >
      > Please help in understanding the problem. Advanced thanks.[/color]

      Better:

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

      void mem_fun (int **i);

      int main (void)
      {
      int *p = 0;
      mem_fun(&p);
      if (p)
      printf("%p : %d\n", p, *p);
      free(p);
      getch();
      return 0;
      }

      void mem_fun (int **i)
      {
      *i = malloc(sizeof **i);
      if (*i)
      **i = 10;
      else {
      fprintf(stderr, "mem alloc trouble\n");
      exit(EXIT_FAILU RE); /* Replace by _real_ error
      ** handling whenever possible */
      }
      }

      As you see, I check _twice_ whether malloc() succeeded to be
      on the safe side. This is because mem_fun() does not directly
      tell me whether it was successful. You may consider returning
      *i/int * to signal success or failure.
      In addition, as malloc() is hidden within a function, free()
      can be easily forgotten.
      It is usually better to malloc() on the same "level" as you free()
      or to provide companion functions to be called from the same
      level performing malloc() and free().


      Cheers
      Michael
      --
      E-Mail: Mine is an /at/ gmx /dot/ de address.

      Comment

      • shan_rish@yahoo.com

        #4
        Re: Basic Pointer Question...

        Hi Guys,
        Thanks for your reply. Now i understand what the problem is. When
        coding with pointers, i stumble a lot, even though i know that values
        are passed to C functions. Thanks again for your time for clearing my
        doubt.
        Cheers
        Shan

        Comment

        • Krishanu Debnath

          #5
          Re: Basic Pointer Question...


          Michael Mair wrote:

          <snip>
          [color=blue]
          > Better:
          >
          > #include<stdio. h>
          > #include<stdlib .h>
          >
          > void mem_fun (int **i);
          >
          > int main (void)
          > {
          > int *p = 0;
          > mem_fun(&p);
          > if (p)
          > printf("%p : %d\n", p, *p);[/color]

          typo? printf("%p : %d\n",(void *) p, *p);

          Krishanu

          Comment

          • kernelxu@hotmail.com

            #6
            Re: Basic Pointer Question...

            >Better:
            [color=blue]
            >#include<stdio .h>
            >#include<stdli b.h>[/color]

            [color=blue]
            >void mem_fun (int **i);[/color]

            [color=blue]
            >int main (void)
            >{
            > int *p = 0;[/color]
            hi,Michael
            maybe "int *p = (void *)0; " is better than "int *p = 0; ".
            and, I always use NULL to initilize a pointer. do you think it's a good
            style?
            Is the macro "NULL" a standard C's definition?
            any word would be appreciated.[color=blue]
            > mem_fun(&p);
            > if (p)
            > printf("%p : %d\n", p, *p);
            > free(p);
            > getch();
            > return 0;[/color]



            }

            Comment

            • Krishanu Debnath

              #7
              Re: Basic Pointer Question...


              kernelxu@hotmai l.com wrote:[color=blue][color=green]
              > >Better:[/color]
              >[color=green]
              > >#include<stdio .h>
              > >#include<stdli b.h>[/color]
              >
              >[color=green]
              > >void mem_fun (int **i);[/color]
              >
              >[color=green]
              > >int main (void)
              > >{
              > > int *p = 0;[/color]
              > hi,Michael
              > maybe "int *p = (void *)0; " is better than "int *p = 0; ".[/color]

              No, both are same.
              [color=blue]
              > and, I always use NULL to initilize a pointer. do you think it's a good
              > style?
              > Is the macro "NULL" a standard C's definition?
              > any word would be appreciated.[/color]

              Did you read FAQs?



              Krishanu

              --
              "Be nice to nerds. Chances are you'll end up working for one."
              --Bill Gates

              Comment

              • Peter Pichler

                #8
                Re: Basic Pointer Question...

                kernelxu@hotmai l.com wrote:
                [color=blue]
                > maybe "int *p = (void *)0; " is better than "int *p = 0; ".
                > and, I always use NULL to initilize a pointer. do you think it's a good
                > style?
                > Is the macro "NULL" a standard C's definition?
                > any word would be appreciated.[/color]

                Look up Q 5.4 in the FAQ. I would also recommend to read the whole
                section 5, which deals with NULL pointers. The FAQ can be found at
                http://www.eskimo.com/~scs/C-faq/top.html. It used to be anyway; I
                couldn't connect to that site today :(

                Peter

                Comment

                • Michael Mair

                  #9
                  Re: Basic Pointer Question...

                  Krishanu Debnath wrote:[color=blue]
                  > Michael Mair wrote:
                  >
                  > <snip>
                  >[color=green]
                  >>Better:
                  >>
                  >>#include<stdi o.h>
                  >>#include<stdl ib.h>
                  >>
                  >>void mem_fun (int **i);
                  >>
                  >>int main (void)
                  >>{
                  >> int *p = 0;
                  >> mem_fun(&p);
                  >> if (p)
                  >> printf("%p : %d\n", p, *p);[/color]
                  >
                  >
                  > typo? printf("%p : %d\n",(void *) p, *p);[/color]

                  True. Thanks.

                  Cheers
                  Michael
                  --
                  E-Mail: Mine is an /at/ gmx /dot/ de address.

                  Comment

                  • ranjeet.gupta@gmail.com

                    #10
                    Re: Basic Pointer Question...

                    [color=blue]
                    > Here's your code corrected:[/color]

                    I dont think so !!![color=blue]
                    >
                    > #include <stdio.h>
                    > #include <stdlib.h>
                    >
                    > void mem_fun(int **);
                    >
                    > int
                    > main() {
                    >
                    > int *p;
                    > mem_fun(&p);
                    > (void)printf("% d\n",*p);
                    > exit(0);
                    > }
                    >
                    > void
                    > mem_fun(int **i)
                    > {
                    >
                    > *i = malloc(sizeof(i nt));
                    > if(*i == NULL)
                    > return;
                    > **i = 10;
                    > (void)printf("% d\n",**i);
                    > return;[/color]
                    what you are returning and why ? Check your declaration.[color=blue]
                    > }[/color]

                    Comment

                    Working...