By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,851 Members | 2,097 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,851 IT Pros & Developers. It's quick & easy.

Undefined behavior?

P: n/a
*** post for FREE via your newsreader at post.newsfeed.com ***

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
-----= Posted via Newsfeed.Com, Uncensored Usenet News =-----
http://www.newsfeed.com - The #1 Newsgroup Service in the World!
-----== 100,000 Groups! - 19 Servers! - Unlimited Download! =-----

Nov 14 '05 #1
Share this Question
Share on Google+
24 Replies


P: n/a
Andrew Clark <an*****@syr.edu> writes:
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.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Nov 14 '05 #2

P: n/a
Andrew Clark wrote:

*** post for FREE via your newsreader at post.newsfeed.com ***

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.

--
pete
Nov 14 '05 #3

P: n/a
Andrew Clark wrote:
*** post for FREE via your newsreader at post.newsfeed.com ***

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
--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.

Nov 14 '05 #4

P: n/a
Andrew Clark wrote:
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);

Nov 14 '05 #5

P: n/a
*** post for FREE via your newsreader at post.newsfeed.com ***

Artie Gold <ar*******@austin.rr.com> wrote in news:3FE64536.8090902
@austin.rr.com:
Andrew Clark wrote:
*** post for FREE via your newsreader at post.newsfeed.com ***

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


Well, for my snippet I assumed that ;-)

Andrew
-----= Posted via Newsfeed.Com, Uncensored Usenet News =-----
http://www.newsfeed.com - The #1 Newsgroup Service in the World!
-----== 100,000 Groups! - 19 Servers! - Unlimited Download! =-----

Nov 14 '05 #6

P: n/a
nrk
E. Robert Tisdale wrote:
Andrew Clark wrote:
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?

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.

No. Neither will this

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

Nov 14 '05 #7

P: n/a
On Mon, 22 Dec 2003 01:38:32 UTC, "E. Robert Tisdale"
<E.**************@jpl.nasa.gov> wrote:
Andrew Clark wrote:
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


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.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation

Nov 14 '05 #8

P: n/a
nrk <ra*********@devnull.verizon.net> spoke thus:
E. Robert Tisdale wrote:

char* p = (char*)malloc(16);
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?" ;)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #9

P: n/a
nrk wrote:
.... 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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 14 '05 #10

P: n/a
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message news:<3F**************@jpl.nasa.gov>...
Andrew Clark wrote:
Someone brought this up in a group I read on Yahoo!

char* p = (char*)malloc(16); ^^^^^^^

This cast does not appear in the original article you are quoting.
Why did you add it here?
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);

Nov 14 '05 #11

P: n/a
Andrew Clark <an*****@syr.edu> wrote in message news:<Xn*********************@208.33.61.211>...
*** post for FREE via your newsreader at post.newsfeed.com ***

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.
Andrew
-----= Posted via Newsfeed.Com, Uncensored Usenet News =-----
http://www.newsfeed.com - The #1 Newsgroup Service in the World!
-----== 100,000 Groups! - 19 Servers! - Unlimited Download! =-----

Nov 14 '05 #12

P: n/a
nrk
CBFalconer wrote:
nrk wrote:

... 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.


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.
Nov 14 '05 #13

P: n/a
nrk
John Bode wrote:
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:<3F**************@jpl.nasa.gov>...
Andrew Clark wrote:
No, he most definitely did not write most of what followed below.
> Someone brought this up in a group I read on Yahoo!
>
> char* p = (char*)malloc(16);

^^^^^^^

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.
> 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);


Nov 14 '05 #14

P: n/a
John Bode wrote:
E. Robert Tisdale wrote:
Andrew Clark wrote:
Someone brought this up in a group I read on Yahoo!

char* p = (char*)malloc(16);

^^^^^^^
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=e...up=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.

Nov 14 '05 #15

P: n/a
Something that calls itself nrk wrote:
if (NULL != p) {
char* q = p;
p = NULL;
free(q);
}


Why did you change my original indentation?

Nov 14 '05 #16

P: n/a
In article <news:3F**************@jpl.nasa.gov>
E. Robert Tisdale <E.**************@jpl.nasa.gov> writes:
Something that calls itself nrk wrote:
>if (NULL != p) {
>char* q = p;
>p = NULL;
>free(q);
>}


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.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #17

P: n/a
In article <3F**************@jpl.nasa.gov>,
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote:
John Bode wrote:
E. Robert Tisdale wrote:
Andrew Clark wrote:

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

char* p = (char*)malloc(16);

^^^^^^^
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=e...up=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.
Nov 14 '05 #18

P: n/a
E. Robert Tisdale wrote:
John Bode wrote:
E. Robert Tisdale wrote:
Andrew Clark wrote:

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

char* p = (char*)malloc(16);

^^^^^^^
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.


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.

Nov 14 '05 #19

P: n/a
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> writes:
John Bode wrote:
E. Robert Tisdale wrote:
Andrew Clark wrote:

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

char* p = (char*)malloc(16);
^^^^^^^
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.

[...]

Mr. Tisdale, here's an exact excerpt from your article, Message-ID
<3F**************@jpl.nasa.gov>. 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 Clark wrote:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
(Note new e-mail address)
Nov 14 '05 #20

P: n/a
nrk
Chris Torek wrote:
In article <news:3F**************@jpl.nasa.gov>
E. Robert Tisdale <E.**************@jpl.nasa.gov> writes:
Something that calls itself nrk wrote:
>>if (NULL != p) {
>>char* q = p;
>>p = NULL;
>>free(q);
>>}
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.


You're right. Definitely not intentional, but guilty as charged
nevertheless. In this particular case, the server correctly delivers the
tab characters, and my reader correctly handles them *after* I configure it
to do so. My mistake was that I didn't take the time to go through the
settings to make sure that quoted text was not automatically rewrapped,
something that results in tabs being removed entirely from quoted text in
my reader.
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.)


My reader does this as well. If I use tabs, they are converted to spaces
before the post is sent out. ERT has done his good deed for the day by
getting me to correct an incorrect configuration in my newsreader :-)

-nrk.
Nov 14 '05 #21

P: n/a
On Mon, 22 Dec 2003 20:03:48 GMT, nrk
<ra*********@deadbeef.verizon.net> wrote in comp.lang.c:
CBFalconer wrote:
nrk wrote:

... 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.


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.


You're right, that's an excellent reason for not plonking Tisdale.
But it's not the reason that I haven't plonked him long ago.

Amazingly enough, if you read enough of his replies, you will
occasionally come across one that is helpful, courteous, and correct,
perhaps even insightful.

I like to find these, as it proves even trolls and dilettantes can
have an off day. ;)

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 14 '05 #22

P: n/a

"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:3F**************@jpl.nasa.gov...
John Bode wrote:
E. Robert Tisdale wrote:
Andrew Clark wrote:

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

char* p = (char*)malloc(16);

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


I am *not* quoting Andrew Clark.


I'm glad to see that you at least have the decency to admit that
you're a liar.
Nov 14 '05 #23

P: n/a
"Andrew Clark" <an*****@syr.edu> wrote in message
news:Xn*********************@208.33.61.211...
*** post for FREE via your newsreader at post.newsfeed.com ***

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

Will this invoke UB?


You can even write it to file, then read it back later, and assuming I/O was
successful and you didn't free the pointer in the mean time, it will still
work. [This basically puts a potential stop on true complete garbage
collection within C.]

--
Peter
Nov 14 '05 #24

P: n/a
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message news:<3F**************@jpl.nasa.gov>...
John Bode wrote:
E. Robert Tisdale wrote:
Andrew Clark wrote:

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

char* p = (char*)malloc(16);
^^^^^^^
This cast does not appear in the original article you are quoting.


I am *not* quoting Andrew Clark.


Really? Then why do you have the quote marks ('>') by the code, the
"Andrew Clark wrote:" line, etc. It certainly *looks* like you are
quoting him.
Why did you add it here?
I am paraphrasing Andrew Clark
to reflect *my* understanding of what Andrew Clark meant.


Okay. In the future, please note where and why you are "paraphrasing"
someone's code so it won't look like you're doing something dishonest.
Consult the comp.lang.c newsgroup archives:

http://groups.google.com/groups?hl=e...up=comp.lang.c

if you want to know exactly what Andrew Clark wrote.


Yes, I can see that article, which led me to ask why your version of
his code was different. I frankly can't understand why you felt the
need to "paraphrase" such a simple bit of code. If you had left the
original code intact, then added your "paraphrased" version, then it
wouldn't look so suspicious. But it looked for all the world like you
were deliberately misquoting the OP to make him look like he was
saying something he didn't.

[snip remainder on netiquette]
Nov 14 '05 #25

This discussion thread is closed

Replies have been disabled for this discussion.