malloc vs calloc

B

Barry Schwarz

I find it at some of the goole group.

The internet is full of information, some useful, some less so, and
some downright incorrect.


Remove del for email
 
R

Richard

That's their problem.

It is your problem actually since you seem to have the issue.
It _used_ to be considered merely polite to read both a newsgroup's FAQ
and a decent amount of existing posts before posting your first question
on a newsgroup. In this group, for this error, one or two weeks' backlog
would have been more than enough not to make it. But mere politeness is
no longer the done thing; these days, even on Usenet, instant
gratification and a complete lack of one's own intelligent initiative
are /de rigeur/. 't is a pity, but there it is.

This is a help NG. People come for help. It is not de rigeur to read and
LEARN and REMEMBER a full archive of information first. If that were the
case then this NG would not be required.

As I said, if he didnt know that the cast was his issue how can he know
to look it up?

If you dont want to help, then dont.
 
C

Chris Dollin

Joachim said:
a) as in "possibly not portable"

/Definitely/ not portable.
b) as portable as the P of POSIX...

I've written code on non-POSIX systems. If I dearchived my trusty
RISC PC, I would want to write C code that ran on it.

/If/ you want to restrict yourself to POSIX-compliant systems, that's
fine. I might make that choice for future code I write. (EG if I did
dynamic loading.)
 
L

Laurent Deniau

Clark said:
Because it assumes that any allocation of zero bytes is a failure, and,
presumably, won't be free'd.

right, just reverse the test

if (!x && n) {
// failure
}

a+, ld.
 
G

Guest

[ x = malloc(n * sizeof *x); ]
Because it assumes that any allocation of zero bytes is a failure, and,
presumably, won't be free'd.

No, it assumes that any allocation of zero bytes succeeds, even if
NULL is returned.
 
G

Guest

Laurent said:
right, just reverse the test

if (!x && n) {
// failure
}

When evaluating x and n has no side effects (and in this case, there
are no side effects), !x && n and n && !x are equivalent.
 
R

Richard Heathfield

Richard said:

This is a help NG.

No, it isn't. It's a newsgroup for discussing C programming. We do give
help, but that is not the primary purpose of the group.
People come for help. It is not de rigeur to read
and LEARN and REMEMBER a full archive of information first.

Partly true, although see below for amplification of this point. It is,
however, de rigeur to learn enough about the group that one can take
part in it productively. It is not productive to jar off the people
from whom one seeks help, by flouting local custom and practice. The
customs of this group are not arbitrary. They have been developed over
time because they *work*.
If that were the case then this NG would not be required.

If you don't want to use it, you don't have to. Nobody is forcing you
to.
As I said, if he didnt know that the cast was his issue how can he
know to look it up?

The issue comes up frequently here. All he had to do was read the feed
(a search on malloc would have given him a great many hits). It is
customary, before participating in a newsgroup, to read enough of the
group to get an idea of how the group works. Since you can't chuck a
brick in this newsgroup without hitting a couple of dozen articles
explaining why malloc-casting is a bad idea, nobody should be in any
doubt about the matter by the time they come to post.
If you dont want to help, then dont.

We do want to help (although we are not obliged to, and don't get paid
for doing so). But clc gives the help people *need*, not necessarily
the help they *want*.
 
C

Clark S. Cox III

Laurent said:
right, just reverse the test

if (!x && n) {
// failure
}

That changes nothing. (as long as x and n have no side effects, as is
the case here).
 

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,776
Messages
2,569,602
Members
45,182
Latest member
BettinaPol

Latest Threads

Top