
On Wed, 25 Apr 2007, Michael Marcin wrote: [...]
I originally wrote that using a scoped_array to document the lifetime, ownership, and type of data pointed to. And so I didn't have to worry about things like forgetting to call delete[] instead of delete. I figured that any compiler worth using should inline this out to the same code as the "optimized" version but I have no proof of that I was hoping someone would have done simple tests comparing generated code that I could reference when I discuss these "improvements" with the team later this week. [...]
I did a simple test (code below). By changing delete to delete[] and compiling as (using gcc-4.0.3) g++ -o test_n test.cpp -O3 and g++ -o test_b test.cpp -O3 and then disassembling the two binaries (objdump -d ...) and comparing the generated code, I could see that the generated code for the 'test' function was identical in both case. The code is of course a little bit different between delete and delete[]. #include <iostream> #ifdef USE_BOOST #include <boost/scoped_array.hpp> class Context { public: Context( unsigned int width, unsigned int height ) : m_buffer( new unsigned int[width*height] ) { } unsigned int* GetBuffer() { return m_buffer.get(); } private: boost::scoped_array<unsigned int> m_buffer; }; #else class Context { public: Context( unsigned int width, unsigned int height ) : m_buffer( new unsigned int[width*height] ) { } ~Context() { delete[] m_buffer; } inline unsigned int* GetBuffer() { return m_buffer; } private: unsigned int* m_buffer; }; #endif void test() { Context ctx( 20 , 20 ) ; ::std::cout << ctx.GetBuffer() ; } int main() { test() ; return 0 ; } -- François Duranleau LIGUM, Université de Montréal