Help with understanding this please

M

mdh

I am trying to define a struct called temp, using this code. Please be
kind in explaining with this is not working :)

#include <stdio.h>
#include <stdlib.h>


struct t_node_n{
char *word;
int match;
struct t_node_n *left;
struct t_node_n *right;
};


struct t_node_n *temp = (struct t_node_n *) malloc(sizeof(struct
t_node_n)); /* error: initializer element is not constant */

int main {};



So, this is what I **thought** I was doing.
Create a pointer of type struct t_node_n called "temp".
Define the pointer "temp" by calling malloc....which I **thought**
gives the pointer "memory" to point to, thus defines it?
Clearly these thoughts are way off base!!


Thanks :)
 
M

mdh

mdh said:


Better: struct t_node_n *temp = malloc(sizeof *temp);

Cleaner, tighter, less maintenance hassle.

Richard...before I rest so that sleep can be calm!!, may I ask why you
have not cast the return from malloc to type "struct t_node n *). K&R
devote quite a discussion to this on page 142.

Thanks as always.
 
M

mdh

mdh said:
Better: struct t_node_n *temp = malloc(sizeof *temp);
Cleaner, tighter, less maintenance hassle.


Richard...before I rest so that sleep can be calm!!, may I ask why
you
have not cast the return from malloc to type (struct t_node n *). K&R
devote quite a discussion to this on page 142.
Thanks as always.
 
M

mdh

Why on earth would I do that? What good could it possibly do?


That *was* the proper method, before void * was introduced to the language.
This is one of those very few parts of this excellent book that should
have been written more carefully. It is true that the malloc function used
to return char *, way back in the Cretaceous Period, but for about twenty
years it has returned void *. The language automagically provides an
implicit conversion from void * to struct t_node_n *, so there is no need
for a cast.

Thanks as always...and thanks for the humor or humour...which I know
is lost on some, but is immensely appreciated by myself.
 
R

Richard

Richard Heathfield said:
mdh said:


Why on earth would I do that? What good could it possibly do?

Yeah because C++ is wrong and so was pre Ansi C.

Streuth.
 
R

Richard

Malcolm McLean said:
It's an emphasis problem.
You want to warn about potentially unsafe operations, such as
converting from one type to another.
However only rarely will you want to assign the result of malloc() to
a void *. In this case, the warnings are gratuitous. Too many warnings
are as bad as too few, because people start ignoring them, and the
genuinely exceptional casts get lost in the noise.

You miss the point. Heathfield's typically smarmy reply seemed to indicate
that the cast was something only an idiot would do.
 
L

lawrence.jones

Richard said:
Yeah because C++ is wrong and so was pre Ansi C.

C++ isn't wrong, it's different (C++ programs should use new rather than
malloc). Pre-ANSI C *was* wrong, that's why ANSI changed it.
 
K

Keith Thompson

mdh said:
Richard...before I rest so that sleep can be calm!!, may I ask why you
have not cast the return from malloc to type "struct t_node n *). K&R
devote quite a discussion to this on page 142.
[...]

It's also discussed on the K&R2 errata page,
<http://plan9.bell-labs.com/cm/cs/cbook/2ediffs.html>.
Now that the server is back up, I can quote it:

142(6.5, toward the end): The remark about casting the return
value of malloc ("the proper method is to declare ... then
explicitly coerce") needs to be rewritten. The example is correct
and works, but the advice is debatable in the context of the
1988-1989 ANSI/ISO standards. It's not necessary (given that
coercion of void * to ALMOSTANYTYPE * is automatic), and possibly
harmful if malloc, or a proxy for it, fails to be declared as
returning void *. The explicit cast can cover up an unintended
error. On the other hand, pre-ANSI, the cast was necessary, and it
is in C++ also.
 
R

Richard

C++ isn't wrong, it's different (C++ programs should use new rather than
malloc). Pre-ANSI C *was* wrong, that's why ANSI changed it.

My point seems to have eluded you. And that is it is NOT obvious. And
Heathfield's reply was less than useless.
 
M

mdh

In C, you *never* have to cast the return from malloc.


This is an error in K&R which the authors have acknowledged. The
discussion predates ANSI C.

Thank you to all who have pointed this out...much appreciated.
 
K

Keith Thompson

Richard said:
My point seems to have eluded you. And that is it is NOT obvious. And
Heathfield's reply was less than useless.

Let me draw your attention to the original poster's response to
RH's "less than useless" reply:

Thanks as always...and thanks for the humor or humour...which I know
is lost on some, but is immensely appreciated by myself.
 
R

Richard

Keith Thompson said:
Let me draw your attention to the original poster's response to
RH's "less than useless" reply:

Thanks as always...and thanks for the humor or humour...which I know
is lost on some, but is immensely appreciated by myself.

I'm not sure if you are trying to be amusing or typically pedantic and
elusive.

If you think

is a good answer to a newbies question then you need to take a break.
 
K

Keith Thompson

Richard said:
I'm not sure if you are trying to be amusing or typically pedantic and
elusive.
Neither.

If you think


is a good answer to a newbies question then you need to take a break.

I don't think it would have been a good answer by itself. It was, in
fact, a preamble to a longer answer (which you naturally snipped)
which explained why there is no good reason to cast the result of
malloc(). Since the person asking the question found the answer *as a
whole* useful, your opinion that it's "less than useless" would seem
to be nothing more than your usual trolling.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top