Allocating (= creating) objects in Java
 
 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
   }
 }
   

Allocating (= creating) objects in Java - illustrated

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:

Allocating (= creating) objects in Java - illustrated

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

Allocating (= creating) objects in Java - illustrated

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:

Allocating (= creating) objects in Java - illustrated

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:

Allocating (creating) struct variables in C
 

  • The standard C library provides the function malloc( ) (memory allocate) that allocates (= reserve) memory for dynamically created struct variables (= "objects")

  • Syntax of the malloc( ) library function:

       malloc( n )  // Allocate n bytes of memory and  
                    // return the base address
      

  • Problem:

      • What value for n do we need to use ???

      • We need to know how many bytes of memory needed to store the fields in struct variables (= objects) !!

The sizeof operator in C

  • C provides the sizeof operator that returns the #bytes needed to store a variable of a data type

  • Syntax of the sizeof operator:

           sizeof( dataTypeName )      
       or
           sizeof( varName )
      

Example of usage of the sizeof operator
 

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));
 } 
   

Repeated for contrast: allocating (= creating) objects in Java
 


 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

  }
 }
   

Allocating (= creating) objects in Java
 
 #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
  }
 
   

Allocating (= creating) objects in C - illustrated

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:

Allocating (= creating) objects in C - illustrated

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

Allocating (= creating) objects in C - illustrated

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:

Allocating (= creating) objects in C - illustrated

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"):