Thread: trying to remove item from an array

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    9

    trying to remove item from an array

    this is a function that should remove an integer from an array but when i run it, it will remove the integer but the array will remain the same size. do i have an error im not seeing.

    Code:
        // Method that takes an int from the bag making it one size  smaller 
       // int refers to its location in memory using a zero index 
       void Bag::take(int location) 
       { 
          cout<<"TAKING" << endl; 
          int new_maxsize = maxsize - 1; 
          int *temp_array = new int[new_maxsize]; 
          int second_half = location + 1; 
          for (int index = 0; index < location; index++) 
          { 
             temp_array[index] = p[index]; 
          } 
          for (int index = location; index < new_maxsize; index++) 
          { 
             temp_array[index] = p[second_half]; 
             second_half++; 
          } 
          
          maxsize = new_maxsize; 
          p = temp_array; 
          currentloc = new_maxsize; 
          
       }

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Just don't change the maxsize variable if you want it to be the same size.

    vvvvv Get rid of this vvvv
    int new_maxsize = maxsize - 1;
    Last edited by jverkoey; 10-14-2004 at 09:29 PM.

  3. #3
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Code:
    p = temp_array;   // memory leak??
    If 'p' is a pointer to A = int[old_size], and now 'p' points to B = int[new_size], what happens with A?
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    9
    im sorry i mistyped i want the array to get resized but it does not

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    9
    i have a destructor that gets rid of 'A'

  6. #6
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    'maxsize' does change, does it not?
    Don't tell me you get the size of array by doing this.
    Code:
    // if p is defined as: int * p = new int[ maxsize ];
    // the following line will not display the size of array
    
    cout << sizeof(p);
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>i have a destructor that gets rid of 'A'
    That's irrelevant. The destructor will only free the last array that you created; all the others will be leaked.
    Code:
    delete[] p;   //Free the previously allocated array.
    p = temp_array;
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Resource Management question..
    By Raigne in forum C++ Programming
    Replies: 37
    Last Post: 03-08-2008, 09:36 AM
  2. Recursion Revisited again, and again!
    By clegs in forum C++ Programming
    Replies: 93
    Last Post: 12-08-2007, 08:02 PM
  3. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM