Undefined behavior?

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

  1. Andrew Clark

    Andrew Clark Guest

    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
     
    Andrew Clark, Dec 22, 2003
    #1
    1. Advertisements

  2. Andrew Clark

    Ben Pfaff Guest

    No. There is no reason that it should.
     
    Ben Pfaff, Dec 22, 2003
    #2
    1. Advertisements

  3. Andrew Clark

    pete Guest

    No.
    The pointer value returned by malloc,
    is the pointer value which is freed.
     
    pete, Dec 22, 2003
    #3
  4. Andrew Clark

    Artie Gold Guest

    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, Dec 22, 2003
    #4
  5. 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

    Well, for my snippet I assumed that ;-)

    Andrew
     
    Andrew Clark, Dec 22, 2003
    #6
  7. Andrew Clark

    nrk Guest

    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.
     
    nrk, Dec 22, 2003
    #7
  8. 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.
    The same bug as above - resulting in undefined behavior.
    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.
     
    The Real OS/2 Guy, Dec 22, 2003
    #8
  9. 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, Dec 22, 2003
    #9
  10. Andrew Clark

    CBFalconer Guest

    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.
     
    CBFalconer, Dec 22, 2003
    #10
  11. Andrew Clark

    John Bode Guest

    ^^^^^^^

    This cast does not appear in the original article you are quoting.
    Why did you add it here?
     
    John Bode, Dec 22, 2003
    #11
  12. Andrew Clark

    John Bode Guest

    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.
     
    John Bode, Dec 22, 2003
    #12
  13. Andrew Clark

    nrk Guest

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

    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

    No, he most definitely did not write most of what followed below.
    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.
     
    nrk, Dec 22, 2003
    #15
  16. Why did you change my original indentation?
     
    E. Robert Tisdale, Dec 22, 2003
    #16
  17. Andrew Clark

    Chris Torek Guest

    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.)
     
    Chris Torek, Dec 22, 2003
    #17
  18. 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

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

    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.

    ========================================================================
    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, Dec 22, 2003
    #20
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.