error when allocating memory using a pointer inside a struct via a function

Discussion in 'C Programming' started by elmafiacs@yahoo.es, Feb 22, 2005.

  1. Guest

    I need some help with an error I'm getting...
    Sorry if this is basic stuff, but I'm still a newbie on C programming
    (working up, though).
    Also, I'm using gcc as my compiler ('gcc t.c -o t', no strange stuff).
    Sorry about the long subject, I didn't know how to explain it better...

    struct small {
    int num;
    };

    struct big {
    struct small *small_thing;
    };

    void f(struct small **s)
    {
    s = malloc(sizeof(struct small));
    memset(s, 0, sizeof(struct small));
    s->num = 42;
    }

    main()
    {
    struct big *big_thing;

    big_thing = malloc(sizeof(struct big));
    memset(&big_thing, 0, sizeof(struct big));

    f(&big_thing->small_thing);
    printf("%i\n", big_thing->small_thing->n);
    }

    But when I do:
    s->num = 42;
    the compiler fails with error 'request for member 'num' in something
    not an structure or union', and I don't know why.

    I tried this way:

    void f(struct small **s)
    {
    struct small *s_ptr;
    s_ptr->num = 42;
    memcpy(s, &s_ptr, sizeof(struct small));
    }

    I'm not sure, but I think that the second function is unnecessary. So,
    how to fix the first function?

    Thanks.
     
    , Feb 22, 2005
    #1
    1. Advertising

  2. Re: error when allocating memory using a pointer inside a structvia a function

    wrote:

    > I need some help with an error I'm getting...
    > Sorry if this is basic stuff, but I'm still a newbie on C programming
    > (working up, though).
    > Also, I'm using gcc as my compiler ('gcc t.c -o t', no strange stuff).
    > Sorry about the long subject, I didn't know how to explain it better...
    >
    > struct small {
    > int num;
    > };
    >
    > struct big {
    > struct small *small_thing;
    > };
    >
    > void f(struct small **s)
    > {
    > s = malloc(sizeof(struct small));
    > memset(s, 0, sizeof(struct small));
    > s->num = 42;
    > }
    >


    s is a pointer to a pointer to a struct small, not a pointer
    to a struct small. Accordingly, you want this:

    *s = malloc(sizeof **s);
    memset(*s, 0, sizeof **s);
    (*s)->num = 42;

    And you should check the return value of malloc...

    -David



    -David
     
    David Resnick, Feb 22, 2005
    #2
    1. Advertising

  3. Re: error when allocating memory using a pointer inside a structvia a function

    wrote:
    > I need some help with an error I'm getting...
    > Sorry if this is basic stuff, but I'm still a newbie on C programming
    > (working up, though).
    > Also, I'm using gcc as my compiler ('gcc t.c -o t', no strange stuff).
    > Sorry about the long subject, I didn't know how to explain it better...
    >
    > struct small {
    > int num;
    > };
    >
    > struct big {
    > struct small *small_thing;
    > };
    >
    > void f(struct small **s)
    > {
    > s = malloc(sizeof(struct small));
    > memset(s, 0, sizeof(struct small));
    > s->num = 42;
    > }
    >
    > main()
    > {
    > struct big *big_thing;
    >
    > big_thing = malloc(sizeof(struct big));
    > memset(&big_thing, 0, sizeof(struct big));
    >
    > f(&big_thing->small_thing);
    > printf("%i\n", big_thing->small_thing->n);
    > }
    >
    > But when I do:
    > s->num = 42;
    > the compiler fails with error 'request for member 'num' in something
    > not an structure or union', and I don't know why.
    >
    > I tried this way:
    >
    > void f(struct small **s)
    > {
    > struct small *s_ptr;
    > s_ptr->num = 42;
    > memcpy(s, &s_ptr, sizeof(struct small));
    > }
    >
    > I'm not sure, but I think that the second function is unnecessary. So,
    > how to fix the first function?
    >
    > Thanks.
    >


    Please don't post code that is not that which you have actually
    compiled. Here is a start toward fixing your code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>

    struct small
    {
    int num;
    };

    struct big
    {
    struct small *small_thing;
    };

    void f(struct small **s)
    {
    *s = malloc(sizeof **s);
    /* add code to check that *s != 0 */
    memset(*s, 0, sizeof **s);
    (*s)->num = 42;
    }

    int main(void)
    {
    struct big *big_thing;

    big_thing = malloc(sizeof *big_thing);
    /* add code to check that big_thing != 0 */
    memset(&big_thing, 0, sizeof big_thing);

    f(&big_thing->small_thing);
    printf("%i\n", big_thing->small_thing->num);
    free(big_thing->small_thing);
    free(big_thing);
    return 0;
    }
     
    Martin Ambuhl, Feb 22, 2005
    #3
  4. Guest

    Thanks for the help.
     
    , Feb 22, 2005
    #4
  5. Al Bowers Guest

    Re: error when allocating memory using a pointer inside a structvia a function

    Martin Ambuhl wrote:

    >
    > Please don't post code that is not that which you have actually
    > compiled. Here is a start toward fixing your code:


    In addition to compiling, run and testing the code before posting can be
    helpful
    (but not final) in catching additional flaws. For example....

    > #include <stdlib.h>
    > #include <stdio.h>
    > #include <string.h>
    >
    > struct small
    > {
    > int num;
    > };
    >
    > struct big
    > {
    > struct small *small_thing;
    > };
    >
    > void f(struct small **s)
    > {
    > *s = malloc(sizeof **s);
    > /* add code to check that *s != 0 */
    > memset(*s, 0, sizeof **s);
    > (*s)->num = 42;
    > }
    >
    > int main(void)
    > {
    > struct big *big_thing;
    >
    > big_thing = malloc(sizeof *big_thing);
    > /* add code to check that big_thing != 0 */
    > memset(&big_thing, 0, sizeof big_thing);
    >

    Running and testing might identify the memset statement flaw
    above. It should be:
    memset(big_thing, 0 , sizeof big_thing);

    And, as you suggested, code to catch dynamic memory allocations
    with function malloc needs to be added to make execution of the code
    safe. See the example below:

    > f(&big_thing->small_thing);
    > printf("%i\n", big_thing->small_thing->num);
    > free(big_thing->small_thing);
    > free(big_thing);
    > return 0;
    > }
    >

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

    struct small {
    int num;
    };

    struct big {
    struct small *small_thing;
    };

    void f(struct small **s)
    {
    *s = malloc(sizeof(struct small));
    if(*s)
    {
    memset(*s, 0, sizeof(struct small));
    (*s)->num = 42;
    }
    }

    int main(void)
    {
    struct big *big_thing;

    big_thing = malloc(sizeof(struct big));
    if(big_thing)
    {
    memset(big_thing, 0, sizeof(struct big));
    f(&big_thing->small_thing);
    if(big_thing->small_thing)
    printf("big_thing->small_thing->num = %i\n",
    big_thing->small_thing->num);
    free(big_thing->small_thing);
    }
    free(big_thing);
    return 0;
    }

    Al Bowers
     
    Al Bowers, Feb 22, 2005
    #5
  6. Old Wolf Guest

    Al Bowers wrote:
    > Martin Ambuhl wrote:
    > >
    > > Please don't post code that is not that which you have actually
    > > compiled. Here is a start toward fixing your code:

    >
    > > struct big *big_thing;
    > >
    > > big_thing = malloc(sizeof *big_thing);
    > >
    > > memset(&big_thing, 0, sizeof big_thing);

    >
    > Running and testing might identify the memset statement flaw
    > above. It should be:
    > memset(big_thing, 0 , sizeof big_thing);
    >


    1) Running and testing isn't a great way of checking for
    undefined behaviour

    2) A statement: memset(foo, bar, sizeof foo); is wrong, unless
    foo and &foo are the same address (ie. for an array).
    It should be:

    memset(big_thing, 0, sizeof *big_thing);

    The OP (who has been snipped) had it correct.
    IMHO, even better would be to use calloc instead of malloc.
     
    Old Wolf, Feb 22, 2005
    #6
  7. Al Bowers Guest

    Re: error when allocating memory using a pointer inside a structvia a function

    Old Wolf wrote:

    >Al Bowers wrote:
    >
    >
    >>Martin Ambuhl wrote:
    >>
    >>
    >>>Please don't post code that is not that which you have actually
    >>>compiled. Here is a start toward fixing your code:
    >>>
    >>>
    >>> struct big *big_thing;
    >>>
    >>> big_thing = malloc(sizeof *big_thing);
    >>>
    >>> memset(&big_thing, 0, sizeof big_thing);
    >>>
    >>>

    >>Running and testing might identify the memset statement flaw
    >>above. It should be:
    >>memset(big_thing, 0 , sizeof big_thing);
    >>
    >>
    >>

    >
    >
    >2) A statement: memset(foo, bar, sizeof foo); is wrong, unless
    >foo and &foo are the same address (ie. for an array).
    >It should be:
    >
    > memset(big_thing, 0, sizeof *big_thing);
    >
    >The OP (who has been snipped) had it correct.
    >
    >
    >

    I don't think so. The op code was:

    > struct big *big_thing;


    > big_thing = malloc(sizeof(struct big));
    > memset(&big_thing, 0, sizeof(struct big));


    Is that correct?

    I believe it should be:
    memset(big_thing, 0, sizeof *big_thing);
    or
    memset(big_thing, 0, sizeof(struct big));
     
    Al Bowers, Feb 22, 2005
    #7
  8. Old Wolf Guest

    Al Bowers wrote:
    > Old Wolf wrote:
    > >
    > >It should be:
    > >
    > > memset(big_thing, 0, sizeof *big_thing);
    > >
    > >The OP (who has been snipped) had it correct.
    > >

    > I don't think so. The op code was:
    >
    > > struct big *big_thing;
    > > big_thing = malloc(sizeof(struct big));
    > > memset(&big_thing, 0, sizeof(struct big));

    >
    > Is that correct?
    >
    > I believe it should be:
    > memset(big_thing, 0, sizeof *big_thing);
    > or
    > memset(big_thing, 0, sizeof(struct big));


    You're right. I think we got there in the end :)
     
    Old Wolf, Feb 23, 2005
    #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. Ole
    Replies:
    4
    Views:
    623
    Michael Wojcik
    Oct 26, 2004
  2. beetle
    Replies:
    2
    Views:
    972
    beetle
    Jan 25, 2005
  3. Rakesh Kumar
    Replies:
    5
    Views:
    695
    James Kanze
    Dec 21, 2007
  4. Adam Baker
    Replies:
    6
    Views:
    556
    Keith Thompson
    Jan 15, 2008
  5. Scott
    Replies:
    1
    Views:
    455
    Scott
    Jun 19, 2009
Loading...

Share This Page