Copy Constructor for an Array Element ???

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

    #16
    Re: Copy Constructor for an Array Element ???

    > You need #include <new> at the top (or possibly <new.h> for[color=blue]
    > pre-standard compilers). Then it should be accepted by any recent
    > compiler, and certainly MSVC6+, GCC 2.95+, etc.[/color]

    Okay Great, now two out of three compilers accept this, and
    function correctly (invoking the object's copy constructor).

    The problem is that I need it to work on ant AT&T 2.0, pre-template,
    circa 1990 compiler. My whole purpose is to create std::vector for my
    antique compiler.

    I want to have a development machine that is reasonably functional
    and will fit in my pocket. The second requirement eliminates all
    notebook computers and laptops. The first requirement
    (when combined with the second requirement) eliminates
    most everything besides HP Jornada, 690, 720, and 728.

    None of these machines are Intel compatible, thus by themselves will not
    run any compiler. When you add PocketDOS, you can now run any
    software that runs on an 8088, this includes Borland Turbo C++ 1.0.
    It runs on the Jornada's from 2.5 to 7.5 times faster than an 8088.

    The major shortcoming of this old compiler is that lack of std::vector,
    so I wrote one. Now All I need to do is to find the best way to copy
    construct array elements. Do you have any ideas besides <placement new> ?




    Comment

    • Jonathan Turkanis

      #17
      Re: Copy Constructor for an Array Element ???


      "Peter Olcott" <olcott@worldne t.att.net> wrote in message
      news:QZl%b.4517 1$aH3.1403659@b gtnsc04-news.ops.worldn et.att.net...[color=blue][color=green][color=darkred]
      > > > Cy is correct.A vector obtains unitialized memory from an[/color][/color][/color]
      allocator,[color=blue][color=green][color=darkred]
      > > > and initilizes each element, when appropriate, then initializes[/color][/color][/color]
      them[color=blue][color=green][color=darkred]
      > > > using copy constructors with placement new. A default[/color][/color][/color]
      constructor is[color=blue][color=green]
      > >
      > > This came out garbled. It should say:
      > >
      > > A vector obtains unitialized memory from an allocator, then[/color][/color]
      initilizes[color=blue][color=green]
      > > each element, when appropriate, using copy constructors with[/color][/color]
      placement[color=blue][color=green]
      > > new
      > >
      > > Jonathan
      > >[/color]
      > I placed messages in every constructor and destructor of my class,
      > and had std::vector resize() itself. In both of my compilers the[/color]
      number[color=blue]
      > of elements constructed was one more than I asked for. This seems to
      > indicate that the compiler is somehow created an extra temporary
      > object.[/color]

      Was the default constructor ever called by the implementation? It
      shouldn't be -- that was my point.

      Jonathan


      Comment

      • Peter Olcott

        #18
        Re: Copy Constructor for an Array Element ???

        > Was the default constructor ever called by the implementation? It[color=blue]
        > shouldn't be -- that was my point.
        >
        > Jonathan
        >[/color]
        tom_usenet gave the the complete sytax and instructions on how
        to use <placement new>. It worked perfectly on two of my
        three compilers. It did not work on the antique compiler that
        I am developing std::vector for. I need another solution because
        this is the compiler where it needs to work. Pre-template
        AT&T 2.0 Borland Turbo C++ 1.0, circa 1990.


        Comment

        • Jonathan Turkanis

          #19
          Re: Copy Constructor for an Array Element ???


          "Peter Olcott" <olcott@worldne t.att.net> wrote in message
          news:WBy%b.1017 11$hR.1994532@b gtnsc05-news.ops.worldn et.att.net...[color=blue][color=green]
          > > Was the default constructor ever called by the implementation? It
          > > shouldn't be -- that was my point.
          > >
          > > Jonathan
          > >[/color]
          > tom_usenet gave the the complete sytax and instructions on how
          > to use <placement new>. It worked perfectly on two of my
          > three compilers. It did not work on the antique compiler that
          > I am developing std::vector for. I need another solution because
          > this is the compiler where it needs to work. Pre-template
          > AT&T 2.0 Borland Turbo C++ 1.0, circa 1990.[/color]

          I looked more carefully at your original post. I see the problem
          you're trying to solve is constructing objects on uninitialized
          memory. (Sorry I didn't read it more carefully the first time -- I
          mostly noticed the unusual syntax.)

          Placement new is really the only portable way to do this. :(. Since
          you're developing for a specific broken compiler, though, anything
          which works, based on knowledge of the implementation, is fair game.

          I tend to agree with Francis Glassborow, though. I don't think there's
          much point trying to use C++ with such an old compiler.

          Jonathan


          Comment

          • tom_usenet

            #20
            Re: Copy Constructor for an Array Element ???

            On Fri, 27 Feb 2004 00:52:31 GMT, "Peter Olcott"
            <olcott@worldne t.att.net> wrote:
            [color=blue][color=green]
            >> You need #include <new> at the top (or possibly <new.h> for
            >> pre-standard compilers). Then it should be accepted by any recent
            >> compiler, and certainly MSVC6+, GCC 2.95+, etc.[/color]
            >
            >Okay Great, now two out of three compilers accept this, and
            >function correctly (invoking the object's copy constructor).
            >
            >The problem is that I need it to work on ant AT&T 2.0, pre-template,
            >circa 1990 compiler. My whole purpose is to create std::vector for my
            >antique compiler.
            >
            >I want to have a development machine that is reasonably functional
            >and will fit in my pocket. The second requirement eliminates all
            >notebook computers and laptops. The first requirement
            >(when combined with the second requirement) eliminates
            >most everything besides HP Jornada, 690, 720, and 728.
            >
            >None of these machines are Intel compatible, thus by themselves will not
            >run any compiler. When you add PocketDOS, you can now run any
            >software that runs on an 8088, this includes Borland Turbo C++ 1.0.
            >It runs on the Jornada's from 2.5 to 7.5 times faster than an 8088.
            >
            >The major shortcoming of this old compiler is that lack of std::vector,
            >so I wrote one. Now All I need to do is to find the best way to copy
            >construct array elements. Do you have any ideas besides <placement new> ?[/color]

            If you have a copy of the ARM (annotated reference manual), I expect
            that will present a pre-standard syntax that placement new replaced.
            ARM presents C++ as implemented in CFront 3.0...

            I don't have a copy of the book. You could try this:

            #include <stdlib.h>
            #include <stddef.h>

            void* operator new(size_t size, void* ptr)
            {
            return ptr;
            }

            void* operator new[](size_t size, void* ptr)
            {
            return ptr;
            }



            class Foo{};

            int main()
            {
            void* mem = malloc(sizeof(F oo));
            Foo* p = new(mem) Foo();
            }

            That may or may not work...

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

            Comment

            • Peter Olcott

              #21
              Re: Copy Constructor for an Array Element ???

              [color=blue]
              > I looked more carefully at your original post. I see the problem
              > you're trying to solve is constructing objects on uninitialized
              > memory. (Sorry I didn't read it more carefully the first time -- I
              > mostly noticed the unusual syntax.)
              >
              > Placement new is really the only portable way to do this. :(. Since
              > you're developing for a specific broken compiler, though, anything
              > which works, based on knowledge of the implementation, is fair game.
              >
              > I tend to agree with Francis Glassborow, though. I don't think there's
              > much point trying to use C++ with such an old compiler.
              >
              > Jonathan
              >[/color]
              It is my ONLY possible choice within my goal of having a reasonably
              functional development machine that will fit in my pocket. ** I have spent
              twelve thousand hours working on my softare invention. I would like
              to do some of this work without the restriction of staying home, or lugging
              around a laptop in a knapsack. In terms of getting some of the basic
              ideas needed for my invention worked out, this antique compiler will
              suffice. Its major shortcoming is the lack of a std::vector. I have since
              created a very efficient std::vector that impements all of the subset of
              functionality that I will ever need. http://home.att.net/~olcott/std_vect.html

              I guess that I will just have to retain my current method of requiring
              all classes that declare their own constructors, and destructors, to
              duplicate this functionality in member functions named:
              Destruct()
              Construct(()
              Construct(&)

              ** The ONLY reasonably functional development machine that will
              fit in one's pocket is an HP Jornada 690, 720, or 728. Every other
              machine either will not fit in the pocket, or is not reasonably functional.
              Having to type with a stylus is not reasonable. The ONLY way that
              this machine can be used for C++ development is by adding PocketDOS
              (and 8088 emulator), and Borland Turbo C++ 1.0 (Currently freeware).


              Comment

              • Ron Natalie

                #22
                Re: Copy Constructor for an Array Element ???


                "Peter Olcott" <olcott@worldne t.att.net> wrote in message news:QZl%b.4517 1$aH3.1403659@b gtnsc04-[color=blue]
                > I placed messages in every constructor and destructor of my class,
                > and had std::vector resize() itself. In both of my compilers the number
                > of elements constructed was one more than I asked for. This seems to
                > indicate that the compiler is somehow created an extra temporary
                > object.[/color]

                Resize and reserve do different things.
                Reserve just allocates space (possibly moving existing objects in the process).
                Resize increases the size() of the vector. In addition to copying the existing
                elements, it also needs to fill the elements above the existing size with it's second
                argument (which is by default a default constructed object, but you could pass
                in anything).

                Resize(10) should therefore be expected to call a default constructor once and
                ten copy constructors (to either move an existing element or to copy the default
                constructed prototype).

                Comment

                • Peter Olcott

                  #23
                  Re: Copy Constructor for an Array Element ???

                  > Resize and reserve do different things.[color=blue]
                  > Reserve just allocates space (possibly moving existing objects in the process).[/color]
                  It is this "possibly moving objects in the process" that I need to be able
                  to have a copy constructor for an array element. With modern compilers
                  this can be easily accomplished with <placement new>. This feature is
                  not available on AT&T 2.0, which is my required development platform.
                  Got any good ideas on the best way to do this?
                  [color=blue]
                  > Resize increases the size() of the vector. In addition to copying the existing
                  > elements, it also needs to fill the elements above the existing size with it's second
                  > argument (which is by default a default constructed object, but you could pass
                  > in anything).
                  >
                  > Resize(10) should therefore be expected to call a default constructor once and
                  > ten copy constructors (to either move an existing element or to copy the default
                  > constructed prototype).[/color]



                  Comment

                  • Peter Olcott

                    #24
                    Re: Copy Constructor for an Array Element ???

                    > If you have a copy of the ARM (annotated reference manual), I expect[color=blue]
                    > that will present a pre-standard syntax that placement new replaced.
                    > ARM presents C++ as implemented in CFront 3.0...
                    >
                    > I don't have a copy of the book. You could try this:
                    >
                    > #include <stdlib.h>
                    > #include <stddef.h>
                    >
                    > void* operator new(size_t size, void* ptr)
                    > {
                    > return ptr;
                    > }
                    >
                    > void* operator new[](size_t size, void* ptr)
                    > {
                    > return ptr;
                    > }
                    >
                    >
                    >
                    > class Foo{};
                    >
                    > int main()
                    > {
                    > void* mem = malloc(sizeof(F oo));
                    > Foo* p = new(mem) Foo();
                    > }
                    >
                    > That may or may not work...[/color]


                    Below is the "official" work-around. I also included the usenet URL where this material
                    was found. I could not get this work-around to work. Can you see why this would not
                    make the syntax that you provided function correctly?

                    //######### ######### ######### ######### ######### ######### ######### #########
                    //######### Bug report version 1.01.6 and earlier starts here #########
                    //######### ######### ######### ######### ######### ######### ######### #########
                    //
                    // Severity: Missing header file
                    // Workaround: Provided below
                    // Versions: Occurs in both TC++ 1.00 and 1.01
                    // Fixed in: Fixed in 2.0

                    // There is no <new.h>. I constructed the following work-alike. It should go
                    // into your standard include directory (where <iostream.h> is, for example):
                    // new.h
                    // Author: Dr. Marshall Cline/ECE Dept/Clarkson Univ/Potsdam,NY 13676
                    // Email: cline@sun.soe.c larkson.edu
                    // Phone: Voice: 315-268-6511; Fax: 315-268-7600
                    // Copyright: The Author releases this to the Public Domain, 9-July-90.
                    // Date: 9-July-1990
                    // Please include these acknowledgement s when distributing this file
                    #ifndef __NEW_H
                    #define __NEW_H
                    #ifndef _SIZE_T
                    #define _SIZE_T
                    typedef unsigned size_t;
                    #endif
                    void* operator new(size_t size, void* ptr);
                    // _new_handler is a ptr to a parameterless function returning void
                    extern void (*_new_handler) ();
                    void (*set_new_handl er(void (*replacement_h andler)()))();
                    #endif __NEW_H

                    // Borland says:
                    // NEW.H is NOT defined by AT&T 2.0 C++ specs. The contents
                    // were in C++ specs 1.2 and is making a comeback in the
                    // upcoming ANSI C++ spec. We do have the underlying code, ie
                    // _new_handler support, but it is not documented. It would be
                    // nicer to use a typedef for the void function pointer, for the
                    // above work-around.
                    //
                    //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
                    // http://groups.google.com/groups?selm...n.edu&oe=UTF-8


                    Comment

                    • Peter Olcott

                      #25
                      Re: Copy Constructor for an Array Element ???

                      > #include <stdlib.h>[color=blue]
                      > #include <stddef.h>
                      >
                      > void* operator new(size_t size, void* ptr)
                      > {
                      > return ptr;
                      > }
                      >
                      > void* operator new[](size_t size, void* ptr)
                      > {
                      > return ptr;
                      > }
                      >
                      >
                      >
                      > class Foo{};
                      >
                      > int main()
                      > {
                      > void* mem = malloc(sizeof(F oo));
                      > Foo* p = new(mem) Foo();
                      > }[/color]

                      Preliminary indications are that this idea works.
                      I am still working through the details. Thanks for
                      your help.


                      Comment

                      • Peter Olcott

                        #26
                        Re: Copy Constructor for an Array Element ???

                        > void* operator new(size_t size, void* ptr)[color=blue]
                        > {
                        > return ptr;
                        > }
                        >[/color]

                        This has become my <new> header file. This succinct advice, along with
                        the <placement new> syntax and instructions has amounted to more help
                        than everyone else put together. Since I was also able to stumble across the
                        Ye Olde syntax for delete[], (which is delete [int]) I will be able to adapt
                        my current std::vector (for pre-template compilers) so that it has identical
                        usage syntax and requirements the STL versions.

                        //
                        //
                        // A Turbo C++ 1.0 Workaround that permits <placement new>
                        // provided by "tom_usenet " <tom_usenet@hot mail.com> from
                        // the comp.lang.c++ newsgroup.
                        //
                        //
                        #pragma warn -par
                        void* operator new(size_t size, void* ptr)
                        {
                        return ptr;
                        }

                        Thanks again Tom for the excellent help !


                        Comment

                        • Ron Natalie

                          #27
                          Re: Copy Constructor for an Array Element ???


                          "Peter Olcott" <olcott@worldne t.att.net> wrote in message news:ulP%b.5054 3$aH3.1548049@b gtnsc04-news.ops.worldn et.att.net...[color=blue][color=green]
                          > > Resize and reserve do different things.
                          > > Reserve just allocates space (possibly moving existing objects in the process).[/color]
                          > It is this "possibly moving objects in the process" that I need to be able
                          > to have a copy constructor for an array element.[/color]

                          It's one of the reasons. The other reason is to copy in the "fill" object
                          on resize. This avoids:
                          1. requiring a default constructor for the contained object.
                          2. creating an object that's just going to be copied over (a slight efficiency)
                          [color=blue]
                          > With modern compilers
                          > this can be easily accomplished with <placement new>. This feature is
                          > not available on AT&T 2.0, which is my required development platform.
                          > Got any good ideas on the best way to do this?[/color]

                          Well, if you're willing to put some limitations on std::vector, for example that
                          the object must be default constructable, you could just default construct the
                          new allocation, and then use assignment.

                          Comment

                          Working...