When to check the return value of malloc

P

Phil Carmody

Keith Thompson said:
Phil Carmody said:
Keith Thompson said:
[...]
The main problem was that he wanted a "?:=" version of gcc's "?:"
operator. If he'd have actually wanted "? :", it wouldn't have been
so bad to use "? :" (I like dense code, I don't like redundant code).

And as an aside, I think ?: should be standardised.

(And please overlook the syntactical sloppiness, I didn't want to have
to explain ?:)

Since you're talking about a gcc-specific extension (and since C already
has a ?: operator), perhaps explaining it would have been a good idea.

Nope, my time's too valuable to me currently. Anyone interested enough
in gcc specifics had all the clues available to them in order to stick
them into a search engine.

I happened to have read about about gcc's extension of the
conditional operator previously, so I knew what you were talking
about. Someone who didn't know about it would assume that ?:
referred to the operator defined by the C standard (and would
wonder what you meant by ``gcc's "?:"'' and why you think it should
be standardized).

When some of your valuable time becomes available, try sticking "?:"
into a search engine and see what happens.

Why would I stick punctuation into a text-based search engine?
Do you think I'm the kind of idiot who would waste time doing
that? If I was looking for something clearly identified as a
gcc c language extension, I'd probably search for something like
"gcc c language extension", for example. Maybe the first hit
would even contain the string '?:', if I was lucky.

Phil
 
K

Keith Thompson

Phil Carmody said:
Frig, it was there in black and white all along.

What was the final score?

Observation: Some trolls post through aioe.org.

Conclusion: All aioe.org users are trolls?

Nonsense. I'm a former aioe.org user myself (I later switched to
eternal-september.org because it seemed, at the time, to be more
stable).
 
K

Keith Thompson

Seebs said:
Speaking of GNU C: Nested functions, worst idea ever. I have never seen
them used in a remotely sane way. I did, however, just encounter a provably
insane nested function which Made No Sense At All. Also it caused crashes
on the ARM compiler. But mostly it was just insane.

It is the Flying Dutchman of C language extensions, a solution doomed to
wander the libraries and source trees of the world until the end of the time,
seeking a problem.

Interesting. I've certainly made extensive use of nested functions
(and/or procedures) in Pascal and Ada. Perhaps there's something
about C (other than the obvious fact that the standard doesn't
support them) that makes nested functions less useful?
 
T

Tim Harig

The occasional counter-examples don't do much to undermine the general
principle. aioe exists to allow people to harass with impunity.

Since ISPs have starting dropping usenet support, I have had little choice.
I cannot justify the costs a paid service in addition to my other
telecommunications costs.
 
K

Keith Thompson

Phil Carmody said:
Keith Thompson said:
Phil Carmody said:
[...]
The main problem was that he wanted a "?:=" version of gcc's "?:"
operator. If he'd have actually wanted "? :", it wouldn't have been
so bad to use "? :" (I like dense code, I don't like redundant code).

And as an aside, I think ?: should be standardised.

(And please overlook the syntactical sloppiness, I didn't want to have
to explain ?:)

Since you're talking about a gcc-specific extension (and since C already
has a ?: operator), perhaps explaining it would have been a good idea.

Nope, my time's too valuable to me currently. Anyone interested enough
in gcc specifics had all the clues available to them in order to stick
them into a search engine.

I happened to have read about about gcc's extension of the
conditional operator previously, so I knew what you were talking
about. Someone who didn't know about it would assume that ?:
referred to the operator defined by the C standard (and would
wonder what you meant by ``gcc's "?:"'' and why you think it should
be standardized).

When some of your valuable time becomes available, try sticking "?:"
into a search engine and see what happens.

Why would I stick punctuation into a text-based search engine?
Do you think I'm the kind of idiot who would waste time doing
that? If I was looking for something clearly identified as a
gcc c language extension, I'd probably search for something like
"gcc c language extension", for example. Maybe the first hit
would even contain the string '?:', if I was lucky.

The point is that it wasn't at all clear from your previous article
what you were talking about. I barely figured it out myself,
and Seebs (who is no idiot) didn't understand it at all until I
explained it.

As you know, ?: is a standard C language feature. The phrase ``gcc's
"?:"'' is not, in my opinion, sufficient to make it clear that you're
talking about an extension. Plenty of people are unclear on the
distinction between features defined by the language and features
supported by a particular compiler; I don't think you're one of them,
but it's unreasonable to expect everyone to be aware of that.

Please spend just a moment considering the possibility that I might
have a point.
 
K

Keith Thompson

Seebs said:
The occasional counter-examples don't do much to undermine the general
principle. aioe exists to allow people to harass with impunity.

You are making an unwarranted assumption about the purpose behind
aioe.org, and an equally unwarranted assumption that aioe.org users
are necessarily aware of that purpose.

I used aioe.org myself for some time, simply because it was a
prominent free NNTP server. A quick look at aioe.org's web site
seems to imply that they have reasonable terms of service and
complaint policies. They might not be very good at handling
complaints, but I don't recall seeing any direct evidence of
that myself.
 
B

bart.c

Geoff said:
Programming is about correctness of the solution to a problem, not
about terseness. To obtain correctness and ease of maintenance the
programmer should write it as though someone unfamiliar with the
solution will be maintaining the code. The compiler will optimize the
code. Yes, there are occasions when the solution has bottlenecks, but
these are conceptual problems with the implementation of the solution,
not the size of the code.


Simply false. As Tim stated above, if/else generates essentially the
same machine code as ?: yet the latter is harder to read and maintain.

Does it?

x = a?b:c would need to be written like this:

if (a)
x=b;
else
x=c;

Notice the x is written twice. This may or may not generate the same code,
but when x is complex it's more work to write.

And when ?: is properly embedded in an expression:

f( p+(a?b:c)+q*(d?e:g))

then I'd really like to see your version using if/else.
 
S

Seebs

Interesting. I've certainly made extensive use of nested functions
(and/or procedures) in Pascal and Ada. Perhaps there's something
about C (other than the obvious fact that the standard doesn't
support them) that makes nested functions less useful?

I don't know. The usage in question was as follows:

There's a function in glibc called rpmatch(), which basically tells
you whether a provided string looks like "yes" or "no". To do this,
it has a nested function called try().

/* Determine whether string value is affirmation or negative response
according to current locale's data.
This file is part of the GNU C Library.
Copyright (C) 1996, 1997, 2000, 2003 Free Software Foundation, Inc.

The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */

#include <langinfo.h>
#include <stdlib.h>
#include <regex.h>


int
rpmatch (response)
const char *response;
{
/* Match against one of the response patterns, compiling the pattern
first if necessary. */
auto int try (const int tag, const int match, const int nomatch,
const char **lastp, regex_t *re);

int try (const int tag, const int match, const int nomatch,
const char **lastp, regex_t *re)
{
const char *pattern = nl_langinfo (tag);
if (pattern != *lastp)
{
/* The pattern has changed. */
if (*lastp)
{
/* Free the old compiled pattern. */
__regfree (re);
*lastp = NULL;
}
/* Compile the pattern and cache it for future runs. */
if (__regcomp (re, pattern, REG_EXTENDED) != 0)
return -1;
*lastp = pattern;
}

/* Try the pattern. */
return __regexec (re, response, 0, NULL, 0) == 0 ? match : nomatch;
}

/* We cache the response patterns and compiled regexps here. */
static const char *yesexpr, *noexpr;
static regex_t yesre, nore;

return (try (YESEXPR, 1, 0, &yesexpr, &yesre) ?:
try (NOEXPR, 0, -1, &noexpr, &nore));
}

A bit of analysis:

It does make some sense to create a function for the "create a new
regex for this pattern, if we don't have one yet already". (The reason
for the elaborate dance to figure out whether the pattern is the same,
I think, is in case the locale changes after you've previously called this.)

It's a nested function because it wants to check the value of "response",
and apparently, while it's awesome to have a function that takes five
parameter, including one for what it should return on a match and one
for what it should return on a non-match, it is ridiculous to have a function
that takes a sixth parameter.

Had I been writing this, it would likely have been:

static int
check_response(const int tag,
const char **lastp,
const regex_t *re,
const char **response) {
/* return 1 on a match */
}

and been invoked as:
if (check_response(YESEXPR, &yesexpr, &yesre, response)) {
return 1;
} else if (check_response(NOEXPR, &noexpr, &nore, response)) {
return 0;
} else {
return -1;
}

It is amusing to me to note that the GNU C "?:" was used here. (And to
good effect, even!)

Basically, though, this is a great example of the same kind of insane
pseudo-cleverness that sandeep's example code has been such a rich source
of. The nested function is not particularly necessary. The elaborate
match/nomatch returns create an undue complication, to put it mildly.

I'm sure, somewhere, there must be legitimate uses for this extension, but
I've never seen it for anything but stuff like the above, which exists only
to be a weakest link in some piece of software with some set of compiler
options, because it's so rarely used.

-s
 
S

Seebs

You are making an unwarranted assumption about the purpose behind
aioe.org, and an equally unwarranted assumption that aioe.org users
are necessarily aware of that purpose.

I'm making an observation about the net effect it has.
I used aioe.org myself for some time, simply because it was a
prominent free NNTP server. A quick look at aioe.org's web site
seems to imply that they have reasonable terms of service and
complaint policies.

Their complaint policy, for any kind of harassment, is that they will do
NOTHING without an order signed by an Italian judge.

So if I were harassing you through aioe.org, your options would be:
1. Put up with it.

You can't, in practice, get an Italian judge to care about internet
harassment occurring between two people neither of whom is in Italy.
They might not be very good at handling
complaints, but I don't recall seeing any direct evidence of
that myself.

They are the news server of choice for stalkers these days, because they
provide anonymizing (no injection IP or anything) and will not cooperate
with any kind of investigation that isn't in Italy.

They're not too bad about spam, which is nice, but they are a magnet for
stalkers, and don't seem likely to change this in the forseeable future.

And you're right, individual users may well be exceptions to that. It
could even be that a majority of their users are harmless, but the ones
who aren't are pretty bad, and the aioe policy pages look to me to
be expressing derision and contempt for the victims of harassment through
their servers.

-s
 
K

Keith Thompson

Richard Heathfield said:
Are you being deliberately obtuse, or is this genuinely a maze of twisty
little misunderstandings, all different?

I suspect the latter.

Richard, you introduced the word "problem" into the conversation.
What exactly did you mean? Consider that Phil was not the only
person who was bewildered by your question.

In my opinion, Phil simply asked a reasonable question to which
the answer was not entirely obvisou.
 
S

Seebs

It was phrased as an assumption about their purpose: "aioe exists to
allow people to harass with impunity".

Ahh.

Sorry, badly phrased. That's meant in the same way that "teenage girls
exist to keep Sanrio in business".
Are you referring to the "Log Data Retention" section on
<http://aioe.org/19.html>? That's the only thing I can see that
refers to Italian court orders, and it refers only to revealing
their logs. Harassment should be covered by the last bullet in
"Content-Related Rules" on <http://aioe.org/03.html>, "Respect
netiquette.".

Okay. So, imagine that someone is harassing you to a sufficient extent
that courts would normally become involved.

Unless you're Italian, you're screwed. You can never get them to disclose
the posting IP address, without which there's nothing you can do but
speculate as to who might be doing it.
2. Follow the complaint procedure documented on aioe.org's web page,
which says nothing about court orders.

But also says nothing about any likelihood that they'll do anything.
I'm not debating whether they're a haven for harassers, just whether
there's any real indication of it on their web pages.

NNTP-posting-host is not optional, IMHO. Hiding it, and refusing to
disclose it without a court order from a judge is an attractive nuisance
and an invitation to stalkers and harassers.

-s
 
K

Keith Thompson

Seebs said:
I'm making an observation about the net effect it has.

It was phrased as an assumption about their purpose: "aioe exists to
allow people to harass with impunity".
Their complaint policy, for any kind of harassment, is that they will do
NOTHING without an order signed by an Italian judge.

Are you referring to the "Log Data Retention" section on
<http://aioe.org/19.html>? That's the only thing I can see that
refers to Italian court orders, and it refers only to revealing
their logs. Harassment should be covered by the last bullet in
"Respect said:
So if I were harassing you through aioe.org, your options would be:
1. Put up with it.

2. Follow the complaint procedure documented on aioe.org's web page,
which says nothing about court orders.

Again, I can't confirm from my own experience that that actually pay
attention, but I see no statement of policy that says they won't.

I'm not debating whether they're a haven for harassers, just whether
there's any real indication of it on their web pages.

[...]
 
K

Keith Thompson

William Ahern said:
That's a poor example. It's unreasonable as a practical matter to write two
sort routines just to recover gracefully from a malloc failure, not to
mention the possibility of opening oneself up to a computational complexity
attack. The scenarios where writing two routines is sensible should be
extremely limited.

In this particular case, the better approach involves an ounce of
prevention. I never use data structure implementations which require a
separately allocated node object, primarily because it greatly simplifies
memory management. Making the node a member of a structure is simple enough.

Algorithms which require a dynamically sized array or stack is a different
story, but all things being equal (or nearly so), I tend to prefer
algorithms where that isn't necessary. For example, I always choose to use a
red-black tree over an AVL tree.

Suppose you want to sort a potentially large disk file. Whatever
algorithm you use, it's likely to involve loading chucks of the file
into memory. The more you can load, the faster the sort is likely to
be. In that particular case, it's perfectly reasonable to ask for a
large buffer and settle for a smaller one.

But that's a relatively rare special case, and it doesn't justify
making your default memory allocator allocate a small buffer when
you ask for a large one.
 
K

Keith Thompson

Seebs said:
Okay. So, imagine that someone is harassing you to a sufficient extent
that courts would normally become involved.

Unless you're Italian, you're screwed. You can never get them to disclose
the posting IP address, without which there's nothing you can do but
speculate as to who might be doing it.

If they follow the policies stated on their own web pages, they should
at least shut down the harassing user's access to aioe.org. I concede
that that's a big "if".

[...]
 
D

Dennis \(Icarus\)

Phil Carmody said:
All that stuff that's just done for you in C++ so you don't have to
worry about may be the stuff that you don't want to happen. Whilst
it's certainly possible, I think you'd have to go a little further
out of your way to get such behaviour in C, you would have to
deliberately design it in more.

Not really - just declare a struct, then call a function to initialize it.
Doesn't seem to be that far out of the way.
However, thanks for sharing your C++ woes on comp.lang.c.

The behavior described could happen in any language. Still, you're welcome.

Dennis
 
N

Nick Keighley

I even KNOW about it, and didn't realize that was the intended reference.

Speaking of GNU C:  Nested functions, worst idea ever.  I have never seen
them used in a remotely sane way.  I did, however, just encounter a provably
insane nested function which Made No Sense At All.  Also it caused crashes
on the ARM compiler.  But mostly it was just insane.

It is the Flying Dutchman of C language extensions, a solution doomed to
wander the libraries and source trees of the world until the end of the time,
seeking a problem.

plenty of other languages implement nested function definitions. Why
are functions so special that everything else can be nested but them?
Its another locality of reference thingy
 
P

Phil Carmody

Keith Thompson said:
Observation: Some trolls post through aioe.org.

Conclusion: All aioe.org users are trolls?

Nonsense.

Indeed - and entirely from your pen!

It's /evidence/ that is as clear as day, which, in combination with the
fact that his content is becoming more indistinguishable from a troll's,
is quite convincing; I never claimed it was /proof/. I may even be wrong.

Phil
 
P

Phil Carmody

Richard Heathfield said:
Are you being deliberately obtuse, or is this genuinely a maze of
twisty little misunderstandings, all different?

One might say my understanding has been almost preternatural.
I correctly worked out what language he was using, a language
to which this newsgroup is not dedicated, despite the fact
that he never explicitly stated it.

Phil
 
P

Phil Carmody

bart.c said:
And when ?: is properly embedded in an expression:

f( p+(a?b:c)+q*(d?e:g))

then I'd really like to see your version using if/else.

Why? If the thing that makes the line unreadable is the
fact that it's *embedded in an expression*, then the solution
is to simply *not embed it in an expression*.

int base_foo = a?b:c; /* or ``a ? b : c'' if you like airy code */
int scaled_foo = d?e:g;
f(p + base_foo + q*scaled_foo);

There's nothing intrinsically less readable about the ternary ? :
operator than multiple assignments in an if/else.

Phil
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top