On Tue Nov 22, 2005 at 16:52:06 +1100, O Plameras wrote:
>Benno wrote:
>
>>On Tue Nov 22, 2005 at 16:16:29 +1100, O Plameras wrote:
>>
>>>
>>>Coding 1.
>>>#include <stdio.h>
>>>#include <stdlib.h>
>>>#include <string.h>
>>>char *
>>>somefunction()
>>>{
>>> char *string1;
>>> char *string2 = "some words\0";
>>> string1 = (char *)calloc(strlen(string2 + 1), sizeof (char));
>>>
>>>
>>
>>Should be strlen(string2) + 1
>>
>>
>>
>
>Your are wrong. The string is "some words\0". there is no need for extra
>char.
I am right. You are wrong. I love this game. So, even if you were right
about the extra \0 being counted, which you aren't, it would still not
be long enough because you have:
strlen(string2 + 1);
not:
strlen(string2) + 1;
>>> strcpy(string1, string2);
>>> return string1;
>>>}
>>>int main (void)
>>>{
>>> char *string;
>>> string = somefunction();
>>> printf ("\n\nString is: %s\n\n", string);
>>> return 0;
>>>}
>>>
>>>Coding 2.
>>>#include <stdio.h>
>>>#include <stdlib.h>
>>>#include <string.h>
>>>char *
>>>somefunction()
>>>{
>>> char *string1;
>>> char *string2 = "some words\0";
>>> string1 = malloc(strlen(string2));
>>>
>>>
>>
>>Should be strlen(string2) + 1
>>
>>
>>
>
>Your are wrong. The string is "some words\0". there is no need for extra
>char.
Bzzt. Wrong. C strings are NULL terminated. \0 is null. In memory you will have
two NULL terminated, strlen is going to hit it first. Don't belive me? try
strlen("A\0this doesn't count");
It returns 1.
In fact here is a whole program you can copy and compile to test the above:
include <stdio.h>
#include <string.h>
#include <assert.h>
int
main(void)
{
char *s = "pants\0";
char *s2 = "pants";
printf("strlen(s) == %d\n", strlen(s));
printf("strlen(s2) == %d\n", strlen(s2));
printf("strlen(s) + 1 == %d\n", strlen(s) + 1);
printf("strlen(s + 1) == %d\n", strlen(s + 1));
printf("strlen(\"A\\0this doesn't count\") == %d\n",
strlen("A\0this doesn't count"));
assert(strlen(s) == strlen(s2));
}
It outputs:
strlen(s) == 5
strlen(s2) == 5
strlen(s) + 1 == 6
strlen(s + 1) == 4
strlen("A\0this doesn't count") == 1
>>> strcpy(string1, string2);
>>> return string1;
>>>}
>>>int main (void)
>>>{
>>> char *string;
>>> string = somefunction();
>>> printf ("\n\nString is: %s\n\n", string);
>>> return 0;
>>>}
>>>
>>>
>>
>>Moral of the story is don't code in C ;) Or if you do use as many tools
>>to help you as possible. -Wall, and a C linter are a good start.
>>Valgrind is also meant to be good, although I haven't
>>used it.
>>
>>
>
>I don't understand this. Please explain.
C is a very difficult programming language to program in and get right.
(As seen in this thread). Programming errors in C are likely to lead
to buggy unstable, insecure software. Therefore if you program in C you
should try and get as much help from tools to ensure that what you have
programmed is correct. Some of those tools are:
1/ Using the compilers warning system.
2/ Using something like valgrind to test for memory errors.
3/ Use a static analysis tool like splint.
(Apart from that C is a very unproductive language to program in, so unless
you really need it then use something higher level -- please!)
Benno
--
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html