![]() |
| | #1 |
| Registered User Join Date: Jan 2005
Posts: 15
| strncpy question, size -1?? saw this code, curious as to why the author -1 from the sizeof? Code: char host[512] memset (&host, 0, sizeof(host)); strncpy (host, argv[optind], sizeof (host) -1 ); Cheers! |
| fayte is offline | |
| | #2 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,698
| The last character wouldn't be copied.
__________________ dwk Seek and ye shall find. quaere et invenies. "Simplicity does not precede complexity, but follows it." -- Alan Perlis "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra "The only real mistake is the one from which we learn nothing." -- John Powell Other boards: DaniWeb, TPS Unofficial Wiki FAQ: cpwiki.sf.net My website: http://dwks.theprogrammingsite.com/ Projects: codeform, xuni, atlantis, nort, etc. |
| dwks is offline | |
| | #3 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,710
| > memset (&host, 0, sizeof(host)); Unnecessary & here > why wouldnt the strncpy just be the size of host? Well having expensively filled the array with zeros, by only copying n-1 characters you ensure there is at least one \0 in the string. |
| Salem is offline | |
| | #4 |
| Registered User Join Date: Jan 2005
Posts: 15
| So do you think the memset is unnesessary here? I quite new to mem operations, i assumed it was good to sanatise the array?? So by coping one less you make sure there is one 0 at the end. Is that was \0 is? Sorry, i understand that \0 represents the end of a string, (not sure if that the best way to put it) just didnt know how that is actually represnted. Is a 0 byte = \0 then. Im sure this probably seems very stupid! Just one of those things, self taught so im sure I always miss things I should have leant! Thankyou! Appreciate that, clears it up. I thought it was due to some kind of security, stop buffer overflows or something! *edit-spelling. |
| fayte is offline | |
| | #5 |
| Registered Luser Join Date: Jul 2005 Location: Singapore
Posts: 867
| The memset is unecessary because it fills the entire buffer with zeroes. However, you need something to ensure there a '\0' (null character) at the end because strncpy is brain damaged. If the source is longer than the destination buffer (according to your specified size) then strncpy won't terminate the string with a null, so you need something to add a null at the end always. strncpy's other brain damaged behaviour is to fill the rest of the buffer with nulls if the source is shorter than the destination buffer. |
| cwr is offline | |
| | #6 | ||
| Been here, done that. Join Date: May 2003
Posts: 1,036
| Quote:
Code: #include <stdio.h>
#include <string.h>
int main()
{
char a[] = "abcdefghijklmnop";
char b[] = "1234567890";
printf("%s \n", a);
strncpy(a, b, 7);
printf("%s \n", a);
return 0;
}
Quote:
If the source buffer is smaller than the number of characters specified, behaviour of strncpy() is to fill the extra characters in the destination buffer with '\0' until the number of characters specified is reached. IOW, Code: char a[] = "abcdefghijklmnop";
char b[] = "123";
strncpy(a, b, 7);
__________________ There are only 10 types of people in the world -- those that use binary, and those that don't | ||
| WaltP is offline | |
| | #7 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,710
| strncpy's other (other) brain damage is that it doesn't always append a \0. > So do you think the memset is unnesessary here? Well this does the same thing strncpy (host, argv[optind], sizeof (host) -1 ); host[sizeof (host)-1] = '\0'; ISTR a website from one of the authors of C explaining the behaviour of strncpy etc. It is a relic of some very old UNIX API as I recall. |
| Salem is offline | |
| | #8 | |||
| Registered Luser Join Date: Jul 2005 Location: Singapore
Posts: 867
| Quote:
Quote:
Quote:
Code: char x[16384]; strncpy(x, src, sizeof x - 1); | |||
| cwr is offline | |
| | #9 |
| Registered User Join Date: Jan 2005
Posts: 15
| Wow, some really interesting responses. Really understand whats going on now. Its really interesting how different people approach a subject. Salem, cheers for that sample code, that makes much more sense. |
| fayte is offline | |
| | #10 |
| & the hat of GPL slaying Join Date: Sep 2001
Posts: 5,732
| How is strncpy not copying a null over a brain damage thing? What if you are only copying part of a string into the middle of another string? Would you really want it putting a null character there? |
| Thantos is offline | |
| | #11 |
| Registered User Join Date: Jan 2005
Posts: 15
| I tried doing what Saleam suggested: strncpy (host, argv[optind], sizeof (host) -1 ); host[sizeof (host)-1] = '\0'; However the compiler wouldnt let me. Replacing the ='\0'; with = 0; does work thought. Are they the same thing?? |
| fayte is offline | |
| | #12 | |
| Registered Luser Join Date: Jul 2005 Location: Singapore
Posts: 867
| Quote:
It's brain damaged because you'd expect a function concerned with copying strings to always result in a string. If you consider that in C, "string" is synonymous with "null terminated character array", and strncpy violates this idea. A lot of bad code simply has strncpy(dst, src, sizeof dst); and doesn't bother to explicitly null terminate the string. One might be tempted to say "that's the programmer's fault", and it is, but it also goes against what one expects. edit: The C FAQ entry has more info on why strncpy was designed as it was, and also suggests memcpy and/or strncat for accomplishing similar. | |
| cwr is offline | |
| | #13 | |||
| Been here, done that. Join Date: May 2003
Posts: 1,036
| Quote:
Quote:
strncpy() is meant to move substrings into other strings, not concatinate the source string into the middle of another string erasing the end. Quote:
__________________ There are only 10 types of people in the world -- those that use binary, and those that don't | |||
| WaltP is offline | |
| | #14 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,710
| > I already stated that in my post above. So you did - my mistake. And thanks for the link - I'd obviously forgotten that it was in the clc FAQ. > However the compiler wouldnt let me Explain with an error message - how didn't it "let you" do that? |
| Salem is offline | |
| | #15 |
| Yes, my avatar is stolen Join Date: Dec 2002
Posts: 2,544
| |
| anonytmouse is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [Tutorial] Implementing the Advanced Encryption Standard | KONI | C Programming | 16 | 11-23-2007 01:48 PM |
| Error with a vector | Tropicalia | C++ Programming | 20 | 09-28-2006 07:45 PM |
| char array size question, please help! | Ash1981 | C Programming | 4 | 01-29-2006 02:30 AM |
| another exercise question | luigi40 | C# Programming | 3 | 11-28-2005 03:52 PM |
| A noob question - how to reduce the size of an array? | itachi | C Programming | 2 | 11-24-2005 01:30 AM |