Ch1 : 4- C/C++

Objectives : Walking along with C and C++

  • Looking C and C++ side by side
    • allocating / freeing memories: The input are saved and managed distinctively.
      • with C : malloc(10*sizeof(data)) and free(data)
      • with C++ :
        • non-array type 
          • int *n1 = new int; // dynamically allocate an integer
            *n1 = 5; // assign 5 to this integer
            delete n1; // freeing memory
        • arrays
          int x = 10;
          int * n2 = new int[x];// allocate an array of x number
          n2[5] = 5;// assign 5 to n[5] element
          delete[]n2;// freeing memory
    • Comparing "printf" of C and "cout" of C++ : the output can be similar but these two functions are not similar
      • C : prints only with "printf";
      • C++: can print with "cout and "printf"; printf is a function and prints faster, but not type safe. The object "cout" belongs to "iostream".
  • Files Used :
    • compare_c_cplus1.cpp -- did not release memory, may cause memory leak
    • compare_c_cplus2.cpp --release memory with free(n1) and delete[ ] n2;
  • Code : compare_c_cplus1.txt
 

Step: 1 Create two source files.
 

Step: 2 Edit and save Source file

Note that in the example shown below, the function," free (pointer-variable)", of C language, and the object "delete[]" of C++ language were used to free up the memories.

Step: 3 Runtime Views:

a) Example of not releasing memories:  Allocating memories with "malloc(10*sizeof(int))" function of C language , and "new[]" object of C++ language.

b) Free and Reuse Memory Location:  Releasing memories with "free()" function and "delete[]" object, with C and C++ ways respectively.

Step: 4 Brief Discussion: The pair of functions, "malloc()" and "free()"  of C, and the pair objects "new[]" and delete[]" of C++ , are used to allocate and free memories in the above examples; but though a different mechanisms.

Special Notes To: Allocating Memory Storage

Both C and C++, allow dynamically allocate and free-up memories   from a region known as "Free-Store" or "HEAP".   The pointer  ( * ) of C/C++ language, is deeply involved in allocating memories from the heap. It introduces a  variable,  pointee, to the compiler. The complier then engages the functions, (like malloc() or calloc() ) of C language or the object, "new" of C++ language, to carry out the job.

	int x;                  /* global stack storage */
        int  main(void) {
        int y;                 /*  local stack storage */
        char ch1;              /*  local stack storage */
        ch1 = malloc(50);      /*  allocates 50 bytes of dynamic heap storage */
        size = calcSize(10);   /*  dynamic heap storage */
        }

A pointer is used access the location (address) where data is stored during the life cycle of the current application.

As a rule of thumb, you need to free the space allocated with malloc() or new. Failing to free-up the allocated memories can cause a slow down or crash your system, this cause is coined as "memory leak".

 The purpose of  malloc()  function, is to find a free space of memory on the heap, return an address so that you allocate the block of data, and free up the allocated memory with another function, "free()", as soon as the task is completed.

Collected information

 
*~Copy of compare_c_cplus1.htm