tag:blogger.com,1999:blog-13960885.post730828019468810473..comments2024-03-26T02:03:01.072-07:00Comments on C++ Truths: const overload functions taking char pointersSumanthttp://www.blogger.com/profile/11957855386259543653noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-13960885.post-86847341802137340572012-09-02T03:20:43.296-07:002012-09-02T03:20:43.296-07:00Some people still need old compilers. I do.Some people still need old compilers. I do.Sumanthttps://www.blogger.com/profile/11957855386259543653noreply@blogger.comtag:blogger.com,1999:blog-13960885.post-67357343892691733122012-08-29T00:15:44.890-07:002012-08-29T00:15:44.890-07:00Of course with modern compilers you **will** get a...Of course with modern compilers you **will** get a warning:<br /><br />test.cpp|16 col 17| warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]<br /><br />so this advice is a bit obsoleteAmberhttps://www.blogger.com/profile/02588145544781882509noreply@blogger.comtag:blogger.com,1999:blog-13960885.post-65811108713306566812007-07-27T07:32:00.000-07:002007-07-27T07:32:00.000-07:00@maximWhen you declare a function taking a non-con...@maxim<BR/>When you declare a function taking a non-const char pointer, you are also declaring your intention to potentially change the input parameter. If you really are sure that you would never change the input parameter string use const char pointer instead. Now assuming that you do intend to change the input parameter string, you declared it as a non-const char pointer and someone out there passed in a string literal to it. That is a problem. Your function will try to change the string literal which is undefined by standard. To avoid this from happening you can take some help from the compiler. If you just declare a function of the same name and rest of the signature, taking const char pointer as input parameter, compiler will pick that up when someone out there uses a string literal to call it. const char pointer is a better match than non-const char pointer for a string literal. But because it is undefined (remember you just declared it) program won't link.Sumanthttps://www.blogger.com/profile/11957855386259543653noreply@blogger.comtag:blogger.com,1999:blog-13960885.post-4242614880223453362007-07-27T01:41:00.000-07:002007-07-27T01:41:00.000-07:00Sorry, I did not understand how the fact that a st...Sorry, I did not understand how the fact that a string literal converts to char* infers that one needs two overloads for char* and char const*?Maxim Yegorushkinhttps://www.blogger.com/profile/13335665761944189051noreply@blogger.comtag:blogger.com,1999:blog-13960885.post-79722880399364716712007-07-25T00:01:00.000-07:002007-07-25T00:01:00.000-07:00What you ascribe to the uniqueness of the literals...What you ascribe to the uniqueness of the literals is only a part answer.It would crash even if there was only one non const char pointer to a literal and you attempted to modify the literal's value. The Standard defines it to be an Undefined behaviour and hence *anything* might happen. Some compilers have the option of making literals writable (Gcc is a case in point).<BR/><BR/>The rational behind such a decision can be attributed to a number of reasons. One example is, as you've said, the literals being in the DATA segment -- modifying the same is tantamount to creating a self-modifying program.Suman Karhttps://www.blogger.com/profile/07834375818278006770noreply@blogger.comtag:blogger.com,1999:blog-13960885.post-55875905685546922432007-07-18T19:32:00.000-07:002007-07-18T19:32:00.000-07:00"Literal String 1" is the same for all the char po..."Literal String 1" is the same for all the char pointers, const or non-const. The compiler detects uniqueness of the initializer strings and creates exactly one copy of it in the DATA pages of the process, which are read only. So const and non-const both point to the same const readonly string and therefore an attempt to modify it seg-faults. I think irrespective to what a string literal is assigned, a string literal will always be in the DATA page readonly.Sumanthttps://www.blogger.com/profile/11957855386259543653noreply@blogger.comtag:blogger.com,1999:blog-13960885.post-42846823941073215182007-07-17T21:15:00.000-07:002007-07-17T21:15:00.000-07:00Hi there,Could you explain why the *c1 = 'l' segfa...Hi there,<BR/><BR/><BR/>Could you explain why the *c1 = 'l' segfaults under Linux? To me it looks like modifying a proper (non const) string?<BR/><BR/><BR/>Thanks!Stevenhttps://www.blogger.com/profile/10209488772548276335noreply@blogger.com