Two C++ questions

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

    #31
    Re: Two C++ questions

    Julie wrote:
    [color=blue]
    > for me, it
    > is easier to consider allocations through new to be reserved for instances of
    > objects, and allocations through malloc to be raw memory.[/color]

    It is good that you're keeping a conceptual line between raw memory and
    typed memory. I don't think avoiding "new" for the raw memory is the
    clearest way to express this, though. On the rare occasions when I
    really want some raw memory, I typedef unsigned char to a type called
    Byte, and work with Bytes as my raw memory. I've found type and
    variable names the best places in my code to "self-document."

    int main ( )
    {
    typedef unsigned char Byte;

    Byte* raw_memory = new Byte[ 100 ];

    // Work with raw memory...

    delete [ ] raw_memory;
    }
    [color=blue]
    > Derived * p = new Derived[15];
    >
    > Try to explain in simple what p points to, and what you can and can't do w/ the
    > underlying allocated memory.[/color]

    p points to the first element in an array of Derived objects.
    [color=blue]
    > Contrast that with:
    >
    > char * c = new char[15];[/color]

    c points to the first element in an array of char.
    [color=blue]
    > and why you can treat it differently.[/color]

    Because Derived is not POD but char is.

    Comment

    • tom_usenet

      #32
      Re: Two C++ questions

      On Tue, 27 Apr 2004 11:04:11 -0700, Julie <julie@nospam.c om> wrote:
      [color=blue]
      >Claudio Puviani wrote:[color=green][color=darkred]
      >> > If you are going to use the std functions, allocate the memory w/ new.[/color]
      >>
      >> The two concepts are COMPLETELY unrelated. There is nothing practical or
      >> theoretical that correlates how and where memory is allocated with the way it
      >> should be manipulated.[/color]
      >
      >You don't allocate memory w/ new, you allocate space for instances of objects,
      >be they classes or POD types.[/color]

      void* mem = ::operator new(42);
      [color=blue]
      >malloc is the only way to allocate 'memory'.[/color]

      Really!?

      Tom
      --
      C++ FAQ: http://www.parashift.com/c++-faq-lite/
      C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

      Comment

      • tom_usenet

        #33
        Re: Two C++ questions

        On Tue, 27 Apr 2004 11:29:47 -0700, Julie <julie@nospam.c om> wrote:
        [color=blue]
        >Claudio Puviani wrote:[color=green]
        >> There's no need whatsoever in C++ to use malloc/free, except to support legacy
        >> code. new/delete should be used.[/color]
        >
        >Explain to me how you reallocate a block to a smaller size using new w/o
        >copying?
        >
        >I would have contemplated adding the following to the C++ language to
        >accommodate the allocation of (uninitialized) raw blocks of memory:
        >
        >void * p = new void[size_in_bytes];
        >
        >Then it would be possible to make the very real distinction between memory and
        >instance allocation.[/color]

        Read up on operator new.

        Tom
        --
        C++ FAQ: http://www.parashift.com/c++-faq-lite/
        C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

        Comment

        • jeffc

          #34
          Re: Two C++ questions


          "Pat" <Pat@Pat.com> wrote in message news:408dfb57$1 _1@rain.i-cable.com...[color=blue]
          > Thanks all of you.
          >
          > Pat[/color]

          Don't talk while we're interrupting!
          :-)


          Comment

          • Julie

            #35
            Re: Two C++ questions

            Jeff Schwab wrote:[color=blue]
            >
            > Julie wrote:
            >[color=green]
            > > for me, it
            > > is easier to consider allocations through new to be reserved for instances of
            > > objects, and allocations through malloc to be raw memory.[/color]
            >
            > It is good that you're keeping a conceptual line between raw memory and
            > typed memory. I don't think avoiding "new" for the raw memory is the
            > clearest way to express this, though. On the rare occasions when I
            > really want some raw memory, I typedef unsigned char to a type called
            > Byte, and work with Bytes as my raw memory. I've found type and
            > variable names the best places in my code to "self-document."
            >
            > int main ( )
            > {
            > typedef unsigned char Byte;
            >
            > Byte* raw_memory = new Byte[ 100 ];
            >
            > // Work with raw memory...
            >
            > delete [ ] raw_memory;
            > }
            >[color=green]
            > > Derived * p = new Derived[15];
            > >
            > > Try to explain in simple what p points to, and what you can and can't do w/ the
            > > underlying allocated memory.[/color]
            >
            > p points to the first element in an array of Derived objects.
            >[color=green]
            > > Contrast that with:
            > >
            > > char * c = new char[15];[/color]
            >
            > c points to the first element in an array of char.[/color]

            Right! Which, conceptually, should be treated differently than an allocated
            block of raw memory.
            [color=blue][color=green]
            > > and why you can treat it differently.[/color]
            >
            > Because Derived is not POD but char is.[/color]

            Right! I understand that there are special-class types, but feel that it can
            be a sticking point for new learners of the language. Personally, I have no
            problem w/ it because I stated w/ C and therefore had a basic understanding of
            the POD types.

            Part of my point is that you have to treat char as a non-POD type AND as an
            allocation type for general purpose (raw) memory.

            As I've said before, I think of it being more beneficial to think of new as an
            allocator of instances of types, and malloc as an allocator of raw (untyped)
            memory. My use of malloc is few and far between, so when it does happen, it is
            usually very localized or well defined, and I have no problem keeping track of
            the allocation scheme.

            As I've stated, I would have preferred that new provide a method by which raw
            (untyped) memory could be allocated, rather than allocating chars and calling
            it raw. I've proposed

            void * p = new void[size_in_bytes];

            as reasonable candidate for such, although I realize that no such beast will be
            making it into the standard, nor is this the forum to discuss such features.

            Comment

            • Julie

              #36
              Re: Two C++ questions

              tom_usenet wrote:[color=blue]
              >
              > On Tue, 27 Apr 2004 11:04:11 -0700, Julie <julie@nospam.c om> wrote:
              >[color=green]
              > >Claudio Puviani wrote:[color=darkred]
              > >> > If you are going to use the std functions, allocate the memory w/ new.
              > >>
              > >> The two concepts are COMPLETELY unrelated. There is nothing practical or
              > >> theoretical that correlates how and where memory is allocated with the way it
              > >> should be manipulated.[/color]
              > >
              > >You don't allocate memory w/ new, you allocate space for instances of objects,
              > >be they classes or POD types.[/color]
              >
              > void* mem = ::operator new(42);[/color]

              I was not aware of this usage of new. Thank you for pointing it out.

              I ammend my comments:

              Raw memory should be allocated using the method you describe. Raw memory
              allocated through new char[size] should be considered 'bad style'.
              [color=blue]
              >[color=green]
              > >malloc is the only way to allocate 'memory'.[/color]
              >
              > Really!?[/color]

              I was talking conceptually. I prefer to consider allocations (excepting your
              method above) with new as allocations of instances of object(s).
              [color=blue]
              >
              > Tom
              > --
              > C++ FAQ: http://www.parashift.com/c++-faq-lite/
              > C FAQ: http://www.eskimo.com/~scs/C-faq/top.html[/color]

              Comment

              • Julie

                #37
                Re: Two C++ questions

                tom_usenet wrote:[color=blue]
                >
                > On Tue, 27 Apr 2004 11:29:47 -0700, Julie <julie@nospam.c om> wrote:
                >[color=green]
                > >Claudio Puviani wrote:[color=darkred]
                > >> There's no need whatsoever in C++ to use malloc/free, except to support legacy
                > >> code. new/delete should be used.[/color]
                > >
                > >Explain to me how you reallocate a block to a smaller size using new w/o
                > >copying?
                > >
                > >I would have contemplated adding the following to the C++ language to
                > >accommodate the allocation of (uninitialized) raw blocks of memory:
                > >
                > >void * p = new void[size_in_bytes];
                > >
                > >Then it would be possible to make the very real distinction between memory and
                > >instance allocation.[/color]
                >
                > Read up on operator new.[/color]

                Done, thanks for pointing that out.

                Regardless, I personally think that the intention of new void[] is somewhat
                more clear, but this isn't the forum for such discussions.

                Comment

                • tom_usenet

                  #38
                  Re: Two C++ questions

                  On Wed, 28 Apr 2004 08:38:28 -0700, Julie <julie@nospam.c om> wrote:
                  [color=blue]
                  >I ammend my comments:
                  >
                  >Raw memory should be allocated using the method you describe. Raw memory
                  >allocated through new char[size] should be considered 'bad style'.[/color]

                  Why? The lifetime of a POD object begins when storage of suitable
                  alignment becomes available. However, there doesn't seem a compelling
                  reason not to use operator new rather than new char[] - it may be more
                  efficient (in space) for one.
                  [color=blue][color=green][color=darkred]
                  >> >malloc is the only way to allocate 'memory'.[/color]
                  >>
                  >> Really!?[/color]
                  >
                  >I was talking conceptually. I prefer to consider allocations (excepting your
                  >method above) with new as allocations of instances of object(s).[/color]

                  You may prefer to consider allocations with new as allocations of
                  instances of object(s), but for POD types, it is no different from
                  using malloc or any other memory allocator. You know that of course,
                  so you're overstating your case somewhat.

                  Tom
                  --
                  C++ FAQ: http://www.parashift.com/c++-faq-lite/
                  C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

                  Comment

                  • Claudio Puviani

                    #39
                    Re: Two C++ questions

                    "Julie" <julie@nospam.c om> wrote[color=blue]
                    >
                    > I ammend my comments:
                    >
                    > Raw memory should be allocated using the method you describe. Raw memory
                    > allocated through new char[size] should be considered 'bad style'.[/color]

                    Oh, for crying out loud! Just what credentials do you have to presume to dictate
                    style to people who have orders of magnitude more experience and knowledge than
                    you? So far, I haven't seen the slightest indication that you'd even recognize
                    good style, let alone be in a position to define it for others. You can say, "I
                    personally don't like that style," but it's laughable for you to try to define
                    what "good" style is on the basis of your ignorance of the language and its
                    idioms.

                    Claudio Puviani


                    Comment

                    • Julie

                      #40
                      Re: Two C++ questions

                      Claudio Puviani wrote:[color=blue]
                      >
                      > "Julie" <julie@nospam.c om> wrote[color=green]
                      > >
                      > > I ammend my comments:
                      > >
                      > > Raw memory should be allocated using the method you describe. Raw memory
                      > > allocated through new char[size] should be considered 'bad style'.[/color]
                      >
                      > Oh, for crying out loud! Just what credentials do you have to presume to dictate
                      > style to people who have orders of magnitude more experience and knowledge than
                      > you?[/color]

                      I wasn't trying to dictate -- I should have prefaced that with "I prefer to
                      consider ..."
                      [color=blue]
                      > So far, I haven't seen the slightest indication that you'd even recognize
                      > good style,[/color]

                      I don't know that I've tried to indicate good style, as I usually stay out of
                      those discussions. Feel free to arrive at whatever conclusion you desire, I'm
                      not trying to impress you or anyone else on this forum.

                      Regardless, do you consider the following 'good style' or an 'accepted idiom'?

                      // allocate raw memory for image bits
                      void * p = (void *)new char[image_size];

                      (Note: I'm using C-style casts for brevity, substitute C++ style casts if you
                      prefer, the underlying idea is the same.)

                      Personally, I consider that there are more appropriate ways to allocate raw
                      memory rather than confusing the issue w/ a placeholder type such as char.

                      Previously, as I've stated, in the past I've used malloc for the allocation of
                      what I considered raw memory, and new char[] for the allocation of character
                      arrays. In the future, I'll use ::operator new(size) for allocations of raw
                      memory, and new char[] for the allocation of characters arrays.

                      Comment

                      • Andre Kostur

                        #41
                        Re: Two C++ questions

                        Julie <julie@nospam.c om> wrote in news:40900792.E 5E4B20B@nospam. com:

                        [color=blue]
                        > Regardless, do you consider the following 'good style' or an 'accepted
                        > idiom'?
                        >
                        > // allocate raw memory for image bits
                        > void * p = (void *)new char[image_size];
                        >
                        > (Note: I'm using C-style casts for brevity, substitute C++ style casts
                        > if you prefer, the underlying idea is the same.)[/color]

                        Um... why cast at all since you get the conversion to void * for free?

                        Comment

                        • Dave Moore

                          #42
                          Re: Two C++ questions

                          Julie <julie@nospam.c om> wrote in message news:<408FCECC. 20BEF807@nospam .com>...[color=blue]
                          > Jeff Schwab wrote:[color=green]
                          > >
                          > > Julie wrote:
                          > >[color=darkred]
                          > > > for me, it
                          > > > is easier to consider allocations through new to be reserved for instances of
                          > > > objects, and allocations through malloc to be raw memory.[/color]
                          > >
                          > > It is good that you're keeping a conceptual line between raw memory and
                          > > typed memory. I don't think avoiding "new" for the raw memory is the
                          > > clearest way to express this, though. On the rare occasions when I
                          > > really want some raw memory, I typedef unsigned char to a type called
                          > > Byte, and work with Bytes as my raw memory. I've found type and
                          > > variable names the best places in my code to "self-document."
                          > >
                          > > int main ( )
                          > > {
                          > > typedef unsigned char Byte;
                          > >
                          > > Byte* raw_memory = new Byte[ 100 ];
                          > >
                          > > // Work with raw memory...
                          > >
                          > > delete [ ] raw_memory;
                          > > }[/color]
                          >[color=green][color=darkred]
                          > > > Derived * p = new Derived[15];
                          > > >
                          > > > Try to explain in simple what p points to, and what you can and can't do w/ the
                          > > > underlying allocated memory.[/color]
                          > >
                          > > p points to the first element in an array of Derived objects.
                          > >[color=darkred]
                          > > > Contrast that with:
                          > > >
                          > > > char * c = new char[15];[/color]
                          > >
                          > > c points to the first element in an array of char.[/color]
                          >
                          > Right! Which, conceptually, should be treated differently than an allocated
                          > block of raw memory.
                          >[color=green][color=darkred]
                          > > > and why you can treat it differently.[/color]
                          > >
                          > > Because Derived is not POD but char is.[/color]
                          >
                          > Right! I understand that there are special-class types, but feel that it can
                          > be a sticking point for new learners of the language. Personally, I have no
                          > problem w/ it because I stated w/ C and therefore had a basic understanding of
                          > the POD types.
                          >
                          > Part of my point is that you have to treat char as a non-POD type AND as an
                          > allocation type for general purpose (raw) memory.
                          >
                          > As I've said before, I think of it being more beneficial to think of new as an
                          > allocator of instances of types, and malloc as an allocator of raw (untyped)
                          > memory. My use of malloc is few and far between, so when it does happen, it is
                          > usually very localized or well defined, and I have no problem keeping track of
                          > the allocation scheme.
                          >
                          > As I've stated, I would have preferred that new provide a method by which raw
                          > (untyped) memory could be allocated, rather than allocating chars and calling
                          > it raw. I've proposed
                          >
                          > void * p = new void[size_in_bytes];
                          >
                          > as reasonable candidate for such, although I realize that no such beast will be
                          > making it into the standard, nor is this the forum to discuss such features.[/color]

                          So if C++ doesn't deal directly with raw memory, then what is
                          std::raw_memory _iterator for? I thought it was explicitly for
                          stepping through bytes allocated like:

                          void *p = new char[user_defined_si ze];

                          Dave Moore

                          Comment

                          • Jacek Dziedzic

                            #43
                            Re: Two C++ questions

                            Julie wrote:
                            [color=blue]
                            > Regardless of what the answer is, I'd say that this is a definite case of 'bad
                            > style'.[/color]

                            Well, if memset() clears a 1GB array five times faster than
                            any other method (in my implementation, that is) it's not bad style,
                            it's efficiency. I'm not one who falls prey to over-optimizing
                            to gain microseconds, but when we're talking about minutes
                            (clearing an array within a tight loop), I can't resist using
                            memset(), if not going for an compiler-specific even-faster
                            alternative (out of scope of this ng, of course).
                            [color=blue]
                            > If you are going to use mem* functions, allocate the memory w/ malloc
                            > and friends.[/color]

                            Now that I was "made sure" (thanks, John!) that clearing a new'ed
                            array is perfectly safe, I cannot accept your concept of having to
                            introduce one ugly-low-level-C'ism whenever I'm forced to use
                            another ugly-low-level-C'ism -- that's weird. Actually (I have
                            almost no C background) I cannot imagine a rationale for using
                            malloc() ever -- is there any? I'd rather use ::new and allocate
                            char's, at least I don't have to double check it didn't fail.

                            - J.

                            Comment

                            • Julie

                              #44
                              Re: Two C++ questions

                              Andre Kostur wrote:[color=blue]
                              >
                              > Julie <julie@nospam.c om> wrote in news:40900792.E 5E4B20B@nospam. com:
                              >[color=green]
                              > > Regardless, do you consider the following 'good style' or an 'accepted
                              > > idiom'?
                              > >
                              > > // allocate raw memory for image bits
                              > > void * p = (void *)new char[image_size];
                              > >
                              > > (Note: I'm using C-style casts for brevity, substitute C++ style casts
                              > > if you prefer, the underlying idea is the same.)[/color]
                              >
                              > Um... why cast at all since you get the conversion to void * for free?[/color]

                              I was including it for clarity and documentative purposes, making it clear to
                              the reader that I was deliberately storing in a void *.

                              Comment

                              • Old Wolf

                                #45
                                Re: Two C++ questions

                                Julie <julie@nospam.c om> wrote:[color=blue]
                                > Raw memory allocated through new char[size]
                                > should be considered 'bad style'.[/color]

                                I agree, if you mean that you prefer using std::vector to allocate
                                raw memory, as guaranteed by C++03:

                                std::vector<uns igned char> raw(200);
                                unsigned char *ptr = &raw[0];

                                This has the advantage of cleaning up automatically, and being
                                able to re-size and preserve the contents in an exception-safe manner.

                                Comment

                                Working...