[Sussex] Re: C programming help - again!
Captain Redbeard
hairy.one at virgin.net
Wed May 11 18:00:48 UTC 2005
Hi all,
> Geoffrey Teale wrote:
>
> C development can be very slow at first. People tend to
> build up their own libraries of code to help speed things
> along though.
Good God, I thought I was the only one who did that! I've
already written three or four subroutines that encompass
standard library routines adding boundary-checking and other
stuff, and I thought I was doing something wrong!
> As a general rule I'd keep this manual bookmarked:...
Good idea, done.
> Mike Diack wrote:
>
> What's worth noticing is that the "Hello World!\n\n\n"
> string you are trying to copy is longer than the 13 bytes
> you are asking strncpy to copy. Is this intentional?
No, it's a typo. The figure should have been 15 but even
with that I still got the error message. However simply
changing the figure to 16 keeps Valgrind happy so thanks for
the data (same to Steve and Geoffrey who basically said the
same thing).
> Steve Dobson wrote:
>
> Your code has to handle this, for example:
>
> TestStructPtr->CharLen = 13;
> TestStructPtr->CharString =
> (char *) malloc(TestStructPtr->CharLen + 1);
> .
> .
> .
> const char *str = "Hello World!\n\n\n";
> bzero(TestStructPtr->CharString,
> TestStructPtr->CharLen + 1);
> strncpy (TestStructPtr->CharString, str,
> TestStructPtr->CharLen);
Thanks for pointing out the bzero() function, I didn't know
such a function existed and was planning to write my own!
You've saved me an hours work or so.
In the second line of your code you've included a char cast
of the malloc() call, why is this cast necessary (I'm
assuming it's a cast although the in C '*' is a bit of a
swiss army knife)?
Anyway, I've just sat down in front of my Emacs box again so
I'm sure I'll be posting a few more WTF-type questions again
before the end of the night. Thanks for all your help so far!
--
Captain Redbeard
======================================================
Insomnia - it's not a condition, it's a mentality.
Registered Linux user: 388693.
Registered Linux machines: 289172, 289173.
v2sw5+8C+JGhw5ln4/5pr6/7Ock1ma6/8l6+8Sw3+2e4t2b9Hen4+6
g5+3RHaIr9 Hackerkey.com
======================================================
More information about the Sussex
mailing list