Dynamic Memory and Arrays

This is a discussion on Dynamic Memory and Arrays within the C++ Programming forums, part of the General Programming Boards category; I am attempting to work on dynamic memory allocation and I thought trying to make a simplified vector class would ...

  1. #1
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84

    Dynamic Memory and Arrays

    I am attempting to work on dynamic memory allocation and I thought trying to make a simplified vector class would be good practice. The only functions I am going to implement will be push, pop, isEmpty, clear, and size. It only works with string objects right now because I have never done templated classes. That's what I would need to allow a class to work on different objects right? I know function templates so I figured templated classes would be only a slight extension.

    Anyway, now that I have gone off-topic in my OP to get back, I have been having trouble with my push method. Here is the pertinent code, but I will attach everything in case you want to see.

    Code:
    void myVector::push_back(std::string temp)
    {
        //temporary array one bigger than the old array
        std::string *tempArr = new std::string[count + 1];
        //loop over every element and copy all of the original array to the bigger 
        //array. count is number of items currently in vector
        for(int i = 0; i < count; i++)
        {
            tempArr[i] = arr[i];   
        }   
        //add the new element to the back of the new array
        tempArr[count] = temp;
        
        //set the new array to be this vectors container
        arr = tempArr;
        
        
        //loop over every element and print everything out for debugging
        for(int i = 0; i <= count; i++)
        {
            std::cout<<"value at: arr["<<i<<"] is: "<<arr[i]<<std::endl;   
        }
        //increase the number of elements
        count++;
        
        //delete[] tempArr;
        return;
    }

    If I leave the comment on the delete[] tempArr it works, but with it in there it doesn't. To me, it makes sense that you should have to have it there otherwise won't you get memory leaks as every time you are adding elements you are never freeing up the memory you just allocated. It seems to work though albeit not as efficient as it could be without the memory leak. I think the problem is the line where I set arr= tempArr because now they are pointing to each other so deleting tempArr also deletes the other.

    What say you?
    Attached Files Attached Files

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    True you have to delete something. But you want to delete the original data not the the tempArr that still points to the new data.
    Kurt

    EDIT: should work this way
    Code:
    void myVector::push_back(std::string temp)
    {
        //temporary array one bigger than the old array
        std::string *tempArr = new std::string[count + 1];
        //loop over every element and copy all of the original array to the bigger 
        //array. count is number of items currently in vector
        for(int i = 0; i < count; i++)
        {
            tempArr[i] = arr[i];   
        }   
        //add the new element to the back of the new array
        tempArr[count] = temp;
       
        delete [] arr;  // delete the old data    
    
        //set the new array to be this vectors container
        arr = tempArr;
        
        
        //loop over every element and print everything out for debugging
        for(int i = 0; i <= count; i++)
        {
            std::cout<<"value at: arr["<<i<<"] is: "<<arr[i]<<std::endl;   
        }
        //increase the number of elements
        count++;
        
        return;
    }
    Last edited by ZuK; 08-05-2006 at 03:38 AM.

  3. #3
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84
    Ok thanks. So calling delete just frees the memory the pointer referred to and doesn't completely get rid of the pointer? That's why once we called delete[] arr we just freed all of the memory it previously pointed to so we can now have it point to the newer, bigger array.

    What I was trying to do earlier was create a new, bigger array (tempArr). Then I would say arr point to tempArr and then when I called delete [] tempArr the memory that arr pointed to was also freed and no longer available that's why my data was gone. I think I understand now. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  2. Help with a problem regarding dynamic memory and arrays
    By Michty in forum C++ Programming
    Replies: 5
    Last Post: 07-26-2006, 01:26 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. dynamic memory and arrays
    By jomns in forum C++ Programming
    Replies: 4
    Last Post: 01-04-2004, 01:18 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21