Avoiding used uninitialized in this function warning

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

    Avoiding used uninitialized in this function warning

    Hello,

    I am working on upgrading an existent project.

    I have a method with this the following prototype:

    void f2(struct myStruct **myStructArray )

    Now, I need to call a method with the following prototype
    whenever f2 is called:
    void f1(struct myStruct *myStructElemen t)

    Though f2 get an array, in fact it is **always** called with one
    element.
    This is a working assumption on which I can base my work.
    (though in the beginning of this project it was meant to enable
    calling this f2 with an array with more than one element).

    What I tried is this:

    #include <stdio.h>

    struct myStruct
    {
    int val;
    };

    void f2(struct myStruct **myStructArray )
    {
    printf("myStruc t->val=%d\n",(*my StructArray)->val );
    }

    void f1(struct myStruct *myStructElemen t)
    {
    struct myStruct **myStructArray ;
    *myStructArray= myStructElement ;
    f2(myStructArra y);
    }


    int main(int argc, char** argv)
    {
    struct myStruct *mystruct = (struct
    myStruct*)mallo c(sizeof(*mystr uct));
    mystruct->val=1;
    f1(mystruct);

    return 0;
    }


    Now, when building with -Wall -O2
    I get:
    main.c: In function ‘f1’:
    main.c:19: warning: ‘myStructArray’ is used uninitialized in this
    function

    (without this flags, compilation completes with no warnings).

    How can I avoid this warning messages when building with
    -Wall -O2 ? any ideas?

    Regards,
    Mark





  • Jens Thoms Toerring

    #2
    Re: Avoiding used uninitialized in this function warning

    markryde@gmail. com <markryde@gmail .comwrote:
    Hello,
    I am working on upgrading an existent project.
    I have a method with this the following prototype:
    void f2(struct myStruct **myStructArray )
    Now, I need to call a method with the following prototype
    whenever f2 is called:
    void f1(struct myStruct *myStructElemen t)
    Though f2 get an array, in fact it is **always** called with one
    element.
    This is a working assumption on which I can base my work.
    (though in the beginning of this project it was meant to enable
    calling this f2 with an array with more than one element).
    What I tried is this:
    #include <stdio.h>
    struct myStruct
    {
    int val;
    };
    void f2(struct myStruct **myStructArray )
    {
    printf("myStruc t->val=%d\n",(*my StructArray)->val );
    }
    void f1(struct myStruct *myStructElemen t)
    {
    struct myStruct **myStructArray ;
    'myStructArray' is a pointer, pointing nowhere useful.
    *myStructArray= myStructElement ;
    Now you try to write somethig to the location 'myStructArray'
    points to. But since you haven't set where 'myStructArray' is
    supposed to point to it's uninitialized and it points to no
    place in memory you would be allowed to write to.

    I guess what you meant to write is

    myStrArray = &myStructElemen t;
    f2(myStructArra y);
    }
    You can simplify this function by just writing

    void f1( struct myStruct *myStructElemen t )
    {
    f2( &myStructElemen t );
    }
    int main(int argc, char** argv)
    {
    struct myStruct *mystruct = (struct myStruct*)mallo c(sizeof(*mystr uct));
    You shouldn't cast the return value of malloc(). I guess you put
    it there since you forgot to include <stdlib.h(that' s where
    malloc() is declared and it's not included in the code you posted)
    and then got a compiler warning. The cast can actually be dangerous
    if you're using a machine where a pointer doesn't fit into an int
    or which has dedicated address and data registers since without
    a prototype for malloc() the compiler will assume that it returns
    an int.
    mystruct->val=1;
    f1(mystruct);
    return 0;
    }
    Now, when building with -Wall -O2
    You probably also should add '-W' to the mix (despite the name
    '-Wall' does not include all warnings that get produced by '-W').

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ jt@toerring.de
    \______________ ____________ http://toerring.de

    Comment

    Working...