Code Comments
Programming Forum and web based access to our favorite programming groups.why the following code will crash?
int main()
{
char* str = "aa";
strcat (str, "hello");
cout << str << "\n";
strcat (str, "you1");
cout << str << "\n";
return 0;
}
Post Follow-up to this messageEric Kaplan wrote:
> why the following code will crash?
>
Do you mean after it has been fixed to compile?
> int main()
> {
> char* str = "aa";
> strcat (str, "hello");
What is this trying to do? (hint what is str?).
--
Ian Collins.
Post Follow-up to this message
"Eric Kaplan" <tobycraftse@yahoo.com> wrote in message
news:u048v3tmo9qgkkh37qs8ptek9ebd93jr5t@
4ax.com...
> why the following code will crash?
>
> int main()
> {
> char* str = "aa";
Where is str pointing to? It's pointing to a string literal.
> strcat (str, "hello");
Is the location pointed to by str modifiable?
Post Follow-up to this messageOn Apr 2, 4:01 pm, Eric Kaplan <tobycraf...@yahoo.com> wrote:
> why the following code will crash?
>
> int main()
> {
> char* str = "aa";
> strcat (str, "hello");
> cout << str << "\n";
> strcat (str, "you1");
> cout << str << "\n";
>
> return 0;
>
> }
1) When you find the documentation for strcat, you will notice that it
includes some requirements like "The strings may not overlap, and the
dest must have enough space for the result."
In your example, there is no space for the result.
2) String literals are constant objects, which means that the
characters that they contain may not be modified. The fact that it is
possible to initialize pointers to non-const with string literals is
for backward compatibility.
You should not take advantage of that in new code. So instead of
writing
char* str = "aa";
write
const char* str = "aa";
Ali
Post Follow-up to this message> You should not take advantage of that in new code. So instead of > writing > > char* str = "aa"; > > write > > const char* str = "aa"; > > Ali For the code at hand, this does not help to recover the segfault though. Still you will be trying to change a const char array. So why bother, use strings without the implementation details of pointers ;), pointers/arrays are evil :-), as far as possible I refrain to use them.
Post Follow-up to this message"utab" <umut.tabak@gmail.com> wrote in message news:e8a61d54-c9d0-4c4e-9ba5-65c23159ced5@e6g2000prf.googlegroups.com... > > For the code at hand, this does not help to recover the segfault > though. It does in some way. The program won't compile now. > Still you will be trying to change a const char array. So why bother, > use strings without the implementation details of pointers ;), > pointers/arrays are evil :-), as far as possible I refrain to use > them. Yes, in C++ you are better off with std::string and std::vector. Sharad
Post Follow-up to this message> > It does in some way. The program won't compile now. Ah yes, using "const" becomes an insurance in this case. That is good practice. > > > Yes, in C++ you are better off with std::string and std::vector. > > Sharad
Post Follow-up to this messageOn Apr 2, 4:50 pm, utab <umut.ta...@gmail.com> wrote: > > > > > > For the code at hand, this does not help to recover the segfault > though. The OP used 'cout', so I'm assuming this is C++. (Heck, the group is clc++!) So the compiler is required to reject passing str to strcat now. ;) Ali
Post Follow-up to this messageOn Apr 2, 4:41 pm, "Sharad" <sharadk.nospam_...@yahoo.com> wrote: > <acehr...@gmail.com> wrote in message > > > > > <snip> > > IIRC, this statement is not true. String literals as defined in the C > Standard are not defined as constant. Yet, modifying them leads to undefin ed > behavior. So they are constant. > This is what I recollect from C89 (I have to admit that it's been > quite some time, so I may be wrong). Same here, but luckily we are on a C++ forum and discussing a C++ code. :) > Most of the Unix compilers place string > literals in a read only region, so modifying them gets a segmention > violation there. That's because string literals are constant. :) Ali
Post Follow-up to this message<acehreli@gmail.com> wrote in message > On Apr 2, 4:41 pm, "Sharad" <sharadk.nospam_...@yahoo.com> wrote: > > So they are constant. I don't think so. I will need to go back to the C Standard to verify that. > > Same here, but luckily we are on a C++ forum and discussing a C++ > code. :) Nah, C++ has inherited a lot from C. Most C programs are valid C++ programs also. And the behavior we are talking about is the inherited behavior. > > That's because string literals are constant. :) Read above. Sharad
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.