Scoping question in C

James Zhou jameszhou2000-PkbjNfxxIARBDgjK7y7TUQ at public.gmane.org
Mon Jun 13 15:39:14 UTC 2005


It makes sense at first glance. However, if you can provide a correct
length, you can already know whether your buffer (suppose the buffer size is
known) is big enough to use strcpy(), unless what you need is only a
sub-string. Plus, strncpy() will not write '\0' at the end.

I think an alternative might be the strdup() - string duplicate. But it
brings another potential problem if the programmer forgets to explicitly
"free" the space after the use. That is because strdup() actually allocates
the memory space from heap to hold the copied string. :)

Jame Zhou.

----- Original Message ----- 
From: "Taavi Burns" <jaaaarel-Re5JQEeQqe8AvxtiuMwx3w at public.gmane.org>
To: <tlug-lxSQFCZeNF4 at public.gmane.org>
Sent: Monday, June 13, 2005 10:39 AM
Subject: Re: [TLUG]: Scoping question in C


On 6/12/05, Henry Spencer <henry-lqW1N6Cllo0sV2N9l4h3zg at public.gmane.org> wrote:
> C doesn't do array assignments, e.g. copying whole strings, with `='.
> For that, you have to call strcpy().

Note: NEVER use strcpy().  Use only strncpy(), and always provide a
correct length parameter, lest you summon a buffer overflow demon.  ;)

-- 
taa
/*eof*/
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list