In CS170, you have learned how to create dynamic arrays in Java:
public class demo { public static void main( String[] args ) { int[] A; // A is a reference variable ! A = new int[10]; // The new operator allocates // memory for an array object // and returns its base address // The #bytes of memory needed // = array size * sizeof(int) } } |
I will illustrate what happens inside the computer system in this program:
public class demo {
public static void main( String[] args )
{
int[] A; // A is a reference variable !
A = new int[10];
}
}
|
"int[] A" will allocate (reserve memory) a reference variable A:
I will illustrate what happens inside the computer system in this program:
public class demo {
public static void main( String[] args )
{
int[] A; // A is a reference variable !
A = new int[10];
}
}
|
"new int[10]" will dynamically allocate (reserve memory) for an int[10] array (= 40 bytes) and return its base address:
I will illustrate what happens inside the computer system in this program:
public class demo {
public static void main( String[] args )
{
int[] A; // A is a reference variable !
A = new int[10];
}
}
|
"A = " will assign the return value to the variable A:
Summary of the effect of:
int[] A; // A is a reference variable ! A = new int[10]; |
The reference variable A will point to a newly created int[10] (array of 10 integers) object:
We can achieve the identical result in C with the following:
int main( int argc, char *argv[] ) { int *A; // A is a reference variable ! A = malloc( 10*sizeof(int) ); // The malloc(10*sizeof(int) call // allocates memory for an array // and returns its base address // The #bytes of memory needed // = array size * sizeof(int) } } |
I will illustrate what happens inside the computer system in this program:
int main( int argc, char *argv[] )
{
int *A; // A is a reference variable !
A = malloc( 10*sizeof(int) );
}
|
"int *A" will allocate (reserve memory) a reference variable A:
I will illustrate what happens inside the computer system in this program:
int main( int argc, char *argv[] )
{
int *A; // A is a reference variable !
A = malloc( 10*sizeof(int) );
}
|
"malloc(10*sizeof(int))" will dynamically allocate (reserve memory) for an int[10] array (= 40 bytes) and return its base address:
I will illustrate what happens inside the computer system in this program:
int main( int argc, char *argv[] )
{
int *A; // A is a reference variable !
A = malloc( 10*sizeof(int) );
}
|
"A = " will assign the return value to the variable A:
Summary of the effect of:
int *A; // A is a reference variable !
A = malloc( 10*sizeof(int) );
|
The reference variable A will point to a newly created int[10] (array of 10 integers) object:
We still need a language construct to access the array elements:
Right now, we can only access A[0] with what we learned (without using an offset):
*A ≡ A[0] |
New language construct of C:
C has a "pointer arithmetic" mechanism that adds an integer (=index) to an address:
A + i (A is an address value and i is an integer) |
We will study the pointer arithmetic concept next