malloc

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • aarklon@gmail.com

    malloc

    Hi all,

    suppose i am having a structure as follows

    typedef struct node
    {
    int n;
    struct node * next;
    }sn;

    sn *p;

    now which of the following is the correct way to allocate memory and
    why ?

    OR

    what is the trade off between the following two allocations

    1) p = malloc( sizeof(sn) );
    2) p = malloc( sizeof(*p) );





  • Eric Sosman

    #2
    Re: malloc

    aarklon@gmail.c om wrote:
    [... brutal context snippage for didactic purposes ...]
    >
    what is the trade off between the following two allocations
    >
    1) p = malloc( sizeof(sn) );
    2) p = malloc( sizeof(*p) );
    Using only the context available in this reply -- that
    is, looking only at the two lines of code in the question
    and not referring back to the declaration of `p' in your
    question -- which line is easier to check for correctness?

    Q.E.D.

    --
    Eric.Sosman@sun .com

    Comment

    • aarklon@gmail.com

      #3
      Re: malloc

      On Feb 21, 11:48 pm, Eric Sosman <Eric.Sos...@su n.comwrote:
      aark...@gmail.c om wrote:
      [... brutal context snippage for didactic purposes ...]
      >
      what is the trade off between the following two allocations
      >
      1) p = malloc( sizeof(sn) );
      2) p = malloc( sizeof(*p) );
      >
      Using only the context available in this reply -- that
      is, looking only at the two lines of code in the question
      and not referring back to the declaration of `p' in your
      question -- which line is easier to check for correctness?
      >
      Q.E.D.
      Generally in programs involving linked lists i have seen the latter
      one being used most often.

      now p is simply a pointer variable, if by
      accident if p points to NULL , p = malloc( sizeof(*p) ); will not
      give correct allocation isn't it....??????


      Comment

      • David T. Ashley

        #4
        Re: malloc

        <aarklon@gmail. comwrote in message
        news:02ce42ce-92fe-4bdc-85d5-6d4f816bbdc2@u6 9g2000hse.googl egroups.com...
        Hi all,
        >
        suppose i am having a structure as follows
        >
        typedef struct node
        {
        int n;
        struct node * next;
        }sn;
        >
        sn *p;
        >
        now which of the following is the correct way to allocate memory and
        why ?
        >
        OR
        >
        what is the trade off between the following two allocations
        >
        1) p = malloc( sizeof(sn) );
        2) p = malloc( sizeof(*p) );
        The compiler treats each of these two identically, and will simply call
        malloc with a constant (the same constant in either case).

        The first form is 33% better than the first. The reason is that in the
        second form, you normally have to use the SHIFT key on your keyboard, for 3
        keystrokes. For the first form, only two keystrokes. If you had to type
        the malloc() statement several million times, the second form may put more
        wear on your keyboard and fingers.

        Comment

        • CBFalconer

          #5
          Re: malloc

          "David T. Ashley" wrote:
          <aarklon@gmail. comwrote in message
          >
          >suppose i am having a structure as follows
          >>
          >typedef struct node {
          > int n;
          > struct node * next;
          >}sn;
          >>
          >sn *p;
          >>
          .... snip ...
          >>
          >what is the trade off between the following two allocations
          >>
          >1) p = malloc( sizeof(sn) );
          >2) p = malloc( sizeof(*p) );
          >
          The compiler treats each of these two identically, and will simply
          call malloc with a constant (the same constant in either case).
          True.
          >
          The first form is 33% better than the first. The reason is that
          in the second form, you normally have to use the SHIFT key on your
          keyboard, for 3 keystrokes. For the first form, only two
          keystrokes. If you had to type the malloc() statement several
          million times, the second form may put more wear on your keyboard
          and fingers.
          Utter nonsense. See some of the other answers, including Sosman.

          --
          [mail]: Chuck F (cbfalconer at maineline dot net)
          [page]: <http://cbfalconer.home .att.net>
          Try the download section.



          --
          Posted via a free Usenet account from http://www.teranews.com

          Comment

          • MisterE

            #6
            Re: malloc


            what is the trade off between the following two allocations
            >
            1) p = malloc( sizeof(sn) );
            2) p = malloc( sizeof(*p) );
            I have seldom seen people use *p. Normally you use only the type definition
            itself and try not to use a variable at all. I think this is because some
            early compilers would only allow type definitions. All the 'guidelines' at
            companies I have coded for always ask to use type definitions only, never
            variables.


            Comment

            • Ian Collins

              #7
              Re: malloc

              MisterE wrote:
              >what is the trade off between the following two allocations
              >>
              >1) p = malloc( sizeof(sn) );
              >2) p = malloc( sizeof(*p) );
              >
              I have seldom seen people use *p. Normally you use only the type definition
              itself and try not to use a variable at all. I think this is because some
              early compilers would only allow type definitions. All the 'guidelines' at
              companies I have coded for always ask to use type definitions only, never
              variables.
              >
              Yet more nonsense, verging on bollocks. Where is the variable in
              sizeof(*p)?

              --
              Ian Collins.

              Comment

              • Bartc

                #8
                Re: malloc

                CBFalconer wrote:
                "David T. Ashley" wrote:
                ><aarklon@gmail .comwrote in message
                >>
                >>suppose i am having a structure as follows
                >>>
                >>typedef struct node {
                >> int n;
                >> struct node * next;
                >>}sn;
                >>>
                >>sn *p;
                >>>
                ... snip ...
                >>>
                >>what is the trade off between the following two allocations
                >>>
                >>1) p = malloc( sizeof(sn) );
                >>2) p = malloc( sizeof(*p) );
                >>
                >The compiler treats each of these two identically, and will simply
                >call malloc with a constant (the same constant in either case).
                >
                True.
                >
                >>
                >The first form is 33% better than the first. The reason is that
                >in the second form, you normally have to use the SHIFT key on your
                >keyboard, for 3 keystrokes. For the first form, only two
                >keystrokes. If you had to type the malloc() statement several
                >million times, the second form may put more wear on your keyboard
                >and fingers.
                >
                Utter nonsense. See some of the other answers, including Sosman.
                He has a point. I once did so much typing that my fingers hurt and I had to
                type wearing woollen gloves.

                But, if that is really a problem then C is full of punctuation! In that case
                use a different language -- or a softer keyboard.

                --
                Bart


                Comment

                • Mark Bluemel

                  #9
                  Re: malloc

                  Bartc wrote:
                  CBFalconer wrote:
                  >"David T. Ashley" wrote:
                  >><aarklon@gmai l.comwrote in message
                  [snip]
                  >>>what is the trade off between the following two allocations
                  >>>>
                  >>>1) p = malloc( sizeof(sn) );
                  >>>2) p = malloc( sizeof(*p) );
                  >>The first form is 33% better than the first. The reason is that
                  >>in the second form, you normally have to use the SHIFT key on your
                  >>keyboard, for 3 keystrokes. For the first form, only two
                  >>keystrokes. If you had to type the malloc() statement several
                  >>million times, the second form may put more wear on your keyboard
                  >>and fingers.
                  >Utter nonsense. See some of the other answers, including Sosman.
                  I have CBF killfiled but had to reply to this, simply to say "ROTFL"...

                  Did anyone else hear a "Whoosh" as DTA's post past right over Chuck's
                  head?

                  Comment

                  • vippstar@gmail.com

                    #10
                    Re: malloc

                    On Feb 22, 1:21 pm, "Bartc" <b...@freeuk.co mwrote:
                    CBFalconer wrote:
                    "David T. Ashley" wrote:
                    <aark...@gmail. comwrote in message
                    >
                    >suppose i am having a structure as follows
                    >
                    >typedef struct node {
                    > int n;
                    > struct node * next;
                    >}sn;
                    >
                    >sn *p;
                    >
                    ... snip ...
                    >
                    >what is the trade off between the following two allocations
                    >
                    >1) p = malloc( sizeof(sn) );
                    >2) p = malloc( sizeof(*p) );
                    >
                    The compiler treats each of these two identically, and will simply
                    call malloc with a constant (the same constant in either case).
                    >
                    True.
                    >
                    The first form is 33% better than the first. The reason is that
                    in the second form, you normally have to use the SHIFT key on your
                    keyboard, for 3 keystrokes. For the first form, only two
                    keystrokes. If you had to type the malloc() statement several
                    million times, the second form may put more wear on your keyboard
                    and fingers.
                    >
                    Utter nonsense. See some of the other answers, including Sosman.
                    >
                    He has a point. I once did so much typing that my fingers hurt and I had to
                    type wearing woollen gloves.
                    >
                    But, if that is really a problem then C is full of punctuation! In that case
                    use a different language -- or a softer keyboard.
                    I would suggest an ergonomic keyboard and a better chair. (<http://
                    www.kinesis-ergo.com/has some good ones, but for a price)
                    If you still have these pains also pay a visit to your doctor, do
                    *NOT* ignore them.

                    Comment

                    • Ben Bacarisse

                      #11
                      Re: malloc

                      "MisterE" <voids@sometwhe r.worldwrites:
                      >what is the trade off between the following two allocations
                      >>
                      >1) p = malloc( sizeof(sn) );
                      >2) p = malloc( sizeof(*p) );
                      >
                      I have seldom seen people use *p. Normally you use only the type definition
                      itself and try not to use a variable at all. I think this is because some
                      early compilers would only allow type definitions.
                      How early, I wonder? The expression form is there in K&R (published
                      1978). Do we really have to be wary of things that might not get by a
                      compiler more than 30 years old. Have you every seen one that rejects
                      this syntax?
                      All the 'guidelines' at
                      companies I have coded for always ask to use type definitions only, never
                      variables.
                      All that shows is that they are missing a useful feature. I doubt it
                      has anything to do with compiling with old compilers. Do they also
                      bad "void" (not there in 1978) and advocate the

                      f(a)
                      int a;
                      { ... }

                      style of function definition?

                      --
                      Ben.

                      Comment

                      • Bartc

                        #12
                        Re: malloc


                        <vippstar@gmail .comwrote in message
                        news:b03b55f1-6975-4f21-91ee-75c61fcc29e0@d5 g2000hsc.google groups.com...
                        On Feb 22, 1:21 pm, "Bartc" <b...@freeuk.co mwrote:
                        >CBFalconer wrote:
                        "David T. Ashley" wrote:
                        >The first form is 33% better than the first. The reason is that
                        >in the second form, you normally have to use the SHIFT key on your
                        >keyboard, for 3 keystrokes. For the first form, only two
                        >keystrokes. If you had to type the malloc() statement several
                        >million times, the second form may put more wear on your keyboard
                        >and fingers.
                        >>
                        Utter nonsense. See some of the other answers, including Sosman.
                        >>
                        >He has a point. I once did so much typing that my fingers hurt and I had
                        >to
                        >type wearing woollen gloves.
                        >>
                        >But, if that is really a problem then C is full of punctuation! In that
                        >case
                        >use a different language -- or a softer keyboard.
                        I would suggest an ergonomic keyboard and a better chair. (<http://
                        www.kinesis-ergo.com/has some good ones, but for a price)
                        If you still have these pains also pay a visit to your doctor, do
                        *NOT* ignore them.
                        Thanks, but this was many years ago. I no longer spend 10-12 hours a day
                        programming.

                        But for anyone else still at risk, then yes take some action.

                        --
                        Bart


                        Comment

                        • Richard

                          #13
                          Re: malloc

                          "MisterE" <voids@sometwhe r.worldwrites:
                          >what is the trade off between the following two allocations
                          >>
                          >1) p = malloc( sizeof(sn) );
                          >2) p = malloc( sizeof(*p) );
                          >
                          I have seldom seen people use *p. Normally you use only the type
                          definition
                          Then you are not a C programmer of much experience. It is infinitely
                          superior for code maintenance.
                          itself and try not to use a variable at all. I think this is because some
                          early compilers would only allow type definitions. All the 'guidelines' at
                          companies I have coded for always ask to use type definitions only, never
                          variables.

                          Comment

                          • Chris Dollin

                            #14
                            Re: malloc

                            MisterE wrote:
                            All the 'guidelines' at
                            companies I have coded for always ask to use type definitions only, never
                            variables.
                            OK, let's see some.

                            --
                            "Creation began." - James Blish, /A Clash of Cymbals/

                            Hewlett-Packard Limited registered office: Cain Road, Bracknell,
                            registered no: 690597 England Berks RG12 1HN

                            Comment

                            • Ben Bacarisse

                              #15
                              Re: malloc

                              aarklon@gmail.c om writes:
                              On Feb 22, 2:55 am, pete <pfil...@mindsp ring.comwrote:
                              >aark...@gmail. com wrote:
                              >>
                              On Feb 21, 11:48 pm, Eric Sosman <Eric.Sos...@su n.comwrote:
                              aark...@gmail.c om wrote:
                              [... brutal context snippage for didactic purposes ...]
                              >>
                              what is the trade off between the following two allocations
                              >>
                              1) p = malloc( sizeof(sn) );
                              2) p = malloc( sizeof(*p) );
                              <snip>
                              I think this program will make things clear
                              Sorry, not to me. What does it make clear to you?

                              <snip>
                              printf("\n sizeof p = %d", sizeof(p));
                              printf("\n sizeof *p = %d", sizeof(*p));
                              printf("\n sizeof q = %d",sizeof(q) );
                              printf("\n sizeof *q = %d",sizeof(*q)) ;
                              printf("\n sizeof NULL = %d",sizeof(NULL ));
                              printf("\n sizeof r = %d",sizeof(r) );
                              <snip>

                              To print sizes with %d, cast the sizeof expression to int. This is
                              safe since all these sizes will be small. Alternatively use the %zu
                              format if you have a suitable printf.


                              --
                              Ben.

                              Comment

                              Working...