Undefined behavior?

Discussion in 'C Programming' started by Andrew Clark, Dec 22, 2003.

  1. Andrew Clark

    Andrew Clark Guest

    *** 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! =-----
    Andrew Clark, Dec 22, 2003
    #1
    1. Advertising

  2. Andrew Clark

    Ben Pfaff Guest

    Andrew Clark <> 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[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \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\
    );}return 0;}
    Ben Pfaff, Dec 22, 2003
    #2
    1. Advertising

  3. Andrew Clark

    pete Guest

    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
    pete, Dec 22, 2003
    #3
  4. Andrew Clark

    Artie Gold Guest

    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.
    Artie Gold, Dec 22, 2003
    #4
  5. 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);
    E. Robert Tisdale, Dec 22, 2003
    #5
  6. Andrew Clark

    Andrew Clark Guest

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

    Artie Gold <> 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! =-----
    Andrew Clark, Dec 22, 2003
    #6
  7. Andrew Clark

    nrk Guest

    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);
    nrk, Dec 22, 2003
    #7
  8. On Mon, 22 Dec 2003 01:38:32 UTC, "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?

    >
    > 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
    The Real OS/2 Guy, Dec 22, 2003
    #8
  9. nrk <> 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.
    Christopher Benson-Manica, Dec 22, 2003
    #9
  10. Andrew Clark

    CBFalconer Guest

    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 () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Dec 22, 2003
    #10
  11. Andrew Clark

    John Bode Guest

    "E. Robert Tisdale" <> wrote in message news:<>...
    > 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);
    John Bode, Dec 22, 2003
    #11
  12. Andrew Clark

    John Bode Guest

    Andrew Clark <> wrote in message news:<Xns9458CB3D4CCA5asdfjkl@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! =-----
    John Bode, Dec 22, 2003
    #12
  13. Andrew Clark

    nrk Guest

    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.
    nrk, Dec 22, 2003
    #13
  14. 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=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.
    E. Robert Tisdale, Dec 22, 2003
    #14
  15. Andrew Clark

    nrk Guest

    John Bode wrote:

    > "E. Robert Tisdale" <> wrote in message
    > news:<>...
    >> 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);
    nrk, Dec 22, 2003
    #15
  16. Troll Alert: Undefined behavior?

    Something that calls itself nrk wrote:

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


    Why did you change my original indentation?
    E. Robert Tisdale, Dec 22, 2003
    #16
  17. Andrew Clark

    Chris Torek Guest

    Re: Troll Alert: Undefined behavior?

    In article <news:>
    E. Robert Tisdale <> 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 (40°39.22'N, 111°50.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.
    Chris Torek, Dec 22, 2003
    #17
  18. In article <>,
    "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.
    > 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.
    Christian Bau, Dec 22, 2003
    #18
  19. Andrew Clark

    Fao, Sean Guest

    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.
    Fao, Sean, Dec 22, 2003
    #19
  20. "E. Robert Tisdale" <> 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
    <>. 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) <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)
    Keith Thompson, Dec 22, 2003
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. aka
    Replies:
    10
    Views:
    697
  2. REH
    Replies:
    25
    Views:
    828
    Victor Bazarov
    Mar 29, 2005
  3. Mantorok Redgormor
    Replies:
    70
    Views:
    1,751
    Dan Pop
    Feb 17, 2004
  4. VK
    Replies:
    45
    Views:
    591
    Dr John Stockton
    Sep 12, 2006
  5. -Lost
    Replies:
    13
    Views:
    368
    Richard Cornford
    Jan 31, 2007
Loading...

Share This Page