Re: FreeBSD's malloc problem ?

From: Daniel Ellard (ellard_at_eecs.harvard.edu)
Date: 04/24/04

  • Next message: Anton Alin-Adrian: "Re: FreeBSD's malloc problem ?"
    Date: Sat, 24 Apr 2004 15:14:46 -0400 (EDT)
    To: Anton Alin-Adrian <aanton@reversedhell.net>
    
    

    > And let there be light... DANG.. well it almost blinded me. I was
    > confusing with char[16], which has the +1 byte for the null
    > terminating, but the malloc(16) hasn't...

    No, that's still not quite it...

    char[16] allocates exactly 16 characters. Period. There's no extra
    space on the end for the terminating nul. If you try to put a sixteen
    character string into this array, the terminating nul will slop over
    onto whatever follows this array in memory.

    malloc(16) is essentially the same. The difference is that there
    might not be something right there to be clobbered. malloc tends to
    round up the number of bytes to something convenient. It's easier to
    manage a pool of things that are all the same size than a zillion
    different sizes. 16 is pretty small -- the linux malloc might round
    everything smaller than 20 bytes or 24 bytes (why 20 or 24? That's
    another story...) to 20 or 24 bytes bytes just to make its life
    easier. Therefore it's giving you four "extra" bytes and the nul can
    clobber them without causing you to notice the bug.

    -Dan

    _______________________________________________
    freebsd-hackers@freebsd.org mailing list
    http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
    To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@freebsd.org"


  • Next message: Anton Alin-Adrian: "Re: FreeBSD's malloc problem ?"

    Relevant Pages

    • Re: CString help
      ... MSDN has gotten a lot better about defining the meaning of N in the ... As a function parameter, ... terminating nul. ... characters actually written, excluding the nul. ...
      (microsoft.public.vc.mfc)
    • Re: Acquiring UTF-8 string length
      ... http://en.wikipedia.org/wiki/Multi-byte_character_set, this UTF-8 string ... The number of codepoints is four, plus the terminating NUL. ... Four characters plus a terminating NUL. ...
      (microsoft.public.vc.language)
    • Re: about string and character
      ... the number of characters before the terminating nul. ... How does that result differ from the index of the first '\0' in a string? ...
      (comp.lang.c)