RE: [ROOT] compiler warning

From: Valeri Fine (fine@bnl.gov)
Date: Mon Jun 07 2004 - 22:56:22 MEST


Hello Christian,

  You STILL have a problem
  The pointer your new operator returns

  char *opt  = new char[csize];

  and the pointer you want to use to delete that array MUST match

 However your statement 

>        opt = strtok(NULL, ":");

does change the pointer "opt" and by this reason you can not make free
the memory allocated with the "new"
 

You can have avoid that if you use your "opt", "opt1", opt2" solely to
create and delete. Use other variables for all internal operations and
you will be safe (may be :-)

Of course you may research some  other ways like the STL "string" class
or Qt QString::section(. . .) to do what you want
http://doc.trolltech.com/3.3/qstring.html#section )

  Best regards, Valeri

> Dear all
> 
> Thank you all for giving me some more information.
> 
> Sorrowly, Victor´s statement is really bad news.
> Is the following code allowed in other compilers:
>     const Int_t kBufSize = 512; //in global space
>     char option[kBufSize];  // in different functions
> 
> To reply to Valeri:
> Suppose, I have the following code with the input:
>     "options = "myopt1:myopt2"
> 
>     MyFunction(const char *option)
>     {
>        Int_t csize = strlen(options) + 1;
>        char *opt  = new char[csize];
>        char *opt1 = new char[csize];
>        char *opt2 = new char[csize];
> 
>        opt = strtok(strcpy(opt, options),":");
>        opt1 = strcpy(opt1, opt);
> 
>        opt = strtok(NULL, ":");
>        opt2 = strcpy(opt2, opt);
> 
>        myClass->MyMethod(opt1, opt2)
>        {
>           fString1 = opt1;   //TString fString1;
>           fString2 = opt2;   //TString fString2;
>        }
> 
>        delete [] opt2;
>        delete [] opt1;
>        delete [] opt;
>     }
> Is this code safe as long as I use TString and not char?
> 
> BTW, this is the reason, why I always hated C which I
> have never used, and why I try to use the TString class
> whenever possible, :-)
> 
> Best regards
> Christian
> 
> 
> Victor Perevoztchikov wrote:
> 
> > In addition to Valery, construction
> >
> >>>    char option[csize];
> >
> > allowed only in gcc.
> >
> > Victor
> >
> > Victor M. Perevoztchikov   perev@bnl.gov
> > Brookhaven National Laboratory MS 510A PO Box 5000 Upton NY
11973-5000
> > tel office : 631-344-7894; fax 631-344-4206;
> >
> > ----- Original Message -----
> > From: "Valeri Fine" <fine@bnl.gov>
> > To: "'cstrato'" <cstrato@aon.at>; "'Elias Athanasopoulos'"
> > <elathan@phys.uoa.gr>
> > Cc: <roottalk@pcroot.cern.ch>
> > Sent: Sunday, June 06, 2004 5:47 PM
> > Subject: RE: [ROOT] compiler warning
> >
> >
> >
> >>>Dear Elias, dear Valeri
> >>>
> >>>Thank you for your explanation.
> >>>I have now solved the problem by using:
> >>>    Int_t csize = strlen(opt) + 1;
> >>>    char option[csize];
> >>>
> >>
> >>[Valeri Fine]
> >> Let me call your attention that your solution is still error prone
> >>
> >> You allocated the needed space on stack.
> >> This means the space will be automatically de-allocated and (it can
be
> >>allocated for other purpose) as soon as your code leaves the
"option"
> >>variable scope. Because you did not provide your source I have no
clue
> >>what this scope is. May be it is what you wanted . . .
> >>
> >>
> >>>I hope that this does not only work with gcc but also with
> >>>other compilers.
> >>
> >>[Valeri Fine]
> >>I hope thee smart compiler will warn as soon as you try using this
> >>variable out of scope, however the smart guy always can defeat any
> >>compiler :-)
> >>
> >>Hope this helps.
> >>
> >>
> >>>Best regards
> >>>Christian
> >>>
> >>>
> >>>Valeri Fine wrote:
> >>>
> >>>
> >>>>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