Re: Something wrong in my program
From: Régis Troadec (regt_at_wanadoo.fr)
Date: 01/16/04
- Next message: SNutter: "Re: Sending messages to a menu but never returning"
- Previous message: Default User: "Re: splitting a string and put it into an array"
- In reply to: August Derleth: "Re: Something wrong in my program"
- Next in thread: Mark McIntyre: "Re: Something wrong in my program"
- Reply: Mark McIntyre: "Re: Something wrong in my program"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 16 Jan 2004 19:40:28 +0100
I'm sorry to post such programming misbehaviours,
but it drives me to ask two questions...See below
"August Derleth" <see@sig.now> a écrit dans le message de news:
kyTNb.812$xu6.467@fe02.usenetserver.com...
> Régis Troadec wrote:
> >
> > I suggest you my example :
> >
> > #include <ctype.h>
> > #include <stdio.h>
> > #include <string.h>
> > #include <stdlib.h>
> >
> > char* trim(char* s);
> >
> > int main()
> > {
> > char * str = " \tHello World";
> > char * trimmed;
> > printf("Original :\n%s\n",str);
> > trimmed = trim(str);
> > printf("Trimmed :\n%s\n",trimmed);
> > free(trimmed);
> exit(EXIT_SUCCESS); /* 0 is fine, too */
> /* or you could have return 0; just as well */
> > }
ugghhh...I forgot to return
> >
> > char* trim(char* s)
> > {
> > char * res;
> > int cnt = 0;
> > int cnt2 = 0;
> >
> > while (isspace(s[cnt]) != 0)
> > cnt++;
> >
> > res = (char*)malloc(sizeof(char)*(strlen(s)-cnt+1));
> Not testing the return value of malloc() is VERY bad, because if
> malloc() returns NULL you will attempt to access memory you don't own.
> Which is a source of undefined behavior. Which means your program, OS,
> and hardware can literally do /anything/ after that point.
>
> It is important enough that even trivial code should test for a failure,
> and some coders (myself included) have created a version of malloc that
> will quit the program with an error message instead of returning NULL.
> (That can be deeply stupid in some cases, which is why I only use it in
> the cases where it makes sense.)
Shame on me, I'll provide much more safe-code next time.
I don't always think to test the retval of malloc
in trivial programs ...I've surely acquired this bad practice by programming
at school
>
> Casting the retval of malloc() is bad:
> 1. Can hide a failure to #include <stdlib.h> because if a prototype is
> not in scope, malloc() implicitly returns an int. This can be dangerous
> on some machines.
> 2. Makes code harder to change later, when you decide to make malloc()
> allocate something besides a buffer of char.
> 3. Demonstrates a lack of knowledge about how a pointer to void works.
> 4. Introduces unneeded visual clutter.
>
2 questions :
1. I've heard that explicit conversion from void* to the type of lvalue
was sometimes necessary for portability because the adresses of used types
could
have different formats on the target system, is it true ?
2. I read the FAQ (7.7), and another explanation (surely good) is given :
casting malloc() was required before ANSI/ISO introduced the void* pointer,
only to silence warnings of assignements. Is it the only reason ?
> sizeof(char) is unneeded, as sizeof(char) == 1 by definition.
> >
> > while (s[cnt] != '\0')
> > res[cnt2++] = s[cnt++];
> >
> > res[cnt2] = '\0';
> >
> > return &res[0];
>
> Why not just return res; ?
I find &tab[i] quite easy to understand and interpret as the "i-th element
adress"
Best regards, régis
- Next message: SNutter: "Re: Sending messages to a menu but never returning"
- Previous message: Default User: "Re: splitting a string and put it into an array"
- In reply to: August Derleth: "Re: Something wrong in my program"
- Next in thread: Mark McIntyre: "Re: Something wrong in my program"
- Reply: Mark McIntyre: "Re: Something wrong in my program"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|