Best variable array class?

David Webber 136 Reputation points
2024-03-01T15:26:02.6566667+00:00

I have been using MFC's CArray class as a base class for one of my own, in code written back in the 1980s. It hasn't been broke, so I haven't had to fix it.
Then recently I made some changes to my derived class, causing a bug, which resulted in a crash when I closed the view of a CDocument object. Something was accessing memory beyond the end of the heap. The crash was in an innocent destructor. But I couldn't for the life of me find where the offending code was.

So I ripped out the 35-year old foundations, and replaced the CArray with a std::vector base class to manage the variable array. I got an error message showing me exactly where I had done something silly (editing the n'th element of an array of size n), and was able to fix it immediately, both in the original version of the code and in the revised version.

So a question: should CArray be consigned to history, and std::vector always be used now for variable arrays? (The debugger certainly seems to handle it better!)

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,031 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,683 questions
{count} votes

Accepted answer
  1. Michael Taylor 53,496 Reputation points
    2024-03-01T16:12:11.5233333+00:00

    MFC is a "legacy" framework and was created long before modern C++ features were available. So asking if CArray is deprecated, to me, is sort of like asking if C's malloc should be used in code anymore. In general I'd say don't use it for new development but if it is working in existing code then why bother changing it.

    Do be aware that it doesn't work properly with some types. If you are either in this case or you don't have control over what is getting inserted into the array then it might make sense to switch to a STL vector instead. If nothing else STL's vector is better performant, more secure and more modern so there are gains here.

    At the same time, you're dealing with established code and there may be places in your code, or MFC, where CArray is expected. In those cases sticking with CArray would make more sense. Of course if it is your code then you might be able to just swap out the types using grep. Not sure what members you're using that may not be on the STL vector though. Furthermore the behavior may be different at runtime so you'd need to fully test your changes to ensure everything is behaving the same. But calls into MFC that require a CArray are going to require a conversion so if you're working with very large arrays then the conversion cost may be too high and sticking with CArray may be better.

    If you're dealing with a very large codebase and you are relying on functionality not in STL vector then you could create your own wrapper class that uses CArray under the hood and exposes the same functionality. Then globally replace the usage of CArray. Finally you could migrate your wrapper to STL vector and implement the underlying CArray-only logic using the vector. This is a lot more work to me but it is a middle ground when dealing with large codebases. Once you have things working with the new implementation then you can begin swapping out uses of your wrapper class with a regular STL vector and either create helper functions to handle the CArray-only functionality or leave them with your wrapper class.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.