R
Régis Troadec
I'm sorry to post such programming misbehaviours,
but it drives me to ask two questions...See below
ugghhh...I forgot to return
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
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 ?
I find &tab quite easy to understand and interpret as the "i-th element
adress"
Best regards, régis
but it drives me to ask two questions...See below
August Derleth said:exit(EXIT_SUCCESS); /* 0 is fine, too */
/* or you could have return 0; just as well */
ugghhh...I forgot to return
Not testing the return value of malloc() is VERY bad, because ifchar* 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));
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 quite easy to understand and interpret as the "i-th element
adress"
Best regards, régis