renew() an array?

Discussion in 'C Programming' started by pradeep, Apr 25, 2008.

  1. pradeep

    pradeep Guest

    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.
    pradeep, Apr 25, 2008
    #1
    1. Advertising

  2. 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
    Andrey Tarasevich, Apr 25, 2008
    #2
    1. Advertising

  3. 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?
    Antoninus Twink, Apr 25, 2008
    #3
  4. pradeep

    santosh Guest

    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.
    santosh, Apr 25, 2008
    #4
  5. In article <futlbk$sb3$>,
    Andrey Tarasevich <> wrote:
    >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
    Walter Roberson, Apr 25, 2008
    #5
  6. 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-topic> vector 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,
    Martin Ambuhl, Apr 26, 2008
    #6
  7. pradeep <> writes:
    > 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.com/c++-faq-lite/>, particularly section 16. If
    you have further questions, please post to comp.lang.c++.

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Apr 26, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. A Traveler

    Renew IIS session with an image request?

    A Traveler, Oct 11, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    566
    George Hester
    Oct 12, 2004
  2. TheDude5B

    renew the sessionID

    TheDude5B, Mar 9, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    458
    Alvin Bruney - ASP.NET MVP
    Mar 9, 2006
  3. Anonymous Sender

    gnarlodious--did you try to renew?

    Anonymous Sender, May 26, 2004, in forum: HTML
    Replies:
    1
    Views:
    296
    Gnarlodious
    May 26, 2004
  4. Tarapia Tapioco

    gnarlodious--did you try to renew?

    Tarapia Tapioco, May 26, 2004, in forum: HTML
    Replies:
    0
    Views:
    322
    Tarapia Tapioco
    May 26, 2004
  5. SG
    Replies:
    8
    Views:
    2,691
    Jerry Coffin
    Sep 11, 2009
Loading...

Share This Page