renew() an array?

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

    renew() an array?

    Hello friends:

    Say I allocate an array with
    a = new int(100);

    Later I want to grow the array to size 200. Do I need to allocate
    another array and copy the first one to it? Is there a simpler approach
    (like realloc() - my compiler doesn't recognize renew() as a function).

    Thanks.
  • Andrey Tarasevich

    #2
    Re: renew() an array?

    pradeep wrote:
    Say I allocate an array with
    a = new int(100);
    That does not allocate an array. This does

    a = new int[100];
    Later I want to grow the array to size 200. Do I need to allocate
    another array and copy the first one to it?
    Well, yes, as long as you are insisting on using 'new' to allocate arrays.
    Is there a simpler approach
    (like realloc() - my compiler doesn't recognize renew() as a function).
    The simpler approach would be to use 'std::vector' and leave the details
    to the implementation.

    --
    Best regards,
    Andrey Tarasevich

    Comment

    • Antoninus Twink

      #3
      Re: renew() an array?

      On 25 Apr 2008 at 22:15, pradeep wrote:
      Hello friends:
      >
      Say I allocate an array with
      a = new int(100);
      I think you want square brackets.
      Later I want to grow the array to size 200. Do I need to allocate
      another array and copy the first one to it? Is there a simpler approach
      (like realloc() - my compiler doesn't recognize renew() as a function).
      No.

      Why not use std::vector, which provides dynamic resizing via resize(),
      insert(), and the like?

      Comment

      • santosh

        #4
        Re: renew() an array?

        pradeep wrote:
        Hello friends:
        >
        Say I allocate an array with
        a = new int(100);
        >
        Later I want to grow the array to size 200. Do I need to allocate
        another array and copy the first one to it? Is there a simpler
        approach (like realloc() - my compiler doesn't recognize renew() as a
        function).
        This is a C++ question as far as I can see. Try comp.lang.c++ instead of
        comp.lang.c.

        Comment

        • Walter Roberson

          #5
          Re: renew() an array?

          In article <futlbk$sb3$1@a ioe.org>,
          Andrey Tarasevich <andreytarasevi ch@hotmail.comw rote:
          >pradeep wrote:
          >Say I allocate an array with
          >a = new int(100);
          >That does not allocate an array. This does
          a = new int[100];
          This is comp.lang.c . The above is a syntax error in C.

          To the original poster: 'new' is not part of C. Please consult
          a newsgroup for whatever programming language you are using.
          For example, if you are using C++ then comp.lang.c++ would be appropriate.
          --
          "The first draught serveth for health, the second for pleasure,
          the third for shame, the fourth for madness." -- Sir Walter Raleigh

          Comment

          • Martin Ambuhl

            #6
            Re: renew() an array?

            pradeep wrote:
            Hello friends:
            >
            Say I allocate an array with
            a = new int(100);
            Then you want a language other than C. As that line stands, it is a
            syntax error.
            >
            Later I want to grow the array to size 200.
            Even in (shudder) the bloat-de-jure C++ language, that's the wrong way
            to do it. Consider using the <off-topicvector class </off-topic>.
            Do I need to allocate
            another array and copy the first one to it? Is there a simpler approach
            (like realloc() - my compiler doesn't recognize renew() as a function).
            If it were a C compiler, it would have vomited at the earlier line of code,

            Comment

            • Keith Thompson

              #7
              Re: renew() an array?

              pradeep <nospam@nospam. comwrites:
              Say I allocate an array with
              a = new int(100);
              >
              Later I want to grow the array to size 200. Do I need to allocate
              another array and copy the first one to it? Is there a simpler
              approach (like realloc() - my compiler doesn't recognize renew() as a
              function).
              This is a C++ question. It's also a frequently asked C++ question.
              You'll likely find all the information you need in the C++ FAQ at
              <http://www.parashift.c om/c++-faq-lite/>, particularly section 16. If
              you have further questions, please post to comp.lang.c++.

              --
              Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
              Nokia
              "We must do something. This is something. Therefore, we must do this."
              -- Antony Jay and Jonathan Lynn, "Yes Minister"

              Comment

              Working...