PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > Avoiding used uninitialized in this function warning
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Avoiding used uninitialized in this function warning

Réponse
 
LinkBack Outils de la discussion
Vieux 25/05/2008, 12h47   #1
markryde@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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 *myStructElement)

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("myStruct->val=%d\n",(*myStructArray)->val );
}

void f1(struct myStruct *myStructElement)
{
struct myStruct **myStructArray;
*myStructArray=myStructElement;
f2(myStructArray);
}


int main(int argc, char** argv)
{
struct myStruct *mystruct = (struct
myStruct*)malloc(sizeof(*mystruct));
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





  Réponse avec citation
Vieux 25/05/2008, 13h30   #2
Jens Thoms Toerring
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Avoiding used uninitialized in this function warning

markryde@gmail.com <markryde@gmail.com> wrote:
> 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 *myStructElement)


> 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("myStruct->val=%d\n",(*myStructArray)->val );
> }


> void f1(struct myStruct *myStructElement)
> {
> 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 = &myStructElement;

> f2(myStructArray);
> }


You can simplify this function by just writing

void f1( struct myStruct *myStructElement )
{
f2( &myStructElement );
}

> int main(int argc, char** argv)
> {
> struct myStruct *mystruct = (struct myStruct*)malloc(sizeof(*mystruct));


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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h55.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 2,12390 seconds with 10 queries