realloc in C++

Discussion in 'C++' started by divya_rathore_@gmail.com, Jan 7, 2007.

  1. Guest

    Old question in a new bottle:

    What if i delete a 1D array and again use new to allocate a bigger (or
    smaller) size?
    is this ok? Feel free to shoot me!

    int *array;
    int size = 100;

    //allocate memory for 'size' no of integers.
    array = new int[size];
    //fill up the 'array'.
    for (int i=0; i<100; i++) array = i;

    //delete data pointed to by 'array'
    delete[] array;

    //again allocate but a bigger size!!
    //is this ok??
    array = new int[200];
    //fill up the 'array'.
    for (int i=0; i<200; i++) array = i;

    //delete data pointed to by newly allocated 'array'
    delete[] array;


    Compiler specifics: Microsoft visual studio ver 6.0 and higher.
    What I know: it can be done using STL, I KNOW. But how right/wrong is
    the above approach?
    , Jan 7, 2007
    #1
    1. Advertising

  2. wrote:

    > What if i delete a 1D array and again use new to allocate a bigger (or


    You never 'allocate again', you just allocate. The fact that you assign the
    result of the allocation to the same pointer than a previous allocation is
    completely irrelevant.

    --
    Salu2
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Jan 7, 2007
    #2
    1. Advertising

  3. Guest

    On Jan 8, 4:49 am, Julián Albo <> wrote:
    > wrote:
    > > What if i delete a 1D array and again use new to allocate a bigger (orYou never 'allocate again', you just allocate. The fact that you assign the

    > result of the allocation to the same pointer than a previous allocation is
    > completely irrelevant.
    >
    > --
    > Salu2


    pardon me.. but that means..?
    Would it work or not?

    thanks in advance!
    , Jan 8, 2007
    #3
  4. Guest

    >
    > > --
    > > Salu2pardon me.. but that means..?

    > Would it work or not?
    >


    and moreover, is it an acceptable practice?

    thanks again in advance..
    cheers!
    , Jan 8, 2007
    #4
  5. Jim Langston Guest

    "" <> wrote in message
    news:...
    > >
    >> > --
    >> > Salu2pardon me.. but that means..?

    >> Would it work or not?
    >>

    >
    > and moreover, is it an acceptable practice?
    >
    > thanks again in advance..
    > cheers!


    Yes it would work. Yes, I've seen it done. Where you would do this depends
    on your program.

    I can see std::vector doing this exact thing (not sure if it does, but I
    could see it implemented this way).
    Jim Langston, Jan 8, 2007
    #5
  6. wrote:

    >> pardon me.. but that means..?
    >> Would it work or not?

    > and moreover, is it an acceptable practice?


    It works and is acceptable.

    --
    Salu2
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Jan 8, 2007
    #6
  7. Daniel T. Guest

    "" <> wrote:

    > Old question in a new bottle:
    >
    > What if i delete a 1D array and again use new to allocate a bigger (or
    > smaller) size?
    > is this ok?


    Yes, it is OK, though I suggest you use std::vector instead.

    > int *array;
    > int size = 100;


    I believe the above needs to be a const int.

    > //allocate memory for 'size' no of integers.
    > array = new int[size];
    > //fill up the 'array'.
    > for (int i=0; i<100; i++) array = i;
    >
    > //delete data pointed to by 'array'
    > delete[] array;
    >
    > //again allocate but a bigger size!!
    > //is this ok??
    > array = new int[200];
    > //fill up the 'array'.
    > for (int i=0; i<200; i++) array = i;
    >
    > //delete data pointed to by newly allocated 'array'
    > delete[] array;
    Daniel T., Jan 8, 2007
    #7
  8. Guest

    On Jan 8, 6:31 am, Julián Albo <> wrote:
    > wrote:
    > >> pardon me.. but that means..?
    > >> Would it work or not?

    > > and moreover, is it an acceptable practice?


    >It works and is acceptable.
    >
    > --
    > Salu2


    Thanks a lot, Jim and Julián! I will do some tests and revert back
    with my observations.
    thanks again :)

    - divya rathore
    (remove all underscores for email ID)
    , Jan 8, 2007
    #8
  9. "Daniel T." <> wrote in message
    news:...
    > "" <> wrote:
    >
    >> Old question in a new bottle:
    >>
    >> What if i delete a 1D array and again use new to allocate a bigger (or
    >> smaller) size?
    >> is this ok?

    >
    > Yes, it is OK, though I suggest you use std::vector instead.
    >
    >> int *array;
    >> int size = 100;

    >
    > I believe the above needs to be a const int.


    Why? For the 'array = new int[size];' statement? That's not true, of course
    new array expressions can take nonconst integrals, otherwise they would be
    pretty useless :)

    - Sylvester
    Sylvester Hesp, Jan 8, 2007
    #9
  10. Daniel T. Guest

    "Sylvester Hesp" <> wrote:
    > "Daniel T." <> wrote:
    > > "" <> wrote:
    > >
    > >> Old question in a new bottle:
    > >>
    > >> What if i delete a 1D array and again use new to allocate a bigger (or
    > >> smaller) size?
    > >> is this ok?

    > >
    > > Yes, it is OK, though I suggest you use std::vector instead.
    > >
    > >> int *array;
    > >> int size = 100;

    > >
    > > I believe the above needs to be a const int.

    >
    > Why? For the 'array = new int[size];' statement? That's not true, of course
    > new array expressions can take nonconst integrals, otherwise they would be
    > pretty useless :)


    True, my mistake. I was thinking about auto arrays.
    Daniel T., Jan 8, 2007
    #10
    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. Mr T
    Replies:
    0
    Views:
    594
  2. Eitan Michaelson

    Impossible Leak realloc

    Eitan Michaelson, Jun 27, 2003, in forum: C++
    Replies:
    11
    Views:
    1,210
  3. Jonathan.Bailleul

    g++ 3.2.2 realloc bug?

    Jonathan.Bailleul, Aug 8, 2003, in forum: C++
    Replies:
    3
    Views:
    630
    John Harrison
    Aug 8, 2003
  4. Bren
    Replies:
    8
    Views:
    2,025
    Stephen Howe
    Sep 4, 2003
  5. Henrik J

    Realloc a struct **

    Henrik J, Nov 10, 2003, in forum: C++
    Replies:
    1
    Views:
    413
    David White
    Nov 10, 2003
Loading...

Share This Page