Monday, November 17, 2003

Correction Re: Allocation vs Initialization in Java/.NET

Actually I think I made a mistake regarding allocation in Java and reference types in .NET.

Our syntax in Java/.NET is wrong. You can't just say Foo f[ 10 ]. This is only legal in C++. This is how you do it in Java/.NET...

Foo[] f; // allocate on stack; f is a reference on the stack and is null (Java/.NET implicitly initialize to null)

f = new Foo[ 10 ]; // allocate on heap; f now points to an array on to heap, but all indices are null

for ( int i = 0; i < 10; ++i )
{
f[ i ] = new Foo( 1, 2 ); // initialize indices
}

Sorry about that. I'm in the c++ mindset.

No comments: