Jump to content
  • Advertisement
Sign in to follow this  
nullsquared

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

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by jyk
What'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 this post


Link to post
Share on other sites
Quote:
Original post by agi_shi
Quote:
Original post by jyk
What'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 this post


Link to post
Share on other sites
Quote:
Original post by agi_shi
If 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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by nomichi
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.


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 this post


Link to post
Share on other sites
Quote:
Original post by jyk
Quote:
Original post by agi_shi
If 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 this post


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


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!