Can't figure this out

Discussion in 'C Programming' started by ritesh, Apr 7, 2006.

  1. ritesh

    ritesh Guest

    Hi,

    I'm unable to figure out why initializing a pointer dosen't work this
    way -

    void func(int * x)
    {
    x = (int*)(malloc(sizeof(int*)));
    *x = 2;
    }

    int main(void)
    {
    int * a = 0;
    func(a);
    printf(" value of a = %d" , *a); /* getting SEGV fault here */
    }

    buf works this way -

    void func(int ** x)
    {
    *x = (int*)(malloc(sizeof(int*)));
    **x = 2;
    }

    int main(void)
    {
    int * a = 0;
    func(&a);
    printf(" value of a = %d" , **a); /* Prints 2 */
    }


    Could someone please explain this in detail.

    Shouldn't the first code example work fine
    1. I'm passing a pointer to a function
    2. which intializes it
    3. and assigns a value to the location pointed to by this pointer
    4. Running this in a debugger shows that in main() 'a' hasn't been
    intialized even after the call to func()

    The second example works fine.

    Thanks,
    Ritesh
    ritesh, Apr 7, 2006
    #1
    1. Advertising

  2. ritesh

    ciju Guest

    int * a = 0;
    func(a);

    Here u r passing a value of '0' to the func().

    void func(int * x)
    {
    x = (int*)(malloc(sizeof(int*)));
    *x = 2;
    }

    Now x will be having a value '0'. ( Usual call by value mechanism ).
    Then u r allocating memory to x. Now x will contain an address returned
    by malloc. So the value of the location pointed by 'a' won't be
    updated.
    Again in main u r trying to print '*a' , which is *(0)

    The second example is correct.
    ciju, Apr 7, 2006
    #2
    1. Advertising

  3. ritesh

    suresh Guest

    ritesh wrote:
    > Hi,
    >
    > I'm unable to figure out why initializing a pointer dosen't work this
    > way -
    >
    > void func(int * x)
    > {
    > x = (int*)(malloc(sizeof(int*)));
    > *x = 2;
    > }
    >
    > int main(void)
    > {
    > int * a = 0;
    > func(a);
    > printf(" value of a = %d" , *a); /* getting SEGV fault here */
    > }
    >


    This does not work for the same reason why the swap function below does
    not work.

    void swap(int x, int y)
    {
    int t = x;
    x = y;
    y = t;
    }


    Technically speaking there is no pass-by-reference in C but only
    pass-by-value. Only the value of the expression is passed to the
    function as an argument (as apposed to passing the actual object's
    reference)

    The modifications you do to the argument inside the function only
    affects the local variable that initially had the passed function
    argument's value.

    The above explanation holds good even for pointer arguments.
    suresh, Apr 7, 2006
    #3
  4. ritesh

    pete Guest

    ciju wrote:
    >
    > int * a = 0;
    > func(a);
    >
    > Here u r passing a value of '0' to the func().
    >
    > void func(int * x)


    > Now x will be having a value '0'.


    That value is better expressed as NULL, rather than as '0'.

    '0' is equal to ('1' - 1)

    --
    pete
    pete, Apr 7, 2006
    #4
  5. "ritesh" <> writes:
    > I'm unable to figure out why initializing a pointer dosen't work this
    > way -
    >
    > void func(int * x)
    > {
    > x = (int*)(malloc(sizeof(int*)));
    > *x = 2;
    > }
    >
    > int main(void)
    > {
    > int * a = 0;
    > func(a);
    > printf(" value of a = %d" , *a); /* getting SEGV fault here */
    > }
    >
    > buf works this way -
    >

    [snip]
    >
    > Could someone please explain this in detail.
    >
    > Shouldn't the first code example work fine
    > 1. I'm passing a pointer to a function
    > 2. which intializes it
    > 3. and assigns a value to the location pointed to by this pointer
    > 4. Running this in a debugger shows that in main() 'a' hasn't been
    > intialized even after the call to func()


    You need #include directives for <stdio.h> (for printf) and <stdlib.h>
    (for malloc).

    Don't cast the result of malloc; it can hide errors (such as failing
    to #include <stdlib.h>). malloc() returns void*, which is implicitly
    converted to the target type.

    x need to point to an int, but you allocate enough space to hold an
    int*. A good idiom to avoid this kind of problem is:

    x = malloc(sizeof *x);

    You pass an int* value, a, to func. Inside func, the value is copied
    to x, which is local to func (that's how parameter passing works in
    general, for any type). You then write over that value by assigning a
    new value to it. When you leave func, you lose the only pointer to
    the memory you allocated, creating a memory leak.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Apr 7, 2006
    #5
  6. ritesh

    pemo Guest

    ritesh wrote:
    > Hi,
    >
    > I'm unable to figure out why initializing a pointer dosen't work this
    > way -
    >
    > void func(int * x)
    > {
    > x = (int*)(malloc(sizeof(int*)));
    > *x = 2;
    > }
    >
    > int main(void)
    > {
    > int * a = 0;
    > func(a);
    > printf(" value of a = %d" , *a); /* getting SEGV fault here */
    > }
    >
    > buf works this way -
    >
    > void func(int ** x)
    > {
    > *x = (int*)(malloc(sizeof(int*)));
    > **x = 2;
    > }
    >
    > int main(void)
    > {
    > int * a = 0;
    > func(&a);
    > printf(" value of a = %d" , **a); /* Prints 2 */
    > }
    >
    >
    > Could someone please explain this in detail.
    >
    > Shouldn't the first code example work fine
    > 1. I'm passing a pointer to a function
    > 2. which intializes it
    > 3. and assigns a value to the location pointed to by this pointer
    > 4. Running this in a debugger shows that in main() 'a' hasn't been
    > intialized even after the call to func()
    >
    > The second example works fine.
    >
    > Thanks,
    > Ritesh


    Here is what I believe you want - albeit with some diagnostic /clutter/

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

    void func(int ** x)
    {
    puts("\nIn func()");

    printf("\tx's *value* is %p, *x's value is %p\n", (void *)x, (void
    *)*x);

    puts("\tmalloc()");

    *x = malloc(sizeof(int)); // replaced sizeof(int *)

    printf("\tx's *value* is %p, *x's value is %p\n", (void *)x, (void
    *)*x);

    printf("\t**x's *value* is %d\n", **x);

    **x = 2;

    printf("\t**x's *value* is %d\n", **x);

    puts("Returning from func()\n");
    }


    int main(void)
    {
    int * a = NULL;

    printf("main()\n\ta's address is %p, its value is %p\n", (void *)&a,
    (void *)a);

    func(&a);

    printf("main()\n\ta's address is %p, its value is %p\n", (void *)&a,
    (void *)a);

    printf("\tcontents of *a is %d\n" , *a);

    free(a);

    return 0;
    }

    --
    ==============
    *Not a pedant*
    ==============
    pemo, Apr 7, 2006
    #6
  7. ritesh wrote:
    >
    > Hi,
    >
    > I'm unable to figure out why initializing a pointer dosen't work this
    > way -

    [... calling func(int *) ...]
    > int * a = 0;
    > func(a);

    [...]
    > buf works this way -

    [... calling func(int **) ...]
    > int * a = 0;
    > func(&a);

    [...]
    > Could someone please explain this in detail.


    Simple:

    C passes parameters by value.

    Check your books to see how this differs from "pass by reference".

    --
    +-------------------------+--------------------+-----------------------------+
    | Kenneth J. Brody | www.hvcomputer.com | |
    | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------------+
    Don't e-mail me at: <mailto:>
    Kenneth Brody, Apr 7, 2006
    #7
  8. ritesh

    Default User Guest

    ritesh wrote:

    > Hi,
    >
    > I'm unable to figure out why initializing a pointer dosen't work this
    > way -
    >
    > void func(int * x)
    > {
    > x = (int*)(malloc(sizeof(int*)));
    > *x = 2;
    > }
    >
    > int main(void)
    > {
    > int * a = 0;
    > func(a);
    > printf(" value of a = %d" , *a); /* getting SEGV fault here */
    > }
    >
    > buf works this way -
    >
    > void func(int ** x)
    > {
    > *x = (int*)(malloc(sizeof(int*)));
    > **x = 2;
    > }


    http://c-faq.com/ptrs/passptrinit.html




    Brian
    --
    Please quote enough of the previous message for context. To do so from
    Google, click "show options" and use the Reply shown in the expanded
    header.
    Default User, Apr 7, 2006
    #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. logia
    Replies:
    6
    Views:
    651
  2. Chad A. Beckner

    Please Help! Can't figure this out...

    Chad A. Beckner, Jun 18, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    398
    bloomfield
    Jun 21, 2004
  3. myleslawrence

    Simple but I can't figure it out

    myleslawrence, Jan 19, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    319
    IPGrunt
    Jan 19, 2005
  4. Wayne  Wengert

    Can't Figure Out What is Wrong

    Wayne Wengert, Feb 27, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    447
    Michael H
    Feb 28, 2005
  5. Albert Hopkins
    Replies:
    3
    Views:
    469
    Steven D'Aprano
    Dec 10, 2008
Loading...

Share This Page