PDA

View Full Version : instantiation


BioHazard90
01-16-2009, 03:16 AM
class MyProgram
{
static void Main()
{
int[] integers; // declare array
integers = new int[10]; // size array
}
}

In which line is the array actually instantiated?

Fou-Lu
01-16-2009, 03:34 AM
When you call the new on the int array.
At the point of integers its an int * reference to what will be a pointer value that should currently be null (or an old value if the internal mechanism uses malloc, but I'm betting on it being null). Since a size has not been defined, memory has not been allocated for use in you're integer array. Calling the new and providing a size will indicate to block x (10 in you're example) * sizeof(int) memory locations and the original int * is referenced to the first memory location provided.

int[] integers;
integers[0] = 1; // Should toss an exception at you.


Ralph/Oracleguy, did I get that about right (you guys are the C/C++ experts from the looks of it :P)?

oracleguy
01-16-2009, 05:09 AM
Ralph/Oracleguy, did I get that about right (you guys are the C/C++ experts from the looks of it :P)?

Correct. This looks like C# code more than C++ but the idea in this case is the same.

@BioHazard90: It is always good when posting code to specifically say which language you are using.