Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

strcat
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;
}

Report this thread to moderator Post Follow-up to this message
Old Post
Eric Kaplan
04-03-08 12:37 AM


Re: strcat
Eric 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.

Report this thread to moderator Post Follow-up to this message
Old Post
Ian Collins
04-03-08 12:37 AM


Re: strcat
"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?




Report this thread to moderator Post Follow-up to this message
Old Post
Sharad
04-03-08 12:37 AM


Re: strcat
On 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


Report this thread to moderator Post Follow-up to this message
Old Post
acehreli@gmail.com
04-03-08 12:37 AM


Re: strcat
> 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.


Report this thread to moderator Post Follow-up to this message
Old Post
utab
04-03-08 12:37 AM


Re: strcat
"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



Report this thread to moderator Post Follow-up to this message
Old Post
Sharad
04-03-08 12:37 AM


Re: strcat
 
>
> 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


Report this thread to moderator Post Follow-up to this message
Old Post
utab
04-03-08 12:37 AM


Re: strcat
On 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

Report this thread to moderator Post Follow-up to this message
Old Post
acehreli@gmail.com
04-03-08 12:37 AM


Re: strcat
On 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

Report this thread to moderator Post Follow-up to this message
Old Post
acehreli@gmail.com
04-03-08 12:37 AM


Re: strcat
<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



Report this thread to moderator Post Follow-up to this message
Old Post
Sharad
04-03-08 12:37 AM


Sponsored Links




Last Thread Next Thread Next
Pages (2): [1] 2 »
Search this forum -> 
Post New Thread

C++ archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:44 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.