malloc

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

    #16
    Re: malloc

    On Fri, 22 Feb 2008 00:39:44 -0500, David T. Ashley wrote:
    <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.
    Then this is even better, yes?
    2') p = malloc( sizeof *p);



    Comment

    • aarklon@gmail.com

      #17
      Re: malloc

      On Feb 22, 8:33 pm, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
      aark...@gmail.c om writes:
      On Feb 22, 2:55 am, pete <pfil...@mindsp ring.comwrote:
      aark...@gmail.c om 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.
      >
      >
      AFAIK sizeof operator 's return type is size_t and the type of size_t
      may vary across implementations .in most implementations i have seen
      size_t is treated as unsigned int.

      Am I correct ...???

      any way i haven't seen a format specifier %zu but I have seen %lu


      Comment

      • santosh

        #18
        Re: malloc

        aarklon@gmail.c om wrote:
        On Feb 22, 8:33 pm, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
        >aark...@gmail. com 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.
        >>
        >>
        AFAIK sizeof operator 's return type is size_t and the type of size_t
        may vary across implementations .in most implementations i have seen
        size_t is treated as unsigned int.
        >
        Am I correct ...???
        >
        any way i haven't seen a format specifier %zu but I have seen %lu
        %zu is new in C99. For C90 code using %lu and casting the corresponding
        argument to unsigned long is the best method, as is constantly
        discussed in this group.

        Comment

        • CBFalconer

          #19
          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.
          Then you haven't been learning from the better C programmers. No
          real C compiler has ever refused to use the variable.

          --
          [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

          • Willem

            #20
            Re: malloc

            Richard wrote:
            ) "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.

            'It is a lot better to do X' often does not translate to 'Most people do X'.

            But, conversely this also means:

            'Most people do X' does not imply 'X is better'.


            In any case, a good programmer often doesn't do 'what most people do'.
            Otherwise he'd be an average programmer.


            SaSW, Willem
            --
            Disclaimer: I am in no way responsible for any of the statements
            made in the above text. For all I know I might be
            drugged or something..
            No I'm not paranoid. You all think I'm paranoid, don't you !
            #EOT

            Comment

            • Willem

              #21
              Re: malloc

              )what is the trade off between the following two allocations
              )>
              )1) p = malloc( sizeof(sn) );
              )2) p = malloc( sizeof(*p) );

              MisterE wrote:

              ) 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.

              I seriously doubt that the 'guidelines' specifically tell you to use
              sizeof(type) rather than sizeof(*ptr). It's more likely that whoever
              wrote the original guidelines simply expressed that you should use sizeof
              when allocating memory, and used sizeof(type) as an example, possibly
              because he was not aware of the sizeof(*ptr) possibility.

              And, of course, after that some braindead clown sees you use
              'sizeof(*ptr)', proceeds to tell you "That doesn't work, because
              sizeof() works on types", and when you prove to him that it does work,
              he proceeds to tell you 'the company standard demands 'sizeof(type)'.


              SaSW, Willem
              --
              Disclaimer: I am in no way responsible for any of the statements
              made in the above text. For all I know I might be
              drugged or something..
              No I'm not paranoid. You all think I'm paranoid, don't you !
              #EOT

              Comment

              • Tejas Kokje

                #22
                Re: malloc

                aarklon@gmail.c om wrote:
                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) );
                I don't think there is any tradeoff as compiler already knows pointer type
                of p and that *p is of type sn. So sizeof operator can use sn or *p.

                However, I would prefer first syntax as it just tell me exactly what data
                type is used in sizeof. In second case, I will have to tagjump to know type
                of p.

                Tejas Kokje

                Comment

                • pete

                  #23
                  Re: malloc

                  Tejas Kokje wrote:
                  what is the trade off between the following two allocations

                  1) p = malloc( sizeof(sn) );
                  2) p = malloc( sizeof(*p) );
                  >
                  I don't think there is any tradeoff as compiler
                  already knows pointer type of p and that *p is of type sn.
                  So sizeof operator can use sn or *p.
                  >
                  However, I would prefer first syntax as it just tell me
                  exactly what data type is used in sizeof.
                  In second case, I will have to tagjump to know type of p.
                  "Type (sn)", doesn't really mean a lot more to me than "type (*p)".

                  --
                  pete

                  Comment

                  • Tejas Kokje

                    #24
                    Re: malloc

                    pete wrote:
                    Tejas Kokje wrote:
                    >>
                    >aarklon@gmail.c om wrote:
                    >
                    what is the trade off between the following two allocations
                    >
                    1) p = malloc( sizeof(sn) );
                    2) p = malloc( sizeof(*p) );
                    >>
                    >I don't think there is any tradeoff as compiler
                    >already knows pointer type of p and that *p is of type sn.
                    >So sizeof operator can use sn or *p.
                    >>
                    >However, I would prefer first syntax as it just tell me
                    >exactly what data type is used in sizeof.
                    >In second case, I will have to tagjump to know type of p.
                    >
                    "Type (sn)", doesn't really mean a lot more to me than "type (*p)".
                    >
                    It certainly means something in my book. It certainly saves me extra step to
                    find type of *p, if I want to know how much memory should be allocated by
                    malloc.

                    Tejas Kokje

                    Comment

                    • pete

                      #25
                      Re: malloc

                      Tejas Kokje wrote:
                      >
                      pete wrote:
                      >
                      Tejas Kokje wrote:
                      what is the trade off between the following two allocations

                      1) p = malloc( sizeof(sn) );
                      2) p = malloc( sizeof(*p) );
                      >
                      I don't think there is any tradeoff as compiler
                      already knows pointer type of p and that *p is of type sn.
                      So sizeof operator can use sn or *p.
                      >
                      However, I would prefer first syntax as it just tell me
                      exactly what data type is used in sizeof.
                      In second case, I will have to tagjump to know type of p.
                      "Type (sn)", doesn't really mean a lot more to me than "type (*p)".
                      >
                      It certainly means something in my book.
                      It certainly saves me extra step to find type of *p,
                      if I want to know how much memory should be allocated by malloc.
                      I'm not following you.

                      You're saying that there could be some situation upon seeing
                      p = malloc(sizeof sn);
                      where I won't need to know what type p points to,
                      but that that knowing how much (sizeof sn) is,
                      will be useful to me.
                      That seems unlikely.


                      p = malloc(sizeof *p)
                      tells me that enough memory has been requested
                      for one object of the type that p points to,
                      without me having to look up anything.

                      --
                      pete

                      Comment

                      • William Pursell

                        #26
                        Re: malloc

                        On Mar 2, 7:20 am, Tejas Kokje <binarysemaph.. .@gmail.comwrot e:
                        pete wrote:
                        Tejas Kokje wrote:
                        >
                        aark...@gmail.c om wrote:
                        >
                        what is the trade off between the following two allocations
                        >
                        1) p = malloc( sizeof(sn) );
                        2) p = malloc( sizeof(*p) );
                        >
                        I don't think there is any tradeoff as compiler
                        already knows pointer type of p and that *p is of type sn.
                        So sizeof operator can use sn or *p.
                        >
                        However, I would prefer first syntax as it just tell me
                        exactly what data type is used in sizeof.
                        In second case, I will have to tagjump to know type of p.
                        >
                        "Type (sn)", doesn't really mean a lot more to me than "type (*p)".
                        >
                        It certainly means something in my book. It certainly saves me extra step to
                        find type of *p, if I want to know how much memory should be allocated by
                        malloc.
                        >
                        It does not save you a step unless you are very careless. If
                        you don't know the type of p, then you don't know if the
                        malloc is correct, since it might not be allocating
                        enough space. If p is not of type sn, but something
                        bigger, then you have a problem. If the allocation
                        is:

                        p = malloc( sizeof *p );

                        then you know it is correct without having to check the
                        declaration of p. If it is:

                        p = malloc( sizeof(T));

                        then you must check the declaration to make sure that
                        p is of type T. So the second form requires an extra
                        step, but the first does not. Many will argue that
                        this justifies the cast, and recommend:

                        p = (T) malloc( sizeof(T));

                        which adds additional complexity and many more steps when
                        the type of p is changed from T to TT.

                        Comment

                        Working...