should I free mem in function after alloc and returning pointer... ?

O

Old Wolf

Chris said:
He isn't, I've had the same requirement (maintaining two pieces of code,
one for C and the other for C++, is far more risky than having an
explicit cast where it isn't required). There are other exceptions:

When external coding standards require it.

When using a compiler which exercises its standard-given right to give
diagnostics about anything it feels like it, and company policy is to
release no code with warnings.

One compiler I use issues a warning if you do use a useless
cast (which you can't turn off without turning off other
useful warnings). I hope you don't have to maintain your code
on this compiler as well as the other ones you mention :)
 
K

Keith Thompson

E. Robert Tisdale said:
Keith said:
Yes, that's a much worse way to do it.
cat tree.cc [snip]
g++ -Wall -ansi -pedantic -o tree tree.cc
tree.cc: In function `int main(int, char**)':
tree.cc:12: error: invalid conversion from `void*' to `tnode*'

My compiler doesn't like pete's way.
But it likes my way just fine.

ERT knows exactly what he's doing here, but for the benefit of others:

g++ is a C++ compiler. If he has g++, he certainly has gcc as well,
which is a C compiler quite capable of handling pete's entirely
correct C code. (The relationship between gcc and g++ is a bit more
complex than that, but the details are irrelevant.)

This newsgroup is about C, not C++. We all know that some C source
code can't be compiled by a C++ compiler. If ERT wants to discuss
C++, I'm sure he knows where to find comp.lang.c++.
 
R

Richard Bos

Peter Nilsson said:
David said:
Keith said:
David REsnick writes:
[...]

With the odd exception, don't cast malloc return.

The "odd exception" you're referring to is writing code that needs to
compile as both C and C++, right?

Yes. I could have also said "unless your initials are P.J.P.",
as he seems to be the only one on the group who has expressed a
real need for that.

No, countless people expressed the same need before Plauger
entered the debate. [The possibility has been in the K&R2
errata for ages.]

Many people _expressed_ the same need, but only Plauger demonstrated it
beyond "but it won't work because I'm incapable of getting MSVC++ to do
the right thihingg!!!".

Richard
 
R

Richard Bos

beetle said:
Ehm sorry maybe I am to new to C, but how would you handle something
like structs then ?

Why would structs be _any_ different in this regard than ints, floats
and char **s?

Richard
 
O

Old Wolf

E. Robert Tisdale said:
int main(int argc, char* argv[]) {
tnode* pointer = malloc(sizeof(tnode));
free((void*)pointer);

Now, this is an entirely useless cast -- in both
languages, implicit conversion TO (void *) is
permitted (although C++ does not allow implicit
conversion FROM (void *)).
 
M

Mabden

Keith Thompson said:
I don't char what type bill happens to be

Well some of us DO char! Char-ity begins at main().

Oops, my s2n value may have dropped during this post! (Please ignore:
inside joke meant only for people who have plonked me and will never see
it - and Keith who refuses to out of perversity and good sense)
 

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

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top