How to delete an array?

Discussion in 'C++' started by felixnielsen@hotmail.com, Jan 22, 2006.

  1. Guest

    Hi, im pretty new to this so plz cut me some slag ;-)

    anyhow, i need to delete an array, or what i really need is to increase
    the array size, i was think i could be done by copying the array into a
    temorary array of the same size, then delete the original, initiate the
    old array again, but now with more spaces/cell or what u call it, then
    copy the teporary array into the on just created and last delete the
    temp array.
    a bit complicated, but im told its the only way to do it...
     
    , Jan 22, 2006
    #1
    1. Advertising

  2. TB Guest

    sade:
    > Hi, im pretty new to this so plz cut me some slag ;-)
    >
    > anyhow, i need to delete an array, or what i really need is to increase
    > the array size, i was think i could be done by copying the array into a
    > temorary array of the same size, then delete the original, initiate the
    > old array again, but now with more spaces/cell or what u call it, then
    > copy the teporary array into the on just created and last delete the
    > temp array.
    > a bit complicated, but im told its the only way to do it...
    >


    Here you go:

    int main(int argc, char* argv[])
    {
    // The array which to extend
    int * iptr = new int[5];

    // Allocate new array
    int * temp = new int[8];
    // Copy iptr to nptr
    for(unsigned i = 0; i < 5; ++i) {
    temp = iptr;
    }
    // Delete old array
    delete[] iptr;
    // Repoint iptr to new array
    iptr = temp;
    temp = 0;

    // Delete array
    delete[] iptr;
    return 0;
    }

    Or just use std::vector instead and you won't have to deal with the above.

    --
    TB @ SWEDEN
     
    TB, Jan 22, 2006
    #2
    1. Advertising

  3. Ralph Guest

    <> wrote in message
    news:...
    > Hi, im pretty new to this so plz cut me some slag ;-)
    >
    > anyhow, i need to delete an array, or what i really need is to increase
    > the array size, i was think i could be done by copying the array into a
    > temorary array of the same size, then delete the original, initiate the
    > old array again, but now with more spaces/cell or what u call it, then
    > copy the teporary array into the on just created and last delete the
    > temp array.
    > a bit complicated, but im told its the only way to do it...
    >


    Take a look at the Standard C++ library -
    vector::resize()

    There are reasons C++ provides sequence containers.

    -ralph
     
    Ralph, Jan 22, 2006
    #3
  4. Guest

    is i wrote, im pwer new to this and i must admit that i dont have a
    complete understanding of all the above exprissions, howefer thanks for
    the answer ill take a look at it and return in case i cant get it to
    work.
     
    , Jan 22, 2006
    #4
  5. Mike Wahler Guest

    <> wrote in message
    news:...
    > Hi, im pretty new to this so plz cut me some slag ;-)
    >
    > anyhow, i need to delete an array, or what i really need is to increase
    > the array size, i was think i could be done by copying the array into a
    > temorary array of the same size, then delete the original, initiate the
    > old array again, but now with more spaces/cell or what u call it, then
    > copy the teporary array into the on just created and last delete the
    > temp array.
    > a bit complicated, but im told its the only way to do it...


    Eliminate complications by not using an array, use
    a std::vector instead. It can grow and shrink in
    size automatically, relieving you of all memory
    management issues.

    -Mike
     
    Mike Wahler, Jan 22, 2006
    #5
  6. Guest

    yes, very neat, i just read a little guide about it, really very simple
    endless theres something i have missunderstod.

    there is one thing though.
    With arrays you can assign a value to af "cell" that doesnt excist, not
    very smart i know, however you can do the same with vector i found out,
    is there any complications i should know about?
     
    , Jan 22, 2006
    #6
  7. Mike Wahler Guest

    <> wrote in message
    news:...
    > yes, very neat, i just read a little guide about it, really very simple
    > endless theres something i have missunderstod.
    >
    > there is one thing though.
    > With arrays you can assign a value to af "cell" that doesnt excist,


    You can indeed write syntactically correct code to do this,
    but its behavior is undefined (i.e. anything might happen,
    from 'seems to work', to a crash, or anything else.)

    > not
    > very smart i know,


    Nope.

    > however you can do the same with vector i found out,


    No you cannot. Undefined behavior will also be the result.
    If you want to put additional elements in a vector,
    use its 'insert' or 'push_back' member functions (or
    'resize()', which (if the specified size is greater than
    existing size), will append the necessary number of default-
    initialized elements to bring the size to that specified..

    > is there any complications i should know about?


    C++ is rather complicated. I suggest the best way
    to deal with that is to get some good books. See
    www.accu.org for suggestions.

    -Mike
     
    Mike Wahler, Jan 22, 2006
    #7
  8. Guest

    well, i got it working, however i am having trouble using vectors in
    functions, probably because the vector is declaret the wrong place or
    something like that, but is it at all posible to declare global vectors?
     
    , Jan 22, 2006
    #8
  9. Gavin Deane Guest

    wrote:
    > well, i got it working, however i am having trouble using vectors in
    > functions, probably because the vector is declaret the wrong place or
    > something like that, but is it at all posible to declare global vectors?


    It is possible, but it's not good design to use global variables.
    Passing vectors to functions is not difficult and is a much better
    idea. You've obviously had a go at writing the code but you're having
    problems. Post the code here and people will be able to show you what's
    wrong. Note these guidelines for posting code:

    http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.8

    If you follow the advice there, so that people can copy and paste
    directly from your message into their compiler, you should get the help
    you need.

    Gavin Deane
     
    Gavin Deane, Jan 22, 2006
    #9
  10. Luke Meyers Guest

    wrote:
    > well, i got it working, however i am having trouble using vectors in
    > functions, probably because the vector is declaret the wrong place or
    > something like that, but is it at all posible to declare global vectors?


    Yes, but don't do that. The Right Way to make a variable (such as a
    vector) visible to a function is to pass it in (by value, or by
    reference -- learn the difference, it's important!) as an argument.
    Or, where appropriate to the design, the vector could be member data,
    and the function a member function, of some class which encapsulates
    the logical entity whose behavior and state are being represented.
    Member functions automatically have access to member data. It's nifty
    like that.

    Don't be afraid to post code if you get confused. The STL is freakin'
    rad and all, but as with any powerful tool (e.g. tools involving sharp
    metal moving at high speed), there is a learning curve, and some
    guidance is generally advisable.

    Luke
     
    Luke Meyers, Jan 22, 2006
    #10
  11. Karthi Guest

    Hi,
    you may use STL Arrays or if you are using MFC, then use CArray sort of
    things. Don try to reinvent a wheel.
    Karthikeyan S
    <> wrote in message
    news:...
    > Hi, im pretty new to this so plz cut me some slag ;-)
    >
    > anyhow, i need to delete an array, or what i really need is to increase
    > the array size, i was think i could be done by copying the array into a
    > temorary array of the same size, then delete the original, initiate the
    > old array again, but now with more spaces/cell or what u call it, then
    > copy the teporary array into the on just created and last delete the
    > temp array.
    > a bit complicated, but im told its the only way to do it...
    >
     
    Karthi, Feb 19, 2006
    #11
    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. Sandeep Grover

    delete on delete !

    Sandeep Grover, Jul 12, 2003, in forum: C++
    Replies:
    19
    Views:
    658
    Chris \( Val \)
    Jul 22, 2003
  2. HeroOfSpielburg
    Replies:
    1
    Views:
    415
    Alf P. Steinbach
    Aug 6, 2003
  3. 0to60
    Replies:
    4
    Views:
    325
    Jerry Coffin
    Dec 19, 2003
  4. Mathieu Malaterre

    delete NULL, what about delete[] NULL

    Mathieu Malaterre, Aug 17, 2004, in forum: C++
    Replies:
    2
    Views:
    3,878
    Mathieu Malaterre
    Aug 17, 2004
  5. Replies:
    12
    Views:
    217
    Robert Dober
    Mar 16, 2007
Loading...

Share This Page