Computer Science concepts and their implementation in some programming language

  • Computer concepts are independent from programming languages

      • The way a concept is realised/implemented in a programming language differs only in the syntax

  • In CS171, you have learned about the (linked) list manipulations

      • Therefore, I will not re-teach the manipulations.

      • I.e.: I assume that you know how to search/insert/delete with linked lists

  • I will show you the manipulations of a list in C !

      • In the process, you will learn to how to express the same concept using the syntax of a different programming language

Defining a Node object in Java
 

In Java, Node objects (= nodes) are defined using a class:

 public class Node
 {
    int          value; // Data stored in a List element (= a Node)
    Node         next;  // A reference variable ! 
                        //  points to the next List elem (= Node)     
                        //  in the linked list


   // Other non-relevant stuff omitted
 } 

We now define a struct Node in C to store list "objects" (= nodes)

Defining a list object in C
 

In C, Node "objects" (= nodes) are defined using a struct:

 struct Node
 {
    int          value; // Data stored in a List element (= a Node) 
    struct Node *next;  // A reference variable !!!!!!! 
                        //  points to the next List elem (= Node)    
                        //  in the linked list


   // There are no functions defined inside a struct !!!
 } ; 

Notice we define a reference variable in both cases !

The only difference is the way to express it !

Defining an empty list in Java
 

An empty list is defined as follows in Java:

 public class Node
 {
    int          value; // Data stored in a List element (= a Node)
    Node         next;  // A reference variable ! 
 }



 public class List 
 {
     private Node  head = null; // empty list
                                // head is a reference variable


     public void insert( .... ) { .... } // Only shown for cintext
 } 

We must define a reference variable in C to represent head !

Defining an empty list in C
 

An empty list is defined as follows in C:

 struct Node 
 {
   int          value;
   struct Node *next;  
 };

 #include <stdio.h> // Contains the definition for NULL

 int main( int argc, char *argv[] )
 { 
     struct Node *head = NULL;  // C uses NULL for null 
                                // head is a reference variable
 }

 struct Node *insert( .... ) { ... }  // Shown for context
  

In C, a reference variable head is defined as   struct Node *

Furthermore, it's defined inside the function that manipulates the linked list

Allocating (= creating) objects in Java

An object is allocated using the new operator in Java:

 class Node 
 {
   int          value;
   Node         next;  
 }

 public class demo {
   public static void main( String[] args )
   { 
     Node         p; // p is a reference variable
                  
     p = new Node( ); // The new operator allocates
                      // memory for a list object
		      // and returns its base address
		      // #bytes of memory needed depends
		      // of fields in Node class
   }
 }
   

Allocating (= creating) objects in Java - illustrated

You need to understand what happens inside the computer system when it executes the following:

     Node         p; // p is a reference variable     
     p = new Node( );  

"Node p;" will allocate (= reserve) memory for the reference variable p:

Allocating (= creating) objects in Java - illustrated

You need to understand what happens inside the computer system when it executes the following:

     Node         p; // p is a reference variable     
     p = new Node( ); 

"new Node( )" will dynamically allocate (=reserve) memory for a Node object and return its base address:

Allocating (= creating) objects in Java - illustrated

You need to understand what happens inside the computer system when it executes the following:

     Node         p; // p is a reference variable     
     p = new Node( );  

"p = " will assign the return value to the variable p:

Allocating (= creating) objects in Java - illustrated

Summary of the effect of:

     Node         p; // p is a reference variable     
     p = new Node( );  

The reference variable p will point to a newly created Node object:

Note:   we will be repeating these same steps using the C programming language !!

Allocating (creating) struct variables in C

Allocating memory in the C programming lanugae:

  • C do not have a new operator !

  • 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 )
      

  • The allocate memory storage for a struct Node variable (= object):

        malloc( sizeof(struct Node) )
    

    The malloc( ) function will return the address of the struct Node "object" --- i.e.: exactly the same as the new operator in Java

Example of usage of the sizeof operator

Example program that prints the storage size of some variable types (including a struct Node):

 #include <stdio.h>

 struct Node 
 {
   int          value;
   struct Node *next;  
 };

 int main( int argc, char *argv[] )
 { 
    struct Node a, *p;

    printf("sizeof(int)            = %ld\n", sizeof(int));
    printf("sizeof(double)         = %ld\n", sizeof(double));

    printf("sizeof(struct Node)    = %ld\n", sizeof(struct Node));
    printf("sizeof(struct Node a)  = %ld\n", sizeof(a));
    printf("sizeof(struct Node *p) = %ld\n", sizeof(p));
 } 
   

DEMO: demo/C/set2/sizeof3.c

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

These were the statements in Java to allocate a Node object:



 class  Node 
 {
   int          value;
   Node         next;  
 }

 public class demo {
  public static void main( String[] args )
  { 
    Node         p; // p is a reference variable
                  
    p = new Node( ); 
                     // The new operator allocates
                     // memory for a list object
		     // and returns its base address
		     // #bytes of memory needed depends
		     // of fields in Node class
  }
 }   

We will now express the same actions using C statements !

Allocating (= creating) objects in Java

The statements in C to allocate a Node object are as follows:

 #include <stdlib.h> // Header file containing the declaration for malloc( )
                     
 struct Node 
 {
   int          value;
   struct Node *next;  
 } ;


  int main( int argc, char *argv[] )
  { 
    struct Node *p; // p is a reference variable
                  
    p = malloc( sizeof(struct Node) );
                     // The malloc function allocates
                     // memory for a struct Node variable
		     // and returns its base address
		     // #bytes of memory needed depends
		     // of fields in Node structure
  }
   

I will illustrate what happens inside the computer when these statements are executed.

Allocating (= creating) objects in C - illustrated

I will illustrate what happens inside the computer system in this program:

    struct Node *p; // p is a reference variable             
    p = malloc(sizeof(struct Node));  

"struct Node *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:

    struct Node *p; // p is a reference variable             
    p = malloc(sizeof(struct Node)); 

malloc( ) allocates (=reserve) memory for a struct Node 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:

    struct Node *p; // p is a reference variable             
    p = malloc(sizeof(struct Node)); 

"p = " will assign the return value to the variable p:

Allocating (= creating) objects in C - illustrated

Summary of the effect of:

    struct Node *p; // p is a reference variable             
    p = malloc(sizeof(struct Node)); 

The reference variable p will point to a newly created struct Node var (= "object"):

Note:   we have repeated the same steps used by Java !!    (I.e.: it's the same concept (allocate memory) !)