simple malloc question

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

    #1

    simple malloc question

    Hi, I have a question about malloc.

    When I do this:

    char **p = malloc(12);

    does malloc implicitly do 12 * sizeof(char*) for me? or would I have to
    explicitly do it?

    If it does not implicitly do it, then what is returned?

    - is it 3 pointers to work with since pointers take up 4 bytes each on
    32-bit systems?

    - or is it twelve pointers, which would mean malloc implicitly did 12 *
    sizeof(char*), which equals: 48 bytes..?

    Thanks in advanced,
    relient.

  • Nelu

    #2
    Re: simple malloc question

    On 2006-01-09, relient <xllx.relient.x llx@gmail.com> wrote:[color=blue]
    > Hi, I have a question about malloc.
    >
    > When I do this:
    >
    > char **p = malloc(12);
    >
    > does malloc implicitly do 12 * sizeof(char*) for me? or would I have to
    > explicitly do it?[/color]
    No. malloc takes one argument that is a size_t
    that represents the number of bytes to allocate.
    It can't automatically tell what to allocate based on the
    type of the variable on the left side of =.
    [color=blue]
    > If it does not implicitly do it, then what is returned?[/color]
    It returns the same thing whether it does that or not:
    the address of the allocated object if the allocation was succesful
    and a null pointer otherwise.[color=blue]
    >
    > - is it 3 pointers to work with since pointers take up 4 bytes each on
    > 32-bit systems?
    >
    > - or is it twelve pointers, which would mean malloc implicitly did 12 *
    > sizeof(char*), which equals: 48 bytes..?[/color]
    12 bytes. Use sizeof(type *), forget about the 4 bytes/pointer if
    you want to have portable code.
    sizeof(char) is always 1.


    --
    Ioan - Ciprian Tandau
    tandau _at_ freeshell _dot_ org (hope it's not too late)
    (... and that it still works...)

    Comment

    • Ico

      #3
      Re: simple malloc question

      relient <xllx.relient.x llx@gmail.com> wrote:[color=blue]
      > When I do this:
      >
      > char **p = malloc(12);
      >
      > does malloc implicitly do 12 * sizeof(char*) for me? or would I have to
      > explicitly do it?
      >
      > If it does not implicitly do it, then what is returned?[/color]

      No, it allocates exactly 12 bytes, and returns a pointer to this freshly
      allocated memory.
      [color=blue]
      > - is it 3 pointers to work with since pointers take up 4 bytes each on
      > 32-bit systems?[/color]

      It might, but don't count on that.
      [color=blue]
      > - or is it twelve pointers, which would mean malloc implicitly did 12 *
      > sizeof(char*), which equals: 48 bytes..?[/color]

      No. As you can probably find in your compiler's documentation,
      malloc(size) allocates size *bytes* and returns a pointer to the
      allocated memory.

      To be safe, use something like

      size_t count = 12;
      char **p = malloc(sizeof(c har *) * count);

      or

      size_t count = 12;
      char **p = calloc(sizeof(c har *), count);


      --
      :wq
      ^X^Cy^K^X^C^C^C ^C

      Comment

      • relient

        #4
        Re: simple malloc question

        So my max index range with my example above would be 2, correct?:

        p[2] = "string literal";

        Thanks,
        relient.

        Comment

        • relient

          #5
          Re: simple malloc question

          So my max index range with my example above would be 2, correct?:

          p[2] = "strin literal";

          Thanks,
          relient.

          Comment

          • Nelu

            #6
            Re: simple malloc question

            On 2006-01-09, relient <xllx.relient.x llx@gmail.com> wrote:[color=blue]
            > So my max index range with my example above would be 2, correct?:
            >
            > p[2] = "strin literal";
            >[/color]
            If the size of the pointer is 4 bytes then that should
            be the case.

            Please go to: http://cfaj.freeshell.org/google/

            --
            Ioan - Ciprian Tandau
            tandau _at_ freeshell _dot_ org (hope it's not too late)
            (... and that it still works...)

            Comment

            • Keith Thompson

              #7
              Re: simple malloc question

              Ico <usenet@zevv.nl > writes:[color=blue]
              > relient <xllx.relient.x llx@gmail.com> wrote:[color=green]
              >> When I do this:
              >>
              >> char **p = malloc(12);
              >>
              >> does malloc implicitly do 12 * sizeof(char*) for me? or would I have to
              >> explicitly do it?
              >>
              >> If it does not implicitly do it, then what is returned?[/color]
              >
              > No, it allocates exactly 12 bytes, and returns a pointer to this freshly
              > allocated memory.[/color]

              Or it returns a null pointer value if the allocation failed (you
              should always check for this).

              [...]
              [color=blue]
              > No. As you can probably find in your compiler's documentation,
              > malloc(size) allocates size *bytes* and returns a pointer to the
              > allocated memory.
              >
              > To be safe, use something like
              >
              > size_t count = 12;
              > char **p = malloc(sizeof(c har *) * count);[/color]

              Better:

              char **p = malloc(count * sizeof *p);

              so you don't have to change the call of the type of p changes. (It's
              not a big deal in this case, since the initialization is on the same
              line as the declaration, but it can matter if the assignment is
              separate from the declaration.)
              [color=blue]
              > or
              >
              > size_t count = 12;
              > char **p = calloc(sizeof(c har *), count);[/color]

              calloc() has the overhead of setting the allocated memory to
              all-bits-zero, which isn't particularly useful in this case (a null
              pointer isn't necessarily represented as all-bits-zero).

              --
              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
              San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
              We must do something. This is something. Therefore, we must do this.

              Comment

              • Nils O. Selåsdal

                #8
                Re: simple malloc question

                relient wrote:[color=blue]
                > Hi, I have a question about malloc.
                >
                > When I do this:
                >
                > char **p = malloc(12);
                >
                > does malloc implicitly do 12 * sizeof(char*) for me? or would I have to
                > explicitly do it?[/color]
                The idiom is
                T *var = malloc(count * sizeof *var); /* and whenever count is 1 or
                *var is of type (unsigned) char, you normally omit them*/

                You probably want char **p = malloc(12 * sizeof *p);
                which makes room (unless it fails) for 12 char pointers.

                Comment

                • Christopher Benson-Manica

                  #9
                  Re: simple malloc question

                  Nelu <please@do.not. spam.me> wrote:

                  (WRT calling malloc() with an argument of 12:)
                  [color=blue]
                  > 12 bytes.[/color]

                  At least 12 bytes, possibly more, although there's no portable way to
                  determine how many bytes were actually allocated.

                  --
                  Christopher Benson-Manica | I *should* know what I'm talking about - if I
                  ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

                  Comment

                  • Eric Sosman

                    #10
                    Re: simple malloc question

                    Christopher Benson-Manica wrote:
                    [color=blue]
                    > Nelu <please@do.not. spam.me> wrote:
                    >
                    > (WRT calling malloc() with an argument of 12:)
                    >
                    >[color=green]
                    >>12 bytes.[/color]
                    >
                    >
                    > At least 12 bytes, possibly more, although there's no portable way to
                    > determine how many bytes were actually allocated.[/color]

                    In other words, only 12 bytes are allocated, where
                    "allocated" means "made available for use." No bytes
                    beyond 12 can be considered "allocated" from the point
                    of view of the caller of malloc().

                    --
                    Eric Sosman
                    esosman@acm-dot-org.invalid

                    Comment

                    Working...