malloc and free

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ed

    malloc and free

    Hi,

    When free() is used to free the memory allocated by malloc(), how does
    it know the size of the memory to be freed? I guess I'm asking where
    does the info stored? Thanks

    Ed
  • Dave Vandervies

    #2
    Re: malloc and free

    In article <c959d82c.04101 30818.4980c9f6@ posting.google. com>,
    Ed <kurtoo@netscap e.net> wrote:[color=blue]
    >Hi,
    >
    >When free() is used to free the memory allocated by malloc(), how does
    >it know the size of the memory to be freed?[/color]

    Compiler magic.
    (Well, in this case, library magic. But implementation magic in any
    case.)
    [color=blue]
    > I guess I'm asking where
    >does the info stored?[/color]

    Somewhere you can't get at it. Or possibly it gets reconstructed from
    other information and doesn't need to be stored. Or possibly free()
    just needs to request that the hardware mark the segment as unallocated
    and doesn't need the size at all.

    If you need to know the size of a chunk of memory you got from malloc,
    you'll need to store the size you asked it for somewhere.


    dave

    --
    Dave Vandervies dj3vande@csclub .uwaterloo.ca

    [I]t's less of an ego-boost to be right than it is to be *righter*.
    --Micah Cowan in comp.lang.c

    Comment

    • Nils O. Selåsdal

      #3
      Re: malloc and free

      Ed wrote:[color=blue]
      > Hi,
      >
      > When free() is used to free the memory allocated by malloc(), how does
      > it know the size of the memory to be freed? I guess I'm asking where
      > does the info stored? Thanks[/color]
      Implementation dependant.
      It could store it in memory just before the memory given back by malloc,
      it could use the address as an index into an array/tree/whatnot, and
      many other more or less clever things.

      Comment

      • Default User

        #4
        Re: malloc and free

        Ed wrote:
        [color=blue]
        > Hi,
        >
        > When free() is used to free the memory allocated by malloc(), how does
        > it know the size of the memory to be freed?[/color]

        Magic.
        [color=blue]
        > I guess I'm asking where does the info stored?[/color]

        Someplace secret. None of your business, seriously. How a particular
        implementation does it is highly platform-specific and of little or no
        use to the programmer.




        Brian Rodenborn

        Comment

        • Dan Pop

          #5
          Re: malloc and free

          In <I5JACD.IJE@new s.boeing.com> "Default User" <first.last@boe ing.com.invalid > writes:
          [color=blue]
          >Ed wrote:
          >[color=green]
          >> When free() is used to free the memory allocated by malloc(), how does
          >> it know the size of the memory to be freed?[/color]
          >
          >Magic.[/color]

          It derives it, one way or another, from the address of the memory block.
          See chapter 8 from K&R2 for one possible answer.
          [color=blue][color=green]
          >> I guess I'm asking where does the info stored?[/color]
          >
          >Someplace secret. None of your business, seriously. How a particular
          >implementati on does it is highly platform-specific and of little or no
          >use to the programmer.[/color]

          In an ideal world, where programmers didn't make any mistakes, maybe.
          In the real world, this kind of information helps understanding the
          (mis)behaviour of a buggy program, e.g. why a free() call generates a
          segfault or why some implementations complain about a corrupted malloc
          arena.

          Dan
          --
          Dan Pop
          DESY Zeuthen, RZ group
          Email: Dan.Pop@ifh.de
          Currently looking for a job in the European Union

          Comment

          • Method Man

            #6
            Re: malloc and free

            > > I guess I'm asking where does the info stored?[color=blue]
            >
            > Someplace secret. None of your business, seriously.[/color]

            That may be true.
            [color=blue]
            > How a particular
            > implementation does it is highly platform-specific and of little or no
            > use to the programmer.
            >[/color]

            It can be educational to know how a particual implementation of malloc is
            done. Perhaps the OP wishes to write his own memory manager and is gathering
            information. In any case, there are definitely purposes and uses for seeking
            such knowledge.


            Comment

            • E. Robert Tisdale

              #7
              Re: malloc and free

              Ed wrote:
              [color=blue]
              > When free() is used to free the memory allocated by malloc(),
              > how does it know the size of the memory to be freed?[/color]

              Obviously, malloc(size_t) stores the size somewhere
              where free(void*) can find it.
              [color=blue]
              > I guess I'm asking where does the info stored?[/color]

              Typically, information about the amount of storage allocated
              is stored immediately before the allocated storage.
              Try this:
              [color=blue]
              > cat main.c[/color]
              #include <stdio.h>
              #include <stdlib.h>

              int main(int argc, char* argv[]) {
              if (1 < argc) {
              const
              size_t n = atoi(argv[1]);
              size_t* p = (size_t*)malloc (n*sizeof(size_ t));
              fprintf(stdout, "p[-2] = %u\n", p[-2]);
              fprintf(stdout, "p[-1] = %u\n", p[-1]);
              }
              return EXIT_SUCCESS;
              }
              [color=blue]
              > gcc -Wall -std=c99 -pedantic -o main main.c
              > ./main 1[/color]
              p[-2] = 0
              p[-1] = 17

              On my computer, malloc allocates storage
              in "double word" aligned increments.
              It always allocates one double word to store the size
              and at least one other double word for storage.

              Comment

              • Nils O. Selåsdal

                #8
                Re: malloc and free

                E. Robert Tisdale wrote:[color=blue]
                > Ed wrote:
                >[color=green]
                >> When free() is used to free the memory allocated by malloc(),
                >> how does it know the size of the memory to be freed?[/color]
                >
                >
                > Obviously, malloc(size_t) stores the size somewhere
                > where free(void*) can find it.
                >[color=green]
                >> I guess I'm asking where does the info stored?[/color]
                >
                >
                > Typically, information about the amount of storage allocated
                > is stored immediately before the allocated storage.
                > Try this:[/color]
                ....
                [color=blue]
                > On my computer, malloc allocates storage
                > in "double word" aligned increments.
                > It always allocates one double word to store the size
                > and at least one other double word for storage.[/color]
                fyi;
                bash-2.05b$ ./main 1
                p[-2] = 4294967295
                p[-1] = 4294967295

                bash-2.05b$ ./main 5
                p[-2] = 0
                p[-1] = 0

                :-)

                Comment

                • E. Robert Tisdale

                  #9
                  Re: malloc and free

                  Nils O. Selåsdal wrote:
                  [color=blue]
                  > E. Robert Tisdale wrote:
                  >[color=green]
                  >> Ed wrote:
                  >>[color=darkred]
                  >>> When free() is used to free the memory allocated by malloc(),
                  >>> how does it know the size of the memory to be freed?[/color]
                  >>
                  >> Obviously, malloc(size_t) stores the size somewhere
                  >> where free(void*) can find it.
                  >>[color=darkred]
                  >>> I guess I'm asking where does the info stored?[/color]
                  >>
                  >> Typically, information about the amount of storage allocated
                  >> is stored immediately before the allocated storage.
                  >> Try this:[/color]
                  >
                  > ...
                  >[color=green]
                  >> On my computer, malloc allocates storage
                  >> in "double word" aligned increments.
                  >> It always allocates one double word to store the size
                  >> and at least one other double word for storage.[/color]
                  >
                  > fyi;
                  > bash-2.05b$ ./main 1
                  > p[-2] = 4294967295
                  > p[-1] = 4294967295
                  >
                  > bash-2.05b$ ./main 5
                  > p[-2] = 0
                  > p[-1] = 0
                  >
                  > :-)[/color]

                  That' not a lot of information.
                  Would you care to elaborate a little?

                  Comment

                  • Default User

                    #10
                    Re: malloc and free

                    Dan Pop wrote:
                    [color=blue]
                    > In <I5JACD.IJE@new s.boeing.com> "Default User"
                    > <first.last@boe ing.com.invalid > writes:[/color]
                    [color=blue][color=green]
                    > > Someplace secret. None of your business, seriously. How a particular
                    > > implementation does it is highly platform-specific and of little or
                    > > no use to the programmer.[/color]
                    >
                    > In an ideal world, where programmers didn't make any mistakes, maybe.
                    > In the real world, this kind of information helps understanding the
                    > (mis)behaviour of a buggy program, e.g. why a free() call generates a
                    > segfault or why some implementations complain about a corrupted malloc
                    > arena.[/color]

                    Normally this question is a prelude to accessing this information for
                    the wrong purposes, like figuring out the size of dynamically-allocated
                    arrays or such.




                    Brian Rodenborn

                    Comment

                    • Nils O. Selåsdal

                      #11
                      Re: malloc and free

                      [color=blue][color=green]
                      >> fyi;
                      >> bash-2.05b$ ./main 1
                      >> p[-2] = 4294967295
                      >> p[-1] = 4294967295
                      >>
                      >> bash-2.05b$ ./main 5
                      >> p[-2] = 0
                      >> p[-1] = 0
                      >>
                      >> :-)[/color]
                      >
                      >
                      > That' not a lot of information.
                      > Would you care to elaborate a little?[/color]
                      Just seems that my computer behaves diffrently than yours ;)
                      (Running NetBSD on x86 btw)

                      Comment

                      • Flash Gordon

                        #12
                        Re: malloc and free

                        On Wed, 13 Oct 2004 12:24:23 -0700
                        "E. Robert Tisdale" <E.Robert.Tisda le@jpl.nasa.gov > wrote:
                        [color=blue]
                        > Nils O. Selåsdal wrote:
                        > [color=green]
                        > > E. Robert Tisdale wrote:
                        > > [color=darkred]
                        > >> Ed wrote:
                        > >>
                        > >>> When free() is used to free the memory allocated by malloc(),
                        > >>> how does it know the size of the memory to be freed?
                        > >>
                        > >> Obviously, malloc(size_t) stores the size somewhere
                        > >> where free(void*) can find it.
                        > >>
                        > >>> I guess I'm asking where does the info stored?
                        > >>
                        > >> Typically, information about the amount of storage allocated
                        > >> is stored immediately before the allocated storage.
                        > >> Try this:[/color]
                        > >
                        > > ...
                        > > [color=darkred]
                        > >> On my computer, malloc allocates storage
                        > >> in "double word" aligned increments.
                        > >> It always allocates one double word to store the size
                        > >> and at least one other double word for storage.[/color]
                        > >
                        > > fyi;
                        > > bash-2.05b$ ./main 1
                        > > p[-2] = 4294967295
                        > > p[-1] = 4294967295
                        > >
                        > > bash-2.05b$ ./main 5
                        > > p[-2] = 0
                        > > p[-1] = 0
                        > >
                        > > :-)[/color]
                        >
                        > That' not a lot of information.
                        > Would you care to elaborate a little?[/color]

                        It shows that your suggesting of what is typically done does not apply
                        to Nils system, thus demonstrating that you are talking non-portable
                        rubbish again. I would have said that was completely obvious to someone
                        with two brain cells to run together.
                        --
                        Flash Gordon
                        Sometimes I think shooting would be far too good for some people.
                        Although my email address says spam, it is real and I read it.

                        Comment

                        • E. Robert Tisdale

                          #13
                          Re: malloc and free

                          Nils O. Selåsdal wrote:[color=blue]
                          >[color=green][color=darkred]
                          >>> fyi;
                          >>> bash-2.05b$ ./main 1
                          >>> p[-2] = 4294967295
                          >>> p[-1] = 4294967295
                          >>>
                          >>> bash-2.05b$ ./main 5
                          >>> p[-2] = 0
                          >>> p[-1] = 0
                          >>>
                          >>> :-)[/color]
                          >>
                          >>
                          >>
                          >> That' not a lot of information.
                          >> Would you care to elaborate a little?[/color]
                          >
                          > Just seems that my computer behaves diffrently than yours ;)
                          > (Running NetBSD on x86 btw)[/color]

                          Which compiler?

                          Comment

                          • Nils O. Selåsdal

                            #14
                            Re: malloc and free

                            E. Robert Tisdale wrote:[color=blue]
                            > Nils O. Selåsdal wrote:
                            >[color=green]
                            >>[color=darkred]
                            >>>> fyi;
                            >>>> bash-2.05b$ ./main 1
                            >>>> p[-2] = 4294967295
                            >>>> p[-1] = 4294967295
                            >>>>
                            >>>> bash-2.05b$ ./main 5
                            >>>> p[-2] = 0
                            >>>> p[-1] = 0
                            >>>>
                            >>>> :-)
                            >>>
                            >>>
                            >>>
                            >>>
                            >>> That' not a lot of information.
                            >>> Would you care to elaborate a little?[/color]
                            >>
                            >>
                            >> Just seems that my computer behaves diffrently than yours ;)
                            >> (Running NetBSD on x86 btw)[/color]
                            >
                            >
                            > Which compiler?[/color]

                            bash-2.05b$ gcc -v
                            Using built-in specs.
                            Configured with:
                            /home/nick/work/netbsd/src/tools/gcc/../../gnu/dist/gcc/configure
                            --enable-long-long --disable-multilib --enable-threads --disable-symvers
                            --build=i386-unknown-netbsdelf --host=i386--netbsdelf
                            --target=i386--netbsdelf
                            Thread model: posix
                            gcc version 3.3.3 (NetBSD nb3 20040520)

                            Comment

                            • E. Robert Tisdale

                              #15
                              Re: malloc and free

                              Nils O. Selåsdal wrote:[color=blue]
                              > E. Robert Tisdale wrote:
                              >[color=green]
                              >> Nils O. Selåsdal wrote:
                              >>[color=darkred]
                              >>>
                              >>>>> fyi;
                              >>>>> bash-2.05b$ ./main 1
                              >>>>> p[-2] = 4294967295
                              >>>>> p[-1] = 4294967295
                              >>>>>
                              >>>>> bash-2.05b$ ./main 5
                              >>>>> p[-2] = 0
                              >>>>> p[-1] = 0
                              >>>>>
                              >>>>> :-)
                              >>>>
                              >>>>
                              >>>>
                              >>>>
                              >>>>
                              >>>> That' not a lot of information.
                              >>>> Would you care to elaborate a little?
                              >>>
                              >>>
                              >>>
                              >>> Just seems that my computer behaves diffrently than yours ;)
                              >>> (Running NetBSD on x86 btw)[/color]
                              >>
                              >>
                              >>
                              >> Which compiler?[/color]
                              >
                              >
                              > bash-2.05b$ gcc -v
                              > Using built-in specs.
                              > Configured with:
                              > /home/nick/work/netbsd/src/tools/gcc/../../gnu/dist/gcc/configure
                              > --enable-long-long --disable-multilib --enable-threads --disable-symvers
                              > --build=i386-unknown-netbsdelf --host=i386--netbsdelf
                              > --target=i386--netbsdelf
                              > Thread model: posix
                              > gcc version 3.3.3 (NetBSD nb3 20040520)[/color]
                              [color=blue]
                              > gcc -v[/color]
                              Reading specs from /usr/local/lib/gcc/i686-pc-linux-gnu/3.4.1/specs
                              Configured with: ../gcc-3.4.1.src/configure
                              Thread model: posix
                              gcc version 3.4.1

                              Comment

                              Working...