IS this a proper way of freeing memory with free()

C

Christopher Layne

Yevgen said:
Okay, it's really funny how people read not what's written.
I am *not* saying any compiler must emit warning about missing
prototype. I am saying gcc does it when you use -W -Wall.
And Ryan Ply said he uses appropriate compiler flags to get
the warning.

IF you use -W. A lot of people just use -Wall.
I don't get the issue here. How about just not cast the return from malloc()
in the first place because it's a pointless thing to do? Not changing that is
just plain refusal to change.
 
Y

Yevgen Muntyan

Christopher said:
IF you use -W. A lot of people just use -Wall.

Who said anything about those people?
I don't get the issue here. How about just not cast the return from malloc()
in the first place because it's a pointless thing to do? Not changing that is
just plain refusal to change.

The whole issue is about people in this thread who can't
understand that original thing was about particular original
thing, not about rules of thumb.

Yes, needless casting is no good. But, casting may be 1) needed,
2) desired for whatever ridiculous reason the author pleases.
Now, if this particular person uses the cast and uses the appropriate
compiler flags when compiling his code, then he's safe. If I use
-W -Wall, I get that warning (not mandated by the standard blah blah)
and I am safe. No, I am not saying everybody should use cast there.

It's just ridiculous what you're saying: "Not changing that is just
plain refusal to change." If you change your code when you're told
so by people who can't read, then it's your personal business. But
it's a stupid thing to do, for sure.

How about compiling with C++ compiler? Remove cast and get an error?
Because "you didn't refuse to change"? Nonsense.

Yevgen
 
C

Christopher Layne

Yevgen said:
The whole issue is about people in this thread who can't
understand that original thing was about particular original
thing, not about rules of thumb.

Original pointless thing to do, you mean. "Because it makes me feel good."
Yes, needless casting is no good. But, casting may be 1) needed,
2) desired for whatever ridiculous reason the author pleases.
Now, if this particular person uses the cast and uses the appropriate
compiler flags when compiling his code, then he's safe. If I use
-W -Wall, I get that warning (not mandated by the standard blah blah)
and I am safe. No, I am not saying everybody should use cast there.

In response to "1", give me an example of where casting the return value from
malloc(), calloc(), or realloc() is needed in ISO C.
It's just ridiculous what you're saying: "Not changing that is just
plain refusal to change." If you change your code when you're told
so by people who can't read, then it's your personal business. But
it's a stupid thing to do, for sure.

How about compiling with C++ compiler? Remove cast and get an error?
Because "you didn't refuse to change"? Nonsense.

This isn't C++ here.
 
C

Christopher Layne

Yevgen said:
I didn't say there is such a requirement. Did I or someone else say it?
The problem is that Ryan said one thing ("works fine for me because
I make it so explicitely") and you all treated him like an fool which
doesn't read the FAQ or something.

Yevgen

Do you always voraciously support the underdog, or just when the issue hits
home for you?
 
Y

Yevgen Muntyan

Christopher said:
Original pointless thing to do, you mean. "Because it makes me feel good."

Even if so, what is so bad about it? If one finds code with cast more
readable, then the cast is good for him (you can try to read the
thread, you'll see what I am talking about). No, it's not a pointless
thing.
In response to "1", give me an example of where casting the return value from
malloc(), calloc(), or realloc() is needed in ISO C.

Well, see below. How about 2) anyway?
This isn't C++ here.

Same thing again, huh? "here" means what? I can compile C code with
C++ compiler *here*, and I may want my *C* code to be compilable
with C++ compiler, now what? Come on, I am not going to try to convince
*you* that *you* want the cast. Just because I don't think so.
What I am trying to do is to convince you that *I* may want to
do that evil thing, and I am still fine doing that evil thing.
Because I use compiler flags lalala, lalala, and so on.
 
C

Christopher Layne

Yevgen said:
Even if so, what is so bad about it? If one finds code with cast more
readable, then the cast is good for him (you can try to read the
thread, you'll see what I am talking about). No, it's not a pointless
thing.

Yes, it's still a "makes me feel good" touchy feely reason at this point.
There are better ways if C++ compliance is needed.
Well, see below. How about 2) anyway?

In reference to your "see below" that isn't ISO C, it's defensive programming
to C++ compilers (when you should really be using extern "C").

"For whatever one pleases" doesn't jive when you're dealing with sharing code
and/or a group environment.
Same thing again, huh? "here" means what? I can compile C code with
C++ compiler *here*, and I may want my *C* code to be compilable
with C++ compiler, now what? Come on, I am not going to try to convince
*you* that *you* want the cast. Just because I don't think so.
What I am trying to do is to convince you that *I* may want to
do that evil thing, and I am still fine doing that evil thing.
Because I use compiler flags lalala, lalala, and so on.

Cmon dude, extern "C" { }
You keep wanting to facillitate an all-emcompassing method to support what you
want to do - when you aren't even advocating the right way in the first
place.

Compiler flags to catch a malloc casting issue when you should just a: not
cast malloc, b: declare your C code as just that if you're compiling it
within a C++ environment.

Mandating -W as a way "handling" things is nothing but a goofy workaround.
 
Y

Yevgen Muntyan

Christopher said:
Do you always voraciously support the underdog, or just when the issue hits
home for you?

I don't know what "underdog" means, so I can't tell. I can tell though
that I dislike this "mainstream" thing in this newsgroup, when people
get template responses and get *flamed* when try to defend themselves,
even if they are right.
And it does concern me. The last thing I remember is

int *ptr = malloc (10 * sizeof (int));

It's declared heretic here, and you shall not do it, you must
do

int *ptr = malloc (10 * sizeof *ptr);

in any code snippet you post, otherwise you'll get bunch of senseless
replies about why the former is bad. Even if you ask about the
different line of code. It's silly, but when you post a piece of code
in this newsgroup, you have to think not only about its correctness but
also about lot of irrelevant style issues like this. Same thing
with this cast, I wrote a piece of code with such a cast just today,
and I did compile it with g++ and gcc. Happens, you know.

In situations like this I even feel some sympathy to Kenny McSomething.
He's at least an honest troll, not like many people here who insist
on "educating" everybody else and who will throw shit at you if you
refuse to be "educated".

Yevgen
 
Y

Yevgen Muntyan

Christopher said:
Yes, it's still a "makes me feel good" touchy feely reason at this point.
There are better ways if C++ compliance is needed.


In reference to your "see below" that isn't ISO C, it's defensive programming
to C++ compilers (when you should really be using extern "C").

"For whatever one pleases" doesn't jive when you're dealing with sharing code
and/or a group environment.


Cmon dude, extern "C" { }
You keep wanting to facillitate an all-emcompassing method to support what you
want to do - when you aren't even advocating the right way in the first
place.

Sorry, what are you talking about? How do you use extern "C" to make
conversion from void* to int* legal?
Compiler flags to catch a malloc casting issue

Ha! You might not know but it's not an issue at all. If you look at
the holy FAQ or something, you'll see that the issue is the missing
#include, and the cast *may* suppress the warning. I know that you
know it though, so nevermind. Let's just remember that it's not
an issue, and nobody is catching it.

when you should just a: not
cast malloc, b: declare your C code as just that if you're compiling it
within a C++ environment.

Please show an example.
Mandating -W as a way "handling" things is nothing but a goofy workaround.

I am not *mandating* anything! I am saying that *if* I use -W -Wall,
then casting return value of malloc() doesn't cause any troubles.
Do you not agree with this one exact sentence?

Yevgen
 
C

CBFalconer

Yevgen said:
Christopher Layne wrote:
.... snip ...


Same thing again, huh? "here" means what? I can compile C code
with C++ compiler *here*, and I may want my *C* code to be
compilable with C++ compiler, now what? Come on, I am not going
to try to convince *you* that *you* want the cast. Just because
I don't think so. What I am trying to do is to convince you that
*I* may want to do that evil thing, and I am still fine doing
that evil thing. Because I use compiler flags lalala, lalala,
and so on.

Do what you wish, but don't recommend bad habits here. There are
innocent newbies around, who might become perverted. That is the
major reason for jumping on such posts.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
 
C

Christopher Layne

Yevgen said:
Please show an example.


I am not *mandating* anything! I am saying that *if* I use -W -Wall,
then casting return value of malloc() doesn't cause any troubles.
Do you not agree with this one exact sentence?

Yevgen

$ cat nonsense.c
#include <stdlib.h>

int *nonsense(size_t alot)
{
int *a;

a = malloc(alot * sizeof *a);

return a;
}
$ cat alright.cpp
#include <stdlib.h>

extern "C" {
int *nonsense(size_t);
}

int main(void)
{
int *a;

a = nonsense(456);
if (a) free(a);

return 0;
}
$ gcc -W -Wall -pedantic -c nonsense.c
$ g++ -W -Wall -pedantic -c alright.cpp
$ g++ -W -Wall -pedantic -o alright nonsense.o alright.o
$ ./alright
$

But apparently some people still want to compile C code as C++. They're in C++
land and using malloc(), when they should probably be using new? Alright.
 
C

Christopher Layne

Yevgen said:
I don't know what "underdog" means, so I can't tell. I can tell though
that I dislike this "mainstream" thing in this newsgroup, when people
get template responses and get *flamed* when try to defend themselves,
even if they are right.

I'm the last person to be part of any clique here.
And it does concern me. The last thing I remember is

int *ptr = malloc (10 * sizeof (int));

It's declared heretic here, and you shall not do it, you must
do

int *ptr = malloc (10 * sizeof *ptr);

Because it promotes bad habits! That's the entire point. If you change the
type of "ptr" then you must change the sizeof as well. If you do not happen
to remember changing the sizeof (because it's not on the same line, e.g.,
you're a C coder and not just throwing malloc into the declaration AND
definition because it's a: not a typical idiom, b: you won't know how much to
allocate until later, c: you value context of use rather than "well I know
I'll probably be using this later, so let's just allocate now") in the
malloc() statement. This has bitten every single person in this newsgroup
until they learned not to use that construct.

It's like you're at a motorcycle safety school trying to argue why wheelies
should be allowed on public motorways. Nobody is going to buy it there.
In situations like this I even feel some sympathy to Kenny McSomething.
He's at least an honest troll, not like many people here who insist
on "educating" everybody else and who will throw shit at you if you
refuse to be "educated".

I agree with Kenny on other points as well, but like I said: promotes bad
habits.
 
Y

Yevgen Muntyan

Christopher said:
I'm the last person to be part of any clique here.


Because it promotes bad habits! That's the entire point. If you change the
type of "ptr" then you must change the sizeof as well.

And if you change the name, you have to change it twice. And if you look
at

ptr = malloc (10 * sizeof *ptr);

then you don't know how much is allocated, while 10*sizeof(int) means
"ten ints". And if you use spaces instead of tabs, then you're wrong
because you must use tabs.
(Oh, and if suddenly *ptr is simply wrong there, then it's an advanced
code and we are not discussing advanced situations here, sure).
If you do not happen
to remember changing the sizeof (because it's not on the same line, e.g.,
you're a C coder and not just throwing malloc into the declaration AND
definition because it's

You better don't throw malloc in your code at all. If it's strings, you
don't need sizeof. If it's anything else, you better be careful. And if
it's structures, you better isolate allocation to some special functions
(where you simply don't care what form you use).
a: not a typical idiom, b: you won't know how much to
allocate until later, c: you value context of use rather than "well I know
I'll probably be using this later, so let's just allocate now") in the
malloc() statement. This has bitten every single person in this newsgroup
until they learned not to use that construct.

The last sentence is not true. And it's not just me who wasn't bitten
by that (I simply don't use raw malloc() much though).
It's like you're at a motorcycle safety school trying to argue why wheelies
should be allowed on public motorways. Nobody is going to buy it there.

No no, not on public anything. On my own yard, my own one-wheel
motorcycle is fine because of have additional safety measures.
I agree with Kenny on other points as well, but like I said: promotes bad
habits.

What promotes bad habits? Bad habits? Okay, let it be a bad habit to
cast return value of malloc (which I agree with if it's "always
cast"). Do you say that it's fine to claim that the cast in the
following code

#include <stdlib.h>
int main (void)
{
char *foo = (char*) malloc (18);
return 0;
}

*necessarily* and *always* leads to bad results for every single person
in every single situation? Please don't say about habits, just say "yes"
or "no" (and read the thread to see what I am talking about if you're
interested).

Yevgen
 
Y

Yevgen Muntyan

CBFalconer said:
Do what you wish, but don't recommend bad habits here. There are
innocent newbies around, who might become perverted. That is the
major reason for jumping on such posts.

"Innocent newbies"! Think about it next time when you post
not strictly conforming code here. You may hurt some
newbies who can believe that's "C language as defined
by the standard"!
Seriously speaking, if one cares about newbies and wants
to prevent them from learning something bad, he must
say so, he must not say "You're wrong period". Simply
because "You're wrong period" claim is wrong, and poor
newbies who trust everyone will learn wrong things.
Good one, by the way. "Major reason for jumping on such
posts", yeah.

Yevgen
 
M

Mark McIntyre

Me: I am fine here, I make gcc take care of this.
You: No, you're wrong. anycc is not obliged to emit a warning.
Me: *I* make *gcc* take care of this *here*.
You: No, you're wrong. See above.

You: its ok, I always put the safety on.
Me: its still dangerous, safetys don't always work.
You: *I* always make sure the safety is *on*. So there.
Me: I wouldn't do -
<sfx>*BANG* </>
You: urk.
Me: oh well, Darwin in action.


--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
F

Flash Gordon

Yevgen Muntyan wrote, On 08/02/07 13:34:
And if you change the name, you have to change it twice. And if you look
at

ptr = malloc (10 * sizeof *ptr);

then you don't know how much is allocated, while 10*sizeof(int) means
"ten ints". And if you use spaces instead of tabs, then you're wrong
because you must use tabs.
(Oh, and if suddenly *ptr is simply wrong there, then it's an advanced
code and we are not discussing advanced situations here, sure).

This is more obviously a problem
newptr = malloc( 10 * sizeof *oldptr);
than
ptr = malloc( 10 * sizeof(int)

Also with most editors it is easier to do an interactive search and
replace on a variable name that a type name and get it right. Unless you
want to change all int variables to doubles!
You better don't throw malloc in your code at all. If it's strings, you
don't need sizeof. If it's anything else, you better be careful. And if
it's structures, you better isolate allocation to some special functions
(where you simply don't care what form you use).

You should not throw anything in your code. You should always place it
there with care and knowing it is needed. You should also delete
anything that is not needed for the simple reason that it is normally
easier to introduce a bug by having something you don't need that by not
having something you do. Note I am not saying absence is never a bug,
but my experience is that deleting things which are not needed on
average decreases the bug count even if you occasionally delete
something that is needed by mistake.
The last sentence is not true. And it's not just me who wasn't bitten
by that (I simply don't use raw malloc() much though).

It hasn't bitten me but then I have *never* used a cast on the value
returned by malloc in real code.
No no, not on public anything. On my own yard, my own one-wheel
motorcycle is fine because of have additional safety measures.

If you get in to the habit of doing something potentially dangerous you
are more likely to do it at a time when it really matters than if you
are not in the habit.
What promotes bad habits? Bad habits? Okay, let it be a bad habit to
cast return value of malloc (which I agree with if it's "always
cast").

Casting is a bad habit. Casting the value returned by malloc is just a
special case with additional reasons why it can hide problems that
*have* caused programs to fail to run on *modern* hardware and OSs.
> Do you say that it's fine to claim that the cast in the
following code

#include <stdlib.h>
int main (void)
{
char *foo = (char*) malloc (18);
return 0;
}

*necessarily* and *always* leads to bad results for every single person
in every single situation? Please don't say about habits, just say "yes"
or "no" (and read the thread to see what I am talking about if you're
interested).

It does not always lead to bad results any more than crossing the road
with your eyes closed always leads to bad results. However, the *habit*
of casting is always a bad habit and the habit of casting malloc is
always a bad habit.

I have seen exactly one person come up with a convincing reason why he
should cast the value returned by malloc in his specific situation. That
reason is at least in part due to commercial reality rather than
technical merit.

Note the compatibility with C++ is a bad reason because:
1) In C++ there are better methods than using malloc to get memory
2) C++ defines mechanisms to interface to C code that has been compiled
as C code.
 
Y

Yevgen Muntyan

Christopher said:
$ cat nonsense.c

[snip an irrelevant example of using g++ to compile C++ sources and
gcc to compile C sources]
But apparently some people still want to compile C code as C++. They're in C++
land and using malloc(), when they should probably be using new? Alright.

I wonder if it was "yes" or "no".
Anyway, there are cases when one wants to compile C code with C++
compiler. The most realistic example is macros in headers. Then inline
functions. Then sources which don't know about __cplusplus and extern
"C". Generated code doesn't count since it's way too advanced, right?
And if one likes to compile C code with C++ compiler to get stricter
type checking, that's a you-shall-not-do-that-just-because thing.
Anyway, C++ compiler is only one thing. You may get the cast
in C code just because it's there, e.g. in third party sources.
And you didn't fix it because you don't like to touch working code. Etc.
See, what you're saying is a reply to "Cast is always good and needed".
But nobody claimed that. And so on, and so forth.
Funny how people can talk about different things in form of dialog,
isn't it?
Perhaps I should have said that I don't think casting return value of
malloc is a good idea? Instead of not-saying which gets interpreted
as saying-the-contrary.

Yevgen
 
Y

Yevgen Muntyan

Mark said:
You: its ok, I always put the safety on.
Me: its still dangerous, safetys don't always work.
You: *I* always make sure the safety is *on*. So there.
Me: I wouldn't do -
<sfx>*BANG* </>
You: urk.
Me: oh well, Darwin in action.

Indeed, code tends to break when you don't touch it. You
put cast in, it explodes in two months.
What you're all saying is why it's not a good idea to
use cast. And that's indeed true, it's not a good idea
to cast when you don't have to. What's not true
is that any given piece of code with cast is wrong and
dangerous in any situation.

Yevgen
 
Y

Yevgen Muntyan

Flash said:
Yevgen Muntyan wrote, On 08/02/07 13:34:

This is more obviously a problem
newptr = malloc( 10 * sizeof *oldptr);
than
ptr = malloc( 10 * sizeof(int)

Also with most editors it is easier to do an interactive search and
replace on a variable name that a type name and get it right. Unless you
want to change all int variables to doubles!

If you use editor's search to modify code then you'll get errors,
just because this edit is that complex. And you'll get errors
unrelated to malloc() ;)
You should not throw anything in your code. You should always place it
there with care and knowing it is needed. You should also delete
anything that is not needed for the simple reason that it is normally
easier to introduce a bug by having something you don't need that by not
having something you do. Note I am not saying absence is never a bug,
but my experience is that deleting things which are not needed on
average decreases the bug count even if you occasionally delete
something that is needed by mistake.

It hasn't bitten me but then I have *never* used a cast on the value
returned by malloc in real code.


If you get in to the habit of doing something potentially dangerous you
are more likely to do it at a time when it really matters than if you
are not in the habit.

Casting is a bad habit. Casting the value returned by malloc is just a
special case with additional reasons why it can hide problems that
*have* caused programs to fail to run on *modern* hardware and OSs.


It does not always lead to bad results any more than crossing the road
with your eyes closed always leads to bad results.

True statement, just as true as that jumping on the ground is no more
dangerous than jumping off a skyscraper. And so nicely makes it sound
as dangerous even though it doesn't explicitely say so.
However, the *habit*
of casting is always a bad habit and the habit of casting malloc is
always a bad habit.

Yes, such a *habit* is bad. Did I tell it's not?
I have seen exactly one person come up with a convincing reason why he
should cast the value returned by malloc in his specific situation. That
reason is at least in part due to commercial reality rather than
technical merit.

Note the compatibility with C++ is a bad reason because:
1) In C++ there are better methods than using malloc to get memory

You lose compatibility with C then ;)
2) C++ defines mechanisms to interface to C code that has been compiled
as C code.

And I am talking about compiling *C* code with *C++* compiler.

Yevgen
 
M

matevzb

I'm the last person to be part of any clique here.





Because it promotes bad habits! That's the entire point. If you change the
type of "ptr" then you must change the sizeof as well. If you do not happen
to remember changing the sizeof (because it's not on the same line, e.g.,
you're a C coder and not just throwing malloc into the declaration AND
definition because it's a: not a typical idiom, b: you won't know how much to
allocate until later, c: you value context of use rather than "well I know
I'll probably be using this later, so let's just allocate now") in the
malloc() statement. This has bitten every single person in this newsgroup
until they learned not to use that construct.
I tend to disagree on this one. If you change the type of "ptr" you
*should* examine *all* the places where "ptr" was used anyhow (not
just the line with malloc). If you don't, that's promoting bad habits
and that may bite harder than the x*sizeof( said:
It's like you're at a motorcycle safety school trying to argue why wheelies
should be allowed on public motorways. Nobody is going to buy it there.
Nonsense.
 
M

Mark McIntyre

Indeed, code tends to break when you don't touch it.

I assume you got the point, and are merely trying to avoid it. :)
You put cast in, it explodes in two months.

Sure, if during that 2 months, you upgraded your compiler, OS or
target infrastructure.
What you're all saying is why it's not a good idea to
use cast. And that's indeed true, it's not a good idea
to cast when you don't have to.

And malloc is a case where you don't have to.
What's not true
is that any given piece of code with cast is wrong and
dangerous in any situation.

Nobody ever said that.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 

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
473,781
Messages
2,569,615
Members
45,294
Latest member
LandonPigo

Latest Threads

Top