Benno wrote:
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:
I don't find C difficult at all. C has one of the least number of
vocabularies to learn and master.
If you understand pointers and pointer arithmetic you have the right
stuff. So, learn pointers
and pointer arithmetic.
C is extremely good because many OSes and Compilers are written in C
including Linux itself
and many tools in Linux.
Open Source is notoriously lacking in documentations. Unless you know C
you are lost and
you hardly can't proceed if you are doing a project.
So, my advise to newbies in Linux, forget about people that discourages
you from writing in
C language. Use C and you will quickly realise the real beauty and
opportunities of Open Source.
It gives you more freedom and independence.
The object of programming is to get what you want in a simple and easy
to understand programming.
That's why my preferred the solution is
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *
somefunction()
{
char *string2 = "some words";
return string2;
}
int main (void)
{
char *string;
string = somefunction();
printf ("\n\nString is: %s\n\n", string);
return 0;
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
This is tremendously simpler than what you have proposed, as ff:
int somefunction(char **string1)
{
/* a function that takes a pointer to a pointer to memory */
char *string2 = "some words";
*string1 = malloc(strlen(string2) + 1);
/* allocate memory and assign it to the char pointer, to which string1
points
to. Known as dereferencing. */
if ((*string1) == NULL) /*SHould at least check return value! */
return -1;
strcpy(*string1, string2);
/* Copy the string */
return 0;
}
int main(void) {
char * string;
int r;
r = somefunction(&string);
/* Pass the address of string to somefunction */
if (r != 0) {
fprintf(stderr, "Couldn't allocate memory\n");
return 1;
}
/* now we know that string actually points somewhere */
printf("String is %s\n", string);
free(string);
return 0;
}
I am discouraged by simply looking at these codes much more
bother to read it.
The trouble other programmers have with C is that they make their
codes complicated than it should.
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!)
I disagree with this. There are lots of things I can do with C that
cannot be done with
other high level languages easily.
O Plameras
--
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html