|
-
May 30th, 2013, 07:49 AM
#15
Re: Heap Corruption during run time.
 Originally Posted by EdwardKes
Code:
bool IntArrayList::add(Number& num)
{
if(m_Arr[0] == NULL)///If array is empty put the first element at 0 index
{
m_Arr[0] = #
m_Size += 1;
return true;
}
else
{
int in_place = SearchInsertPlace();///find the place where you store another element
if(in_place != -1)///there is still place available
{
m_Arr[in_place] = #
m_Size += 1;
return true;
}
else///if no free places, create new array
{
m_Arr = CreateNewArray(m_Arr);
in_place = SearchInsertPlace();
m_Arr[in_place] = #
return true;
}
}
}
There are so many things wrong with this both from a technical POV as a design POV that I'm not even sure I know where to start.
lets have a start
technical: why are you testing for m_arr[0] being NULL when the obvious check is if size == 0
design: why does your class even assume there will always be an allocated array, it should handle "empty" better by not requiring an allocated array when no such array has been needed yet.
technical: m_Arr[0] = #
why are you storing a pointer to a local stackbased variable ?
design: why does an add function return true, when it makes more sense to return an index to the added array element or a iterator to the added element.
technical SearchInsertPlace();
it's unclear if this function only does a search or also makes room for the add. The name seems to indicate it doesnt. Byt in the "there is still place" branch there doesn't seem to be any code that takes care of moving all the elements up by one.
design: this also assumes that the search functions knows that there isn't any more room, so either it's name is in approproate, or you have gaping functionality holes here.
design: every possible branch in your code seems to return true, so what's the point of always returning the same value ?
I can't even tell if all of this is a matter of incomplete/confusing assignment given by the teacher, or you not properly following said assignment.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|