Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with shrinking array

Status
Not open for further replies.

p1kn1c

Programmer
Sep 29, 2006
4
CA
So i have an array elements and it is declared like this:

Loans ** elements;
elements = new Loans*[initial_size];

where initial_size = 4

I have a remove some element method that checks if it is a good time to shrink the array (ie there is more then twice the amount of space for the amount of elements)

Code:
index--;

if(index < capacity/2) { //shrink the container
cout << "shrinking\n";
Loans ** temp = elements;
elements = new Loans*[capacity/2];
for(int i=0; i<index; i++) elements = temp;
capacity = capacity/2;
delete [] temp;
}


It always crashed on this line:

elements = new Loans*[capacity/2];

Whats the problem??

Thanks for any help.
 
What kind of crash is it? Does it throw an exception that you're not catching?

Does it crash the first time that line is run, or does it crash after a while?

Try this and let me know what capacity is set to when it crashes:
Code:
cout << "shrinking\ncapacity = " << capacity << endl;

You might want to consider switching to a vector instead of an array:
Code:
std::vector<Loans> elements;
...
elements.push_back( element ); // Add an element to the end of the vector.
...
std::vector<Loans>( elements ).swap( elements );  // Shrink the vector.
 
It crashes right away and it seems to be some sort of memory crash:

It pops up with some Just in Time Debugger crap, that doesnt say aytnhing other then im not using or w/e.

its an unhandled win32 error or w/e.

capicty is equal to 2 when it crashes.

It surely has something to do with how im resetting the array..but it doesnt make sense as it is identical to how i first created it.

: Where i initially created the array:
: In SetOfLoans.h file :

SetOfLoans(int initial_size = 4) {
elements = new Loans*[initial_size];
capacity = initial_size;
numberOfElements = 0;
index = 0;
}

: and then the method it is crashing in :

Loans & removeSomeElement() {
// answer some (any) element from the set and remove it from the set
// if the set is more than half empty release some memory
index--;
elements = new Loans*[4];
if(index < capacity/2) { //shrink the container
cout << "shrinking\n";
Loans ** temp = elements;
cout << capacity/2;
elements = new Loans*[capacity/2];
for(int i=0; i<index; i++) elements = temp;
capacity = capacity/2;
delete [] temp;
}

return *elements[index];
}


: I have tried to even make a totaly new array in that method but it crashes there aswell.. for example

Loans ** temp = new Loans*[4]; crashes in the method but not in the constuctor for the class.

Doesnt make sense =/. Its for an assignment so i need to use arrays.
 
I think you might have some memory corruption happening elsewhere in your program, and this is just uncovering the problem.

Try adding a breakpoint just before the line that crashes and run in the debugger. Look at the memory locations of all your pointers and make sure that they all have 'FD FD FD FD' before and after the block of memory allocated to the pointer. If those 4 FD's aren't there, you must have overflowed your array somewhere.
 
Yeah im pretty sure your right.

Ive commented alot of stuff out and just done some basic testing with debugger.

RIght now all my main does it create one Loan and adds it to the array.

Code:
Loan *p1 = new Loan("Lou", "11 Elgin St. Ottawa", Date(14,9,2006), 1000, 6.0, 12); 

SetOfLoans carLoans;

carLoans.add(*p1);

in the main function and my add function is as follows:

Code:
 void add(Loans & element){ 
	*elements[index] = element;
}

when i run this with debugger it crashes on that line and the disasebly is:

00404663 rep movs dword ptr es:[edi],dword ptr [esi]

...i have no idea what that means or if that problem is assosiated to the line where i add the element to the array..but without that line the debugger runs fine.

So whats wrong with how im adding this element to the array?

I init the array like this

elements = new Loans*[initial_size];

and elements is declared as:

Loans ** elements;.

The init size is set to 6 and index is set to 0 at the start.

I aprreciate any help ;p its somewhat urgent as assignment due on sunday ;/. Thx
 
OK, I think the problem is that you aren't newing enough stuff.

elements is a pointer to a Loans pointer (i.e. Loans**)

You are allocating an array of Loans* pointers to the elements variable, but you aren't allocating any Loans objects.

For example:
Code:
Loans** elements = new Loans*[initial_size];

for ( int i = 0; i < initial_size; ++i )
{
   elements[i] = new Loans( "Lou", "11 Elgin St. Ottawa", Date(14,9,2006), 1000, 6.0, 12 );
}

...

for ( int i = 0; i < initial_size; ++i )
{
   delete elements[i];
}

delete [] elements;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top