Re: Something wrong in my program

From: Régis Troadec (regt_at_wanadoo.fr)
Date: 01/16/04


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



Relevant Pages

  • Re: MFC Interview Tests
    ... That's one smart kid for figuring out the problems with BASIC, ... what might be real world programming practices to you might not be to ... way to allocate memory", so I said "malloc or new. ...
    (microsoft.public.vc.mfc)
  • Re: HeapAlloc vs mallc
    ... If you are programming C++, the number of times you EVER want to use malloc or HeapAlloc ... Doing allocation in DllMain is also outside what I consider the scope of "normal" ...
    (microsoft.public.vc.mfc)
  • Re: MFC Interview Tests
    ... The intersect rect problem that I give applicants is for them to actually ... my past experience (18 years of VC++ programming). ... way to allocate memory", so I said "malloc or new. ...
    (microsoft.public.vc.mfc)
  • Re: Implementing Malloc()
    ... The huge majority of programmers won't bother to check malloc() failure ... for such a small allocation, ... then the C programming world is in deep trouble. ...
    (comp.lang.c)
  • Re: Portability: Harmony between PC and microcontroller
    ... int is the natural integer type for the system. ... You are, perhaps unintentionally, paraphrasing the standard in a way ... One of the things that you might not realize is that the C programming ... In the real world, most embedded systems have more complex jobs to do, ...
    (comp.lang.c)