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