473,398 Members | 2,812 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,398 software developers and data experts.

about design consideration of strcpy()

strcpy() returns it's input parameter. i asked a similar question on
c++, most people seem to not think it as a problemic design. how do you
think of this?

http://groups.google.com/group/comp....0aa4307b91bad8

May 25 '06 #1
13 1910
> most people seem to not think it as a problemic design.

sorry. it should be:
most people seem to think it as a problemic design.

May 25 '06 #2
On 2006-05-25, steve yee <yi******@gmail.com> wrote:
most people seem to not think it as a problemic design.


sorry. it should be:
most people seem to think it as a problemic design.


I can't imagine what the C++ folks know about C. There is absolutely
nothing wrong with strcpy's design.

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
It's just like stealing teeth from a baby.
May 25 '06 #3
Andrew Poelstra a écrit :
On 2006-05-25, steve yee <yi******@gmail.com> wrote:
most people seem to not think it as a problemic design.


sorry. it should be:
most people seem to think it as a problemic design.

I can't imagine what the C++ folks know about C. There is absolutely
nothing wrong with strcpy's design.


Everything is wrong in that design:

1) No provision for bounds of the character strings. strcpy is the most
often found culprit of buffer overflows and memory overwrites.
2) The return value makes no provision for error reporting. This is
in most cases a bad design.
3) The return value returns no useful information.
May 25 '06 #4
Andrew Poelstra <ap*******@wpsoftware.net> writes:
On 2006-05-25, steve yee <yi******@gmail.com> wrote:
most people seem to not think it as a problemic design.


sorry. it should be:
most people seem to think it as a problemic design.


I can't imagine what the C++ folks know about C. There is absolutely
nothing wrong with strcpy's design.


Presumably they know about the parts of C that are included by
reference in the C++ standard. That includes strcpy().

I think the point of having strcpy() return its first argument is to
allow calls to be chained together. For example:

strcat(strcpy(s1, s2), s3);

Personally, I'm not convinced this is all that useful; I find it
clearer to do one call at a time:

strcpy(s1, s2);
strcat(s1, s3);

which can also make it easier to confirm there's enough room for the
result.

I think the real answer is historical precedent.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 25 '06 #5
Andrew Poelstra said:
On 2006-05-25, steve yee <yi******@gmail.com> wrote:
most people seem to not think it as a problemic design.
sorry. it should be:
most people seem to think it as a problemic design.


I can't imagine what the C++ folks know about C.


Some of them are pretty bright, you know - and some are even C experts who
happen not to like the language as much as they like C++. (Weird, I know,
but then there's nowt as queer as folk.)
There is absolutely nothing wrong with strcpy's design.


Yeah there is. It ought to return a pointer to the null terminator. Giving
back the pointer you gave it in the first place is a complete waste of
time.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 25 '06 #6
Richard Heathfield posted:

Some of them are pretty bright, you know - and some are even C experts
who happen not to like the language as much as they like C++. (Weird,
I know, but then there's nowt as queer as folk.)


I know my way inside out through the C subset of C++. I can do everything
I want to do in C, but I haven't got its Standard memorised as much as
the C++ Standard. For instance, yesterday I was hesitant to state the
following:
int array[5] = { 3, 4 };

/* First two elements are 3, 4, and all others are zero */
I know without a shadow of a doubt that this is true for C++, but didn't
want to post misinformation just in case I was wrong about C.

Even though I'm an expert C++ programmer, and know its every fancy
feature inside out, I'm still an expert at the lower-level stuff which
are held in common with C.

(Before I say anything: I'm not waving the C++ flag on a C newsgroup, but
since the topic's been brought up:) I myself prefer C++. That said, a
lot of my C++ code will compile as C code (i.e. I like to keep things
basic), but there are times when the more fancy features are definitely
preferable. Even when I'm writing C code, I can find plenty of places
where I'd like to use a template, but can't.

The reason I read and post on this C newsgroup is that there's a lot more
posts about the "lower-level" stuff, than you would find on a C++
newsgroup. In general, I prefer the lower-level stuff to the higher-level
stuff.

-Tomás
May 25 '06 #7
On 2006-05-25, Richard Heathfield <in*****@invalid.invalid> wrote:
Andrew Poelstra said:
On 2006-05-25, steve yee <yi******@gmail.com> wrote:
most people seem to not think it as a problemic design.

sorry. it should be:
most people seem to think it as a problemic design.


I can't imagine what the C++ folks know about C.


Some of them are pretty bright, you know - and some are even C experts who
happen not to like the language as much as they like C++. (Weird, I know,
but then there's nowt as queer as folk.)

Absolutely true. That's not what I meant. strcpy doesn't fit in with C++'s
idea of everything being simple and beautiful and not having pointers.
Therefore, even if a person is a C expert, if they prefer C++, their opinion
will be skewed in a C++ey way.

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
It's just like stealing teeth from a baby.
May 25 '06 #8
Richard Heathfield wrote:
Andrew Poelstra said:
On 2006-05-25, steve yee <yi******@gmail.com> wrote:
most people seem to not think it as a problemic design.

sorry. it should be:
most people seem to think it as a problemic design.


I can't imagine what the C++ folks know about C.


Some of them are pretty bright, you know - and some are even C
experts who happen not to like the language as much as they like
C++. (Weird, I know, but then there's nowt as queer as folk.)
There is absolutely nothing wrong with strcpy's design.


Yeah there is. It ought to return a pointer to the null
terminator. Giving back the pointer you gave it in the first
place is a complete waste of time.


Except that is the wrong problem. Such routines are better off
returning void, to prevent the sort of chaining that often gets
done. Consider a hypothetical strrev(s) routine, that returns the
value of s, and reverses the string in place. It can then be used
as:

printf("%s\n%s\n", s, strrev(s));

which looks clean. However the results will surprise the user,
because both printed strings are the reverse form. If the routine
returns void then the user would have written:

puts(s); strrev(s); puts(s);

and the birds would sing, the sun would shine...

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
May 25 '06 #9
CBFalconer wrote:

Richard Heathfield wrote:
Andrew Poelstra said:
On 2006-05-25, steve yee <yi******@gmail.com> wrote:

> most people seem to not think it as a problemic design.

sorry. it should be:
most people seem to think it as a problemic design.

I can't imagine what the C++ folks know about C.


Some of them are pretty bright, you know - and some are even C
experts who happen not to like the language as much as they like
C++. (Weird, I know, but then there's nowt as queer as folk.)
There is absolutely nothing wrong with strcpy's design.


Yeah there is. It ought to return a pointer to the null
terminator. Giving back the pointer you gave it in the first
place is a complete waste of time.


Except that is the wrong problem. Such routines are better off
returning void, to prevent the sort of chaining that often gets
done. Consider a hypothetical strrev(s) routine, that returns the
value of s, and reverses the string in place. It can then be used
as:

printf("%s\n%s\n", s, strrev(s));

which looks clean. However the results will surprise the user,
because both printed strings are the reverse form. If the routine
returns void then the user would have written:

puts(s); strrev(s); puts(s);

and the birds would sing, the sun would shine...

....or with the "strrev()" that returns the pointer, you could write:

printf("%s\n%s\n",strrev(s),strrev(s));

Now *no* matter which way things are evaluated, the strings will
be the reverse of each other... ;-)
--
+----------------------------------------------------------------+
| Charles and Francis Richmond richmond at plano dot net |
+----------------------------------------------------------------+
May 25 '06 #10
Richard Heathfield wrote:
Andrew Poelstra said:

I can't imagine what the C++ folks know about C.


Some of them are pretty bright, you know - and some are even C
experts who happen not to like the language as much as they like C++.
(Weird, I know, but then there's nowt as queer as folk.)


And some are old C programmers who happen to work in jobs that require
C++.


Brian
May 25 '06 #11
Charles Richmond wrote:
CBFalconer wrote:
.... snip ...
Except that is the wrong problem. Such routines are better off
returning void, to prevent the sort of chaining that often gets
done. Consider a hypothetical strrev(s) routine, that returns the
value of s, and reverses the string in place. It can then be used
as:

printf("%s\n%s\n", s, strrev(s));

which looks clean. However the results will surprise the user,
because both printed strings are the reverse form. If the routine
returns void then the user would have written:

puts(s); strrev(s); puts(s);

and the birds would sing, the sun would shine...

...or with the "strrev()" that returns the pointer, you could write:

printf("%s\n%s\n",strrev(s),strrev(s));

Now *no* matter which way things are evaluated, the strings will
be the reverse of each other... ;-)


Surprise. No they won't be reverses of each other. Try it and
see. That is the evil caused by returning the parameter pointer as
a result. Think about it.

Here's a strrev routine for you to test with: The comments
indicate how to make it return the input pointer.

/* ======================= */
/* reverse string in place */
void revstring(char *string)
{
char *last, temp;
/* char *s = string; */

last = string + strlen(string); /* points to '\0' */
if (*string)
while (last-- > string) {
temp = *string; *string++ = *last; *last = temp;
}
/* return s */
} /* revstring */

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
May 26 '06 #12
CBFalconer wrote:

Charles Richmond wrote:
CBFalconer wrote:
... snip ...
Except that is the wrong problem. Such routines are better off
returning void, to prevent the sort of chaining that often gets
done. Consider a hypothetical strrev(s) routine, that returns the
value of s, and reverses the string in place. It can then be used
as:

printf("%s\n%s\n", s, strrev(s));

which looks clean. However the results will surprise the user,
because both printed strings are the reverse form. If the routine
returns void then the user would have written:

puts(s); strrev(s); puts(s);

and the birds would sing, the sun would shine...

...or with the "strrev()" that returns the pointer, you could write:

printf("%s\n%s\n",strrev(s),strrev(s));

Now *no* matter which way things are evaluated, the strings will
be the reverse of each other... ;-)


Surprise. No they won't be reverses of each other.


That's a good one.
The side effects of both strrev calls are complete,
before printf's side effects begin.

--
pete
May 26 '06 #13
CBFalconer wrote:

Charles Richmond wrote:
CBFalconer wrote:
... snip ...
Except that is the wrong problem. Such routines are better off
returning void, to prevent the sort of chaining that often gets
done. Consider a hypothetical strrev(s) routine, that returns the
value of s, and reverses the string in place. It can then be used
as:

printf("%s\n%s\n", s, strrev(s));

which looks clean. However the results will surprise the user,
because both printed strings are the reverse form. If the routine
returns void then the user would have written:

puts(s); strrev(s); puts(s);

and the birds would sing, the sun would shine...

...or with the "strrev()" that returns the pointer, you could write:

printf("%s\n%s\n",strrev(s),strrev(s));

Now *no* matter which way things are evaluated, the strings will
be the reverse of each other... ;-)

You are right. I am caught again in a familiar trap. The "printf()"
just passes the pointer to the string twice, and the string remains as
it was the *last* time that "strrev()" was run.

At a PPoE, I fixed a c program that someone else had written. There
was a function to do something to a copy of a string. The function
copied the string into a static local string variable, did the
conversion there, and returned the pointer to the static local string.

The problem resulted from calling this function *twice* in the same
parameter list. It is strikingly similar to what happened with
"strrev()" above. Programmer beware!!!
--
+----------------------------------------------------------------+
| Charles and Francis Richmond richmond at plano dot net |
+----------------------------------------------------------------+
May 26 '06 #14

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

Similar topics

40
by: JohnnyCJohnny | last post by:
Is it pretty safe to say that almost all web surfers now use browsers that are Frames compatible? What are most people using these days? IE? Thanks
9
by: ldufrane | last post by:
Hi All, I just started a project, which features various great websites that use CSS/web standards, this projects also highlights what techniques they use to create the wow effects, in certain...
17
by: Shea Martin | last post by:
I have a MyString class: class MyString { public: MyString() {} MyString(const char *str) { _buffer = new char; strcpy(_buffer, str); }
3
by: Andrej Hristoliubov | last post by:
I am the best c++ programmer in the whole wide world. Trust ME! My reference is Victor Bazarov,Valentin Samko,Alf P.Steinbach( Me and Alf actually intern together at Microsoft), and Bjarne...
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...
0
by: Ice | last post by:
All - I just wanted to folks thoughts on what "ideal" webservice design is based on their experiences taking into consideration the two observations (and any others you may have) below: 1....
4
by: JoeC | last post by:
I am trying to design some complex objects that have quite a bit of data. I understand most syntax but I am trying to learn how to make better design choices. The first question is to OK or good...
9
by: Grizlyk | last post by:
Somebody have offered std colors to C++ in the msg here: http://groups.google.com/group/comp.lang.c++/browse_frm/thread/2e5bb3d36ece543b/1acf6cd7e3ebdbcd#1acf6cd7e3ebdbcd The main objection to...
5
by: mike.rosset+gnus | last post by:
Hello All: I am new to C and I have a program design question. Not only am I trying to learn C but I'm also trying to pick up good habits. And I'm cocerned with doing things the right and...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.