70
BigBoo
7y

How malloc works? It's easy. Just follow this simple flowchart and you will understand in no time.

Comments
  • 11
    Higher res version of said flowchart.

    https://raw.githubusercontent.com/c...
  • 3
    @electric-ghost I'm trying to figure out why, according to this flowchart, it's legal to do.

    free(a);
    free(b);
    free(a);

    But not

    free(a);
    free(a);

    I know that it works this way, but can't find out why.
  • 7
    The tags, though.
  • 3
    @GMR516 Rightly brought up
  • 3
    @GMR516 I'm a rebel.

    Was pretty sure I removed the first tag. But I'm willing to roll with being ambiguous.
  • 0
    @BigBoo neither of them should work, should they? You "should" get a double free error on both. At least if a != 0 anyway
  • 0
    @aritzh Try it. It works.
  • 1
    @BigBoo wow. It actually does. But it only happens for small mallocs (in my tests, sizeof(int)*30 or smaller), while for bigger allocations, both give a double free error. So many bugs will be hidden because of this...
  • 1
    @aritzh There's a lot of flaws with memory allocation in C. Which also can lead to security issues if not handled properly.

    It's all a bit complicated. But very interesting to deep dive into.
Add Comment