473,396 Members | 1,998 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,396 software developers and data experts.

alleged mis-form with strcat

Jon
using Borland Compiler.

x[32] = "this is a string"

strcat(x,x)
strcat(x,x)

will produce "this is a stringthis is a stringthis is a stringthis is
a stringt"

but should produce "this is a stringthis is a stringthis is a
stringthis is a string"

does anyone know why the additional character from the beginning of
the string is being appended at the end?
in addition does anyone know the source for the strcat function? I am
curious as to how they did it.
Nov 13 '05 #1
6 2436
On 12 Sep 2003 06:44:30 -0700
tf**@hotmail.com (Jon) wrote:
using Borland Compiler.
Doesn't matter, I hope, or this post is off-topic.
x[32] = "this is a string"

strcat(x,x)
strcat(x,x)

will produce "this is a stringthis is a stringthis is a stringthis is
a stringt"

but should produce "this is a stringthis is a stringthis is a
stringthis is a string"

does anyone know why the additional character from the beginning of
the string is being appended at the end?
The arguments to strcat may not overlap. The way you call it leads to undefined
behaviour. Anything could happen.
in addition does anyone know the source for the strcat function? I am
curious as to how they did it.


www.gnu.org. Download the glibc source package. You'll see why the strings may
not overlap.

--
char*x(c,k,s)char*k,*s;{if(!k)return*s-36?x(0,0,s+1):s;if(s)if(*s)c=10+(c?(x(
c,k,0),x(c,k+=*s-c,s+1),*k):(x(*s,k,s+1),0));else c=10;printf(&x(~0,0,k)[c-~-
c+"1"[~c<-c]],c);}main(){x(0,"^[kXc6]dn_eaoh$%c","-34*1'.+(,03#;+,)/'///*");}
Nov 13 '05 #2
Pieter Droogendijk wrote:
tf**@hotmail.com (Jon) wrote:
x[32] = "this is a string"

strcat(x,x)
strcat(x,x)

will produce "this is a stringthis is a stringthis is a stringthis is
a stringt"

but should produce "this is a stringthis is a stringthis is a
stringthis is a string"
.... The arguments to strcat may not overlap. The way you call it leads to undefined
behaviour. Anything could happen.


Furthermore 32 wouldn't be enough, even for *two* copies of "this is a string".

Jirka

Nov 13 '05 #3
In article <2b**************************@posting.google.com >,
tf**@hotmail.com says...
using Borland Compiler.

x[32] = "this is a string"

strcat(x,x)
"this is a string" requires 17 bytes of storage. Making two copies of
it requires 33 bytes of storage, so you have not enough space allocated
even for this first strcat. But worse than that is that the arguments
to strcat may not overlap. On many machines, I would expect this to
continue writing to memory until it crashed.
strcat(x,x)
Now we are trying to fit 65 bytes into 32. But anyway, the overlap
problem still applies.

will produce "this is a stringthis is a stringthis is a stringthis is
a stringt"

but should produce "this is a stringthis is a stringthis is a
stringthis is a string"
No, it should not. See above. You are lucky (some would say unlucky)
that this did not crash your program.

Trevor

does anyone know why the additional character from the beginning of
the string is being appended at the end?
in addition does anyone know the source for the strcat function? I am
curious as to how they did it.

Nov 13 '05 #4
Jon wrote:
using Borland Compiler. x[32] = "this is a string" strcat(x,x)
strcat(x,x) will produce "this is a stringthis is a stringthis is a stringthis is
a stringt"


Using Linux. man 3 strcat says "The strings may not overlap". Your
code has just entered a dimension of incorrectness, a dimension of
undefined behavior. Welcome to the twilight zone ;)

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13
Nov 13 '05 #5
Jon wrote:
using Borland Compiler.

x[32] = "this is a string"

strcat(x,x)
strcat(x,x)

will produce "this is a stringthis is a stringthis is a stringthis is
a stringt"

but should produce "this is a stringthis is a stringthis is a
stringthis is a string"
Wrong. Such an action isn't guaranteed to produce anything. The source and
target strings overlap, and because of this, the results are undefined by
the standard.

From the C standard:

"The strcat function appends a copy of the string pointed to by s2
(including the terminating null character) to the end of the string
pointed to by s1. The initial character of s2 overwrites the null
character at the end of s1.
=> If copying takes place between objects that overlap, the behavior is
=> undefined."
does anyone know why the additional character from the beginning of
the string is being appended at the end?
See above
in addition does anyone know the source for the strcat function? I am
curious as to how they did it.


There's no one source for strcat. In your case, you should ask Borland for a
copy of the source code of their standard C library.

--

Lew Pitcher, IT Consultant, Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)

Nov 13 '05 #6
Trevor Walker wrote:
tf**@hotmail.com says...
using Borland Compiler.

x[32] = "this is a string"

strcat(x,x)


"this is a string" requires 17 bytes of storage. Making two
copies of it requires 33 bytes of storage, so you have not
enough space allocated even for this first strcat. But worse
than that is that the arguments to strcat may not overlap. On
many machines, I would expect this to continue writing to
memory until it crashed.
strcat(x,x)


Now we are trying to fit 65 bytes into 32. But anyway, the
overlap problem still applies.

.... snip ...

but should produce "this is a stringthis is a stringthis is a
stringthis is a string"


No, it should not. See above. You are lucky (some would say
unlucky) that this did not crash your program.
.... snip ...
in addition does anyone know the source for the strcat
function? I am curious as to how they did it.


It is very simple, and you should be able to generate such with
no problem. First you find where to start copying to, and then
you copy.

A safer function to use is strlcpy and strlcat, which is
available in the BSD distribution and other places. They specify
the size of the destination in the calls, which avoids the silly
overflows you have perpetrated above. You can find one
implementation of them at:

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

--
Replies should be to the newsgroup
Chuck Falconer, on vacation.
Nov 13 '05 #7

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

Similar topics

1
by: Richard_Flaherty | last post by:
I will be out of the office starting 07/19/2004 and will not return until 07/26/2004. I will respond to your message when I return.
4
by: inquisitiveman2002 | last post by:
Hello, My webpage looks fine on my computer using IE v6.0, but looks way mis-aligned on my friend's computer eventhough he is using IE v6.0 also. Why is this happening? I do remember his IE saying...
1
by: JMUApache | last post by:
Where can I download some C# MIS System Source code. Any help would be appreciate! Thanks //Chen Hui
3
by: Boris | last post by:
I notice the following strange behavior of the applications running under ..NET Framework. Let say I have application MyApp.exe which reference assembly MyAssembly.dll. The MyAssembly.dll is...
4
by: TWEB | last post by:
I think I may have an IIS / ASP.Net Configuration issue that I need some guidance with resolving. Here's the problem: a) I have a .stm file. b) I referenc a .aspx file on this .stm file using a...
4
by: yinglcs | last post by:
Hi, In my .h file, I declare this: class A{ public: void f1 (const B* bPtr); }; But In my .cpp, I do this:
7
by: nadahalli | last post by:
Hello, I have an XSL file that has something like this: <xsl:if test-condition1> <body attribute1 attribute2> </xsl:if> <xsl:if test-condition2> <body attribute3 attribute4> </xsl:if>
0
by: jimnl69 | last post by:
Does anyone have solutions to the MS Access Database problems in the book MIS CASES - Decision Making with Application Software? Or, does anyone know where I could purchase solutions? Thanks...
19
by: glchin | last post by:
Does a compiler guarantee that the variable w below is placed on an eight-byte aligned address? void myFunction( long iFreq ) { const double w = two_pi * iFreq; ... ... }
1
NeoPa
by: NeoPa | last post by:
Access QueryDefs Mis-save Subquery SQL Access stores its SQL for Subqueries in a strange manner :s It seems to replace the parentheses "()"with square brackets "" and (often) add an extraneous...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.