473,326 Members | 2,182 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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;
}
Apr 2 '08 #1
12 2346
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.
Apr 2 '08 #2

"Eric Kaplan" <to*********@yahoo.comwrote in message
news:u0********************************@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?

Apr 2 '08 #3

<ac******@gmail.comwrote in message
On Apr 2, 4:01 pm, Eric Kaplan <tobycraf...@yahoo.comwrote:
>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;

}
<snip>
2) String literals are constant objects, which means that the
characters that they contain may not be modified. The fact that it is
IIRC, this statement is not true. String literals as defined in the C
Standard are not defined as constant. Yet, modifying them leads to undefined
behavior. This is what I recollect from C89 (I have to admit that it's been
quite some time, so I may be wrong). Most of the Unix compilers place string
literals in a read only region, so modifying them gets a segmention
violation there.

Sharad
Apr 2 '08 #4
Eric Kaplan wrote:
why the following code will crash?

int main()
{
char* str = "aa";
This is a deprecated conversion from a 3 element array of const char
(holding the values a a and \0) to pointer to (single) character.
strcat (str, "hello");
This attempts to write hello on to the end of the chars starting
at str. The h is written into read only storage, the ello\0 is
written outside of any allocated memory.
cout << str << "\n";
strcat (str, "you1");
cout << str << "\n";
\
}
char* is not a string type.
strcat doesn't allocate any memory. You are expected to have enough
allocated memory in the bytes pointed to by the first argument to
hold the concatenation of both strings.

This is C++ and you shouldn't be playing with C's old and busted
string functions but the C++ new hotness string type.

std::string str = "aa";
str += "hello";
cout << str << "\n"

see.
Apr 2 '08 #5
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.

Apr 2 '08 #6

"utab" <um********@gmail.comwrote in message
news:e8**********************************@e6g2000p rf.googlegroups.com...
>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.
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
Apr 2 '08 #7
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.
Ah yes, using "const" becomes an insurance in this case. That is good
practice.
>
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
Apr 3 '08 #8
On Apr 2, 4:41 pm, "Sharad" <sharadk.nospam_...@yahoo.comwrote:
<acehr...@gmail.comwrote in message
On Apr 2, 4:01 pm, Eric Kaplan <tobycraf...@yahoo.comwrote:
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;
}

<snip>
2) String literals are constant objects, which means that the
characters that they contain may not be modified. The fact that it is

IIRC, this statement is not true. String literals as defined in the C
Standard are not defined as constant. Yet, modifying them leads to undefined
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
Apr 3 '08 #9

<ac******@gmail.comwrote in message
On Apr 2, 4:41 pm, "Sharad" <sharadk.nospam_...@yahoo.comwrote:
><acehr...@gmail.comwrote in message
On Apr 2, 4:01 pm, Eric Kaplan <tobycraf...@yahoo.comwrote:
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;
>}

<snip>
2) String literals are constant objects, which means that the
characters that they contain may not be modified. The fact that it is

IIRC, this statement is not true. String literals as defined in the C
Standard are not defined as constant. Yet, modifying them leads to
undefined
behavior.

So they are constant.
I don't think so. I will need to go back to the C Standard to verify that.
>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. :)
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.
>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. :)
Read above.

Sharad
Apr 3 '08 #10
Sharad wrote:
<ac******@gmail.comwrote:
>So they are constant.

I don't think so. I will need to go back to the C Standard to verify that.
2.13.4 of the C++ standard states that a string literal has type "array
of n const char".

--
Ian Collins.
Apr 3 '08 #11

"Ian Collins" <ia******@hotmail.comwrote in message
news:65*************@mid.individual.net...
Sharad wrote:
><ac******@gmail.comwrote:
>>So they are constant.

I don't think so. I will need to go back to the C Standard to verify
that.
2.13.4 of the C++ standard states that a string literal has type "array
of n const char".
OK thanks!
Apr 3 '08 #12
In article <e8**********************************@e6g2000prf.g ooglegroups.com>,
utab <um********@gmail.comwrote:
>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.
There won't be any segfault because the code won't compile.

Apr 3 '08 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Patrick Coleman | last post by:
Hi, I have the following code: char request = "GET "; strcat(request, path); //Path is the path section of a url ie. "/path/test.htm" strcat(request, " HTTP/1.1"); cout<<request<<"\n";...
9
by: Pascal Damian | last post by:
I read somewhere that strcpy() is safer when dealing with malloc()-ed strings. Is that true? (Of course I know that both are unsafe). -- Pascal
5
by: Kevin C. | last post by:
Never mind my last post about pointer subtraction, I traced the code and found the offender to be strcat. char call = "del "; system(strcat(strcat(call, del->table_name), ".tab")); After this...
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
8
by: ctara_shafa | last post by:
Hi, I have a following problem: I'm creating a list and one of the fields should contain the date. Firstly I ask the user for the year, month and day and then I'd like to collect all this data in...
9
by: cicalese | last post by:
For starters, I am a C novice. I don't fully understand how strings work. I am running on a linux box. I compile w/ gcc. I have a string called 'myabqcommand' that is being overwritten when i use...
87
by: Robert Seacord | last post by:
The SEI has published CMU/SEI-2006-TR-006 "Specifications for Managed Strings" and released a "proof-of-concept" implementation of the managed string library. The specification, source code for...
4
by: nick048 | last post by:
Hi, I have this problem: int n; char nToChar; char firstString = "The number is: "; char resp; /* HERE MAIN WITH THE INPUT OF n*/
3
by: sail0r | last post by:
Perhaps this is obvious but I am not sure what is going on... Here is the relevant code: char *command; char *argument; char url="file:///usr/u/myname/Project/cats/"; char...
28
by: Mahesh | last post by:
Hi, I am looking for efficient string cancatination c code. I did it using ptr but my code i think is not efficient. Please help. Thanks a lot
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.