Thomas Hruska a écrit : > David wrote: > >> Thomas Hruska a écrit : >> >>> kou ksk wrote: >>> >>> >>>>> Why are you bothering to even manipulate the string? On many compilers, >>>>> the code will "crash" (GPF) because you will be attempting to modify >>>>> read-only memory. >>>>> >>>>> >>>> Thomas, >>>> I am afraid that this is not at all true for this code and in widely used >>>> compilers including >>>> gcc, borland. What you are saying is right for : >>>> >>>> char *p="hello"; >>>> p[1]='2'; //this will crash on gcc >>>> >>>> but, as in Anurag's code, >>>> char p[]="hello"; >>>> p[1]='2'; //this will not at all crash !!!! >>>> >>>> -kou. >>>> >>>> >>> Tempting fate is always a bad idea. From my perspective, both are >>> identical and interchangeable. An overzealous optimizing compiler (e.g. >>> Intel*) may think the same way. >>> >>> * Intel's optimizing compiler suite couldn't/can't be used, for >>> instance, to compile the Linux kernel. (Or at least it used to not be >>> able to because it was overzealous in its optimizations - at least that >>> was the explanation I received). >>> >>> >>> >> I don't know much C, >> But can you clarified this : "both are identical" ? >> I am not sure to understand. >> >> I don't have intel icc compiler to see assembly generated by both code But >> it could be interested to see what will be generated by (two initialization >> with the same literal c-string) >> >> char* q = "abcdef"; // could be (as with aCC) a warning for C++ >> char* p = "abcdef"; >> >> And >> >> char q[] = "abcdef"; >> char p[] = "abcdef"; >> >> David >> > > I don't have access to the Intel compiler (but I'd love to try it > sometime and see if it is everything it claims to be). > > My guess (without actually trying it) is the assembler generated for the > above is most likely _identical_. The difference is going to be where > in the final executable the compiler decides to dump the strings > (read-only or read/write memory). > >
The difference, at least, gcc generate a loop for char[] and only an affectation for char* Where the litteral is put (read only or not) is not the same problem. Another problem is : is the litteral is share between p and q or not ? (the better thing to do is probably to avoid intel compiler) Most compilers do not report a warning on char* p = "sdijso"; only not to have nuisance on a non-const C library. aCC is one I know that emit a warning.
