Undefined behavior?

A

Andrew Clark

Hi all,

Someone brought this up in a group I read on Yahoo!

char *p, *q;
p = malloc ( 16 );
if ( p ) {
q = p;
p = NULL;
free ( q );
}

Will this invoke UB?

Andrew
 
B

Ben Pfaff

Andrew Clark said:
char *p, *q;
p = malloc ( 16 );
if ( p ) {
q = p;
p = NULL;
free ( q );
}

Will this invoke UB?

No. There is no reason that it should.
 
P

pete

Andrew said:
Hi all,

Someone brought this up in a group I read on Yahoo!

char *p, *q;
p = malloc ( 16 );
if ( p ) {
q = p;
p = NULL;
free ( q );
}

Will this invoke UB?

No.
The pointer value returned by malloc,
is the pointer value which is freed.
 
A

Artie Gold

Andrew said:
Hi all,

Someone brought this up in a group I read on Yahoo!

char *p, *q;
p = malloc ( 16 );
if ( p ) {
q = p;
p = NULL;
free ( q );
}

Will this invoke UB?

Well yes, but only because of your failure to:

#include <stdlib.h>

(or, at least we can't see it (-: )

Other than that, all is well.

HTH,
--ag
 
E

E. Robert Tisdale

Andrew said:
Someone brought this up in a group I read on Yahoo!

char* p = (char*)malloc(16);
if (NULL != p) {
char* q = p;
p = NULL;
free(q);
}

Will this invoke undefined behavior?

No. Neither will this

char* p = (char*)malloc(16);
char* q = p;
p = NULL;
free(q);
 
A

Andrew Clark

Well yes, but only because of your failure to:

#include <stdlib.h>

(or, at least we can't see it (-: )

Other than that, all is well.

HTH,
--ag

Well, for my snippet I assumed that ;-)

Andrew
 
N

nrk

This is downright malicious. You're pretending to quote someone else, but
have gratuitously added a cast where none exists in the original, apart
from making other unnecessary and unmarked changes. Please don't attribute
your words to someone else. In real life it would be called forgery, and
is a felony.

-nrk.
 
T

The Real OS/2 Guy

No. Neither will this

Wrong. It occures undefined behavior when you forget to include
stdlib.h. You hides the diagnostic saying that you tries to convert
int to a pointer throu casting the value the compiler things it were
int to a pointer.
char* p = (char*)malloc(16);

The same bug as above - resulting in undefined behavior.
char* q = p;
p = NULL;
free(q);

That's fine - but lets q hold an indetermined value (the pointer in it
points to somewhere but not to an address guilty for your program)
after free.
 
C

Christopher Benson-Manica

This is downright malicious.

From Tisdale?? Never! I propose, actually, that a section of the FAQ
be devoted to identifying known trolls and such (21.1 - "Who is E.
Robert Tisdale?") for the benefit of those who might otherwise be
seduced by their wiles... 21.2 should be "What is a killfile?" ;)
 
C

CBFalconer

nrk said:
.... snip Trollsdale stuff ...

This is downright malicious. You're pretending to quote someone
else, but have gratuitously added a cast where none exists in the
original, apart from making other unnecessary and unmarked changes.
Please don't attribute your words to someone else. In real life it
would be called forgery, and is a felony.

This is just standard procedure for ERTisdale, better known as
Trollsdale. He has made himself almost as obnoxious as possible
in his inimitably ignorant way.
 
J

John Bode

^^^^^^^

This cast does not appear in the original article you are quoting.
Why did you add it here?
 
J

John Bode

Andrew Clark said:
Hi all,

Someone brought this up in a group I read on Yahoo!

char *p, *q;
p = malloc ( 16 );
if ( p ) {
q = p;
p = NULL;
free ( q );
}

Will this invoke UB?

Assuming the prototype for malloc() and free() are in scope (i.e.,
you've #included stdlib.h), then no. p and q are separate entities;
modifying p has no effect on q.
 
N

nrk

CBFalconer said:
This is just standard procedure for ERTisdale, better known as
Trollsdale. He has made himself almost as obnoxious as possible
in his inimitably ignorant way.

I can see now, why many regulars here are on ERT's case. In all likelihood,
I just haven't paid enough attention to his past posts where he has done
this egregious thing of editing the original post without marking his
changes. IMO this is worse than a troll, and probably the sneakiest thing
I've seen on usenet. If this is standard procedure for him, then plonking
is not an option, as one would have to point out to others who like me
might make the mistake of taking ERT's quotes of an OP as authentic.

-nrk.
 
E

E. Robert Tisdale

John said:
^^^^^^^
This cast does not appear in the original article you are quoting.

I am *not* quoting Andrew Clark.
Why did you add it here?

I am paraphrasing Andrew Clark
to reflect *my* understanding of what Andrew Clark meant.
Consult the comp.lang.c newsgroup archives:

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&group=comp.lang.c

if you want to know exactly what Andrew Clark wrote.

Don't *quote* previous contributors when you respond
to usenet newsgroup articles. Don't "top post".
Snip away everything that is irrelevant
to the point that you are addressing. Don't complain
about spelling, capitalization, punctuation, grammar or style.
Just ignore it or quietly fix it if it bugs you.
 
N

nrk

No, he most definitely did not write most of what followed below.
^^^^^^^

This cast does not appear in the original article you are quoting.
Why did you add it here?

That is not the only unmarked change he made to the original article. Note
the position of the '*' in char *, the difference in the variable
declarations, and also the condition in the if.

-nrk.
 
C

Chris Torek

Why did you change my original indentation?

I do not believe that he (specifically) did. I went back to the
referenced article, saved it into a file, and viewed that; and
discovered that the text delivered by my news server contained
ASCII horizontal-tab characters ('\t', code 0x09). The software
I use is not defective and retains such characters; the Newsguy
servers' software retains them; but the software used on some
systems -- readers and/or servers; which ones I am not sure -- *is*
defective and simply deletes them entirely.

In response to this problem, I modified my own news-posting software
(the Pnews and Rnmail scripts that come with trn) to expand tabs
into an appropriate number of space by default, adding an option
to suppress this. In other words, *I* use tabs, but I now make
sure that the text I write that goes to Usenet does *not*, because
too many readers and/or servers are broken and there is a simple
workaround. This is similar to the reason I do not use the word
"begin" followed by two blanks at the beginning of a line -- at
least one news reader (Outlook Express) misinterprets it. (Of course,
begin followed by multiple blanks could occur entirely naturally,
if one were careless about whitespace in text, so those who use
this defective newsreader are doing themselves a disservice. But
it is less likely than ASCII tabs.)
 
C

Christian Bau

"E. Robert Tisdale said:
I am *not* quoting Andrew Clark.


I am paraphrasing Andrew Clark
to reflect *my* understanding of what Andrew Clark meant.
Consult the comp.lang.c newsgroup archives:

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&group=comp.lang.c

if you want to know exactly what Andrew Clark wrote.

Not only sneaky and a coward, but also insulting our intelligence.

In addition, you didn't "paraphrase" his code, you replaced fine, decent
C code with absolute crap.
 
F

Fao, Sean

E. Robert Tisdale said:
I am *not* quoting Andrew Clark.


I am paraphrasing Andrew Clark
to reflect *my* understanding of what Andrew Clark meant.

You call that paraphrasing? You took perfectly good code and turned it
into crap by introducing the possibility of undefined behavior and
stated it in a way that would lead Andrew into believing it were safe.
 
K

Keith Thompson

E. Robert Tisdale said:
I am *not* quoting Andrew Clark.


I am paraphrasing Andrew Clark
to reflect *my* understanding of what Andrew Clark meant.
[...]

Mr. Tisdale, here's an exact excerpt from your article, Message-ID
<[email protected]>. To avoid confusion, I'll enclose the
quoted material without added indentation between rows of '=' signs.
This excerpt is the whole of what you posted, excluding headers and
leading and trailing blank lines.

========================================================================
Andrew said:
Someone brought this up in a group I read on Yahoo!

char* p = (char*)malloc(16);
if (NULL != p) {
char* q = p;
p = NULL;
free(q);
}

Will this invoke undefined behavior?

No. Neither will this

char* p = (char*)malloc(16);
char* q = p;
p = NULL;
free(q);
========================================================================

You clearly stated that "Andrew Clark wrote" the material prefixed
with "> " markers. Even if you hadn't actually stated that, it's
clearly implied by the quoting syntax you used and by Usenet
conventions that it was an exact quote. In no way did you state or
imply that you were paraphrasing what Andrew Clark wrote.

(There are C style issues here as well (in my opinion Andrew Clark's
original code was superior to your so-called "paraphrase" of it), but
that's beside the point.)

You need to clarify this, and you need to clearly indicate in the
future whether you're quoting or paraphrasing what others have
written.
 

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,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top