Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > about design consideration of strcpy()

Reply
Thread Tools

about design consideration of strcpy()

 
 
steve yee
Guest
Posts: n/a
 
      05-25-2006
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

 
Reply With Quote
 
 
 
 
steve yee
Guest
Posts: n/a
 
      05-25-2006
> 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.

 
Reply With Quote
 
 
 
 
Andrew Poelstra
Guest
Posts: n/a
 
      05-25-2006
On 2006-05-25, steve yee <(E-Mail Removed)> 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.
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      05-25-2006
Andrew Poelstra a écrit :
> On 2006-05-25, steve yee <(E-Mail Removed)> 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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-25-2006
Andrew Poelstra <(E-Mail Removed)> writes:
> On 2006-05-25, steve yee <(E-Mail Removed)> 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) http://www.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      05-25-2006
Andrew Poelstra said:

> On 2006-05-25, steve yee <(E-Mail Removed)> 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)
 
Reply With Quote
 
Tomás
Guest
Posts: n/a
 
      05-25-2006
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
 
Reply With Quote
 
Andrew Poelstra
Guest
Posts: n/a
 
      05-25-2006
On 2006-05-25, Richard Heathfield <(E-Mail Removed)> wrote:
> Andrew Poelstra said:
>
>> On 2006-05-25, steve yee <(E-Mail Removed)> 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.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      05-25-2006
Richard Heathfield wrote:
> Andrew Poelstra said:
>> On 2006-05-25, steve yee <(E-Mail Removed)> 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/>


 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      05-25-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
mpls bgp consideration nini Cisco 0 11-26-2008 08:16 AM
Letter-of-consideration =?Utf-8?B?Z3RyaWdodA==?= MCSE 4 03-02-2006 05:26 PM
.NET 2005 not taking code changes into consideration when building Ryan Ternier ASP .Net 0 01-10-2006 12:39 AM
Design consideration regarding dynamic controls Henke ASP .Net Web Controls 0 05-07-2004 02:55 PM
Is classless worth consideration David MacQuigg Python 33 05-03-2004 05:17 PM



Advertisments