Public Group

# Array algorithm for pushing back items (home-made).

This topic is 4672 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

In C++, I am trying to do the best I can to replicate the std::vector object (and others). Currenty I am making a function to push back/front objects, but it fails once I call it. The application terminates and the usual "This application has terminated in an unusual way..." appears. This is the function:
template<class _type, class _allocater>
array<_type, _allocater> &array<_type, _allocater>::push_back(const _type &object) {
if (++m_size > m_capacity) {
m_capacity *= 2;

_type *old_array = m_array;

m_array = m_allocater.allocate(m_capacity);
for (unsigned i = 0; i < m_size; i++)
m_array = old_array;

m_allocater.deallocate_array(old_array);
}

m_array[m_size - 1] = object;

return *this;
}


m_allocater is a default allocater for (in it's default form) uses new, new[] and delete, delete[] to manage the heap. Does anyone see what I may be doing wrong or incorrectly? My theory is to double the capacity every time the size is larger than it... Thanks!

##### Share on other sites
What's the initial value of m_capacity?

##### Share on other sites
Quote:
 Original post by jykWhat's the initial value of m_capacity?

m_capacity is either:
1) Set to 0 (default)
2) Set to a copy's capacity
3) Set to a user defined size (if it's array arr(1), etc.)
4) Umm... That's it I think

Any ideas? Thanks for the quick reply!

##### Share on other sites
Quote:
Original post by agi_shi
Quote:
 Original post by jykWhat's the initial value of m_capacity?

m_capacity is either:
1) Set to 0 (default)
2) Set to a copy's capacity
3) Set to a user defined size (if it's array arr(1), etc.)
4) Umm... That's it I think

Any ideas? Thanks for the quick reply!

I think I see what I did wrong. If it's zero and I *= by 2 then it would be 0 and I am allocating an array of size 0 :(. I'll see if that's it...

EDIT: Nope, not it...

##### Share on other sites
Quote:
 Original post by agi_shiIf it's zero and I *= by 2 then it would be 0 and I am allocating an array of size 0
That's what I was getting at. You said it still doesn't work though. Does it crash the very first time you call it? If so, you might post the new code (with the fix for m_capacity == 0). You could also cout << some debug info, such as what m_size and m_capacity are before and after you change their values.

##### Share on other sites
I may be wrong but should:

_type *old_array = m_array;

be:

_type old_array = m_array;

cause it looks like it's pointing to the same memory and then you deallocate old_array.

##### Share on other sites
Quote:
 Original post by nomichiI may be wrong but should:_type *old_array = m_array;be:_type old_array = m_array;cause it looks like it's pointing to the same memory and then you deallocate old_array.

That's exactly what I wanted to do. Use the memory from m_array to copy to a new m_array. So I assign a pointer to m_array's memory and allocate new memory for m_array without deleting it so I can copy it over. Then I have a pointer that I can use to delete the old memory.

Anyone have any ideas?

##### Share on other sites
Quote:
Original post by jyk
Quote:
 Original post by agi_shiIf it's zero and I *= by 2 then it would be 0 and I am allocating an array of size 0
That's what I was getting at. You said it still doesn't work though. Does it crash the very first time you call it? If so, you might post the new code (with the fix for m_capacity == 0). You could also cout << some debug info, such as what m_size and m_capacity are before and after you change their values.

I am thinking of putting this in VC2k5EE and debugging...

Thanks for giving a suggestion though :).

##### Share on other sites
Maybe you should just use namespace std and vectors? Wouldn't that be more efficent?

##### Share on other sites
Quote:
 Original post by PlasmaroboMaybe you should just use namespace std and vectors? Wouldn't that be more efficent?

I should have stated that this is to learn. [smile]

1. 1
2. 2
Rutin
19
3. 3
4. 4
khawk
15
5. 5
A4L
13

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633743
• Total Posts
3013644
×