UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » a strcpy bug

#1 2007-10-31 03:40 PM

zhn636
Member
Registered: 2007-04-24
Posts: 146

Re: a strcpy bug

i define a function to implement copy a string .the code as follows:

void *strcpy(void *dest ,const void *src,size_t n)
{
if(dest==NULL)
   dest=(char * )malloc(n);
   char *d=(char *)dest;
  char *s=(char *)src;
for(int i=0;i!=n;i++)
  {*d++=*s++;
}
  return dest;
}


but I got mistake ,I want to know whether some bugs in above codes.thanks.

Offline

#2 2007-10-31 04:27 PM

biologz
Administrator
From: Puking on the pavement
Registered: 2005-11-02
Posts: 396

Re: a strcpy bug

for(int i=0;i!=n;i++)

gethostbyintuition() is still a dream of mine

                                                 -- quoted from bash

Offline

#3 2007-10-31 07:18 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: a strcpy bug

Actually, as much as I loathe it, I'm pretty sure C99 makes it perfectly valid to declare
variables intermixed with code (rather than at the start of a block), as well as that
silly for() loop index declaration...  I think it's ugly features creeping back from C++...
But, whatever it is, I hate it, and won't tolerate it in MY C code... ;-)

But, yeah, first a question: why the hell are you reinventing the wheel??  If you
want strcpy(), use the libc strcpy()...  If you want strncpy() or memcpy() (as it looks
like you're really trying to recreate), then use that...  They are almost certainly highly
optimized and will outperform any hand-hacked solution you can come up with...

But, if you have some legit reason to create your own version instead, then at least
don't name it the same thing as a common libc function, especially when it has a
completely different API...

Offline

#4 2007-10-31 08:12 PM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: a strcpy bug

Offline

#5 2007-10-31 10:49 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: a strcpy bug

Offline

#6 2007-10-31 11:15 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: a strcpy bug

Offline

#7 2007-10-31 11:45 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: a strcpy bug

Offline

#8 2007-11-01 02:24 AM

zhn636
Member
Registered: 2007-04-24
Posts: 146

Re: a strcpy bug

can we malloc some memory inside a function ?i.e in the above function,  i use

dest=(char*)malloc(n);


Is it right?
can you explain to me?thanks.

Offline

#9 2007-11-01 01:10 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: a strcpy bug

Offline

#10 2007-11-01 06:25 PM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: a strcpy bug

#include <stdio.h>
int main(int, char **)
{
   for (int i=0; i<5; i++) printf("i=%i\n", i);
   for (int j=0; j<5; j++) printf("j=%i\n", i);  // bug, using wrong index variable!
   return 0;
}

[email protected]:~> g++ temp.cpp
temp.cpp: In function &#8216;int main(int, char**)&#8217;:
temp.cpp:6: error: name lookup of &#8216;i&#8217; changed for new ISO &#8216;for&#8217; scoping
temp.cpp:5: error:   using obsolete binding at &#8216;i&#8217;

Offline

#11 2007-11-01 06:37 PM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: a strcpy bug

Offline

#12 2007-11-01 07:09 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: a strcpy bug

My experience is that breaking up big functions into multiple smaller ones (often by
adding one or few helper functions) almost always makes the code more readable,
easier to understand, and shorter. Because it's clear what each functions does,
instead of having one big fuzzy function that tries to do multiple things at once.
Those new functions are of course static and near where they're used.

Of course it's not a hard rule and rarely there's the case that it's over all better to
have one big function because too much state is shared, so adding helper functions
would only increase the mess. Performance could be another reason, if the compiler
messes it up. Long functions that are very straightforward are also no problem.
With "long" I mean more than or around, say, 80 lines.

Offline

#13 2007-11-01 07:24 PM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: a strcpy bug

Offline

#14 2007-11-01 11:14 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: a strcpy bug

int i;

for (i = 0; arr[i]; i++)
    ;

/* i now contains the count of items in the array */

Offline

#15 2007-11-02 06:13 PM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: a strcpy bug

Offline

#16 2007-11-02 07:43 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: a strcpy bug

Offline

#17 2007-11-05 02:40 PM

biologz
Administrator
From: Puking on the pavement
Registered: 2005-11-02
Posts: 396

Re: a strcpy bug

I totally agree with Rob and i3839 :D

Some people say that C is a bad language because it's too binding, but it is what i like. It's much more easy and beautiful to read code with variables all declared at the beginning of the function. Breaking up the code in multiple small functions also increases readibility and is less frightening than a BIG function.

All those rules make the language beautiful to read and to write. Rigor is really important to me, maybe it makes the language more complicated to learn in the beginning but after that it gives you tools to write well structured code, it gives you a clean programming style wich will be easier to read for other people.

I think C has always been a language close to machine and it should stay like it.


gethostbyintuition() is still a dream of mine

                                                 -- quoted from bash

Offline

  • Index
  • » C
  • » a strcpy bug

Board footer

Powered by FluxBB