strcat (run time error)

S

santosh

The following code is giving run-time error.....

char buf[100] = "Hello" ;
strcat(buf,buf) ;

Second line is giving the run-time error.
Any comments please............
 
N

Nelu

santosh said:
The following code is giving run-time error.....

char buf[100] = "Hello" ;
strcat(buf,buf) ;

Second line is giving the run-time error.
Any comments please............

To concatenate two strings you have to find the end of the first string
and copy each character in the second string into the first until you
reach
the end of that string.
Now, the initial string: "Hello" is:
|H|e|l|l|o|\0|
What you do is go the end and overwrite \0 with the first element from
the
second argument until you get to \0 in the second string. Unfortunately
you
are modifying the string and you will never reach 0. The error appears
when you go beyond the automatically allocated memory:
HelloHelloHello..... until you go beyond the 100 characters. You will
never
reach \0 as you've overwritten it already. The function never stops.
Well,
it stops when it runs out of the allocated memory (100 chars).
 
S

stathis gotsis

santosh said:
The following code is giving run-time error.....

char buf[100] = "Hello" ;
strcat(buf,buf) ;

Second line is giving the run-time error.
Any comments please............

When using strcat(char *dest,const char *src), dest and src may not overlap.
 
O

Old Wolf

santosh said:
char buf[100] = "Hello" ;
strcat(buf,buf) ;

Second line is giving the run-time error.

What were you expecting?What were you expecting?
What were you expecting?What were you expecting?
What were you expecting?What were you expecting?
What were y+++NO CARRIER
 
B

Barry Schwarz

The following code is giving run-time error.....

char buf[100] = "Hello" ;
strcat(buf,buf) ;

Second line is giving the run-time error.
Any comments please............

When you use strcat, you are obligated to insure that the area from
which data is being extracted does not overlap the area into which
data is being deposited. Your code invokes undefined behavior. Be
thankful it resulted in an error as opposed to something pernicious.


Remove del for email
 
C

CBFalconer

Barry said:
santosh said:
The following code is giving run-time error.....

char buf[100] = "Hello" ;
strcat(buf,buf) ;

Second line is giving the run-time error.
Any comments please............

When you use strcat, you are obligated to insure that the area from
which data is being extracted does not overlap the area into which
data is being deposited. Your code invokes undefined behavior. Be
thankful it resulted in an error as opposed to something pernicious.

The way to avoid errors with strcpy and strcat is to not use them.
There is a (non-standard for now) replacement set called strlcpy
and strlcat, which do intelligent things and return intelligent
information. They came out of the FreeBSD operation, I believe.
At any rate an implementation in standard C, with suitable
references, is readily available at:

<http://cbfalconer.home.att.net/download/strlcpy.zip>

--
"The power of the Executive to cast a man into prison without
formulating any charge known to the law, and particularly to
deny him the judgement of his peers, is in the highest degree
odious and is the foundation of all totalitarian government
whether Nazi or Communist." -- W. Churchill, Nov 21, 1943
 
D

Dave Thompson

santosh said:
char buf[100] = "Hello" ;
strcat(buf,buf) ;

Second line is giving the run-time error.

What were you expecting?What were you expecting?
What were you expecting?What were you expecting?
What were you expecting?What were you expecting?
What were y+++NO CARRIER

<OT>
With (directly interfaced) Hayes-style modems, three plus signs are
_sent_ to enter command mode, which prompts with OK. For older
(non-error-controlled) modulations the modem will usually 'receive'
garbage characters before detecting carrier loss but the probability
of exactly three plus signs and nothing else is vanishingly small.
</>

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,241
Latest member
Lisa1997

Latest Threads

Top