class List {
int value;
List next;
}
public class demo {
public static void main( String[] args )
{
List p; // p is a reference variable
p = new List( ); // The new operator allocates
// memory for a list object
// and returns its base address
// #bytes of memory needed depends
// of fields in List class
}
}
|
I will illustrate what happens inside the computer system in this program:
public static void main( String[] args )
{
List p; // p is a reference variable
p = new List( );
}
|
"List p;" will allocate (reserve memory) a reference variable p:
I will illustrate what happens inside the computer system in this program:
public static void main( String[] args )
{
List p; // p is a reference variable
p = new List( );
}
|
"new List( )" will dynamically allocate (reserve memory) for a List object and return its base address
I will illustrate what happens inside the computer system in this program:
public static void main( String[] args )
{
List p; // p is a reference variable
p = new List( );
}
|
"p = " will assign the return value to the variable p:
Summary of the effect of:
List p; // p is a reference variable
p = new List( );
|
The reference variable p will point to a newly created List object:
|
|
Example program to print the storage size of some variable types:
#include <stdio.h>
struct List {
int value;
struct List *next;
};
int main( int argc, char *argv[] )
{
struct List a, *p;
printf("sizeof(int) = %ld\n", sizeof(int));
printf("sizeof(double) = %ld\n", sizeof(double));
printf("sizeof(struct List) = %ld\n", sizeof(struct List));
printf("sizeof(struct List a) = %ld\n", sizeof(a));
printf("sizeof(struct List *p) = %ld\n", sizeof(p));
}
|
class List {
int value;
List next;
}
public class demo {
public static void main( String[] args )
{
List p; // p is a reference variable
p = new List( ); // The new operator allocates
// memory for a list object
// and returns its base address
// #bytes of memory needed depends
// of fields in List class
}
}
|
#include <stdlib.h> // Header file that contains
// declaration for malloc( )
struct List {
int value;
struct List *next;
} ;
int main( int argc, char *argv[] )
{
struct List *p; // p is a reference variable
p = malloc(sizeof(struct List));
// The malloc function allocates
// memory for a struct List variable
// and returns its base address
// #bytes of memory needed depends
// of fields in List class
}
|
I will illustrate what happens inside the computer system in this program:
int main( int argc, char *argv[] )
{
struct List *p; // p is a reference variable
p = malloc(sizeof(struct List));
}
|
"struct List *p;" will allocate (reserve memory) a reference variable p:
I will illustrate what happens inside the computer system in this program:
int main( int argc, char *argv[] )
{
struct List *p; // p is a reference variable
p = malloc(sizeof(struct List));
}
|
"malloc(sizeof(struct List))" will dynamically allocate (reserve memory) a struct List variable (= object) and return its base address
I will illustrate what happens inside the computer system in this program:
int main( int argc, char *argv[] )
{
struct List *p; // p is a reference variable
p = malloc(sizeof(struct List));
}
|
"p = " will assign the return value to the variable p:
Summary of the effect of:
struct List *p; // p is a reference variable
p = malloc(sizeof(struct List));
|
The reference variable p will point to a newly created struct List var (= "object"):