RE: [ROOT] compiler warning

From: Valeri Fine (fine@bnl.gov)
Date: Sun Jun 06 2004 - 20:23:23 MEST


Hello Christian


> 
> Dear Rooters
> 
> I apologize for a simple non-root question, but sorrowly I have
> nobody else to ask:
> Sometimes I have statements such as the following:
>     if (strcmp(name, "option") == 0) {
>        char *option;
>        option = strcpy(option, opt);
>     }
> 
> Although it is correct, I get the following compiler warning:
> "warning: `char*option' might be used uninitialized in this function"

[Valeri Fine] It looks like the compiler pointed you to your bug to be
fixed. That must lead to the memory corruption. "strcpy"  alone
allocates NO extra memory.

 
> How can I prevent such compiler warnings?
[Valeri Fine] 
> Would it be save to write:
>        char *option = "";
> (If this is not save, why?)
[Valeri Fine] 
Not it is NOT safe !!!

To avoid the memory corruption you need

  char *option = new char[strlen(opt)+1];

 or something like this to make sure the memory the POINTER "option"
points to is allocated and it is enough to accommodate the entire string
pointed by "opt" pointer.

  Hope this helps.

   Valeri
> 
> Thank you in advance
> Best regards
> Christian
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> C.h.r.i.s.t.i.a.n. .S.t.r.a.t.o.w.a
> V.i.e.n.n.a.         .A.u.s.t.r.i.a
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-



This archive was generated by hypermail 2b29 : Sun Jan 02 2005 - 05:50:08 MET