Memory Allocation In C And Cplus plus-Introduction To Programming-Lecture Notes, Study notes of Computer Programming

A program is a precise sequence of steps to solve a particular problem. This course includes basic programming structure like loops, operator, memory allocation, reference, pointers etc. It teaches how to be a good programmer. This lecture handout is about: Memory, Allocation, C, C plus plus, Operator, Classes, Structures, Abstraction, Messages, Methods, Extend, Constructors, Destructors, Function

Typology: Study notes

2011/2012

Uploaded on 08/06/2012

anchal
anchal 🇮🇳

4.6

(9)

95 documents

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
329
Lecture No. 28
Reading Material
Deitel & Deitel - C++ How to Program Chapter 7
7.6, 7.8
Summary
x Lecture Overview
x Memory Allocation in C
x Memory Allocation in C++
x new Operator and Classes
x Example Program 1
x Classes and Structures in C++
x new Operator and Constructors
x delete Operator and Classes
x Example Program 2
x new, delete outside Constructors and Destructors
x main() Function and Classes
x Class Abstraction
x Messages and Methods
x Classes to Extend the Language
x Tips
Lecture Overview
In the previous lectures, we have been discussing about Classes, Objects, Constructors
and Destructors. In this lecture we will take them further while discussing Memory
Allocation.
-
We’ll see how the memory allocation is done in C++, while discussing
memory allocation in C?
-
How C++ style is different from the C-style of allocation discussed earlier?
-
What are the advantages of C++ approach as compared to that of C?
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Memory Allocation In C And Cplus plus-Introduction To Programming-Lecture Notes and more Study notes Computer Programming in PDF only on Docsity!

Lecture No. 28

Reading Material

Deitel & Deitel - C++ How to Program Chapter 7

Summary

 Lecture Overview  Memory Allocation in C  Memory Allocation in C++  new Operator and Classes  Example Program 1  Classes and Structures in C++  new Operator and Constructors  delete Operator and Classes  Example Program 2  new, delete outside Constructors and Destructors  main() Function and Classes  Class Abstraction  Messages and Methods  Classes to Extend the Language  Tips

Lecture Overview

In the previous lectures, we have been discussing about Classes, Objects, Constructors and Destructors. In this lecture we will take them further while discussing Memory Allocation.

  • We’ll see how the memory allocation is done in C++, while discussing memory allocation in C?
  • How C++ style is different from the C-style of allocation discussed earlier?

- What are the advantages of C++ approach as compared to that of C? docsity.com

Memory Allocation in C Before further proceeding with the concept of memory, it is better to know what else we can create with classes besides objects.

Recapturing of the concept of ‘structures’ can help us to move forward. Consider the following statement.

struct abc { int integer; float floatingpoint; };

We could have declared a structure object as: struct abc xyz; // Declared an object of structure type

and access data members inside structure by using dot operator (“.”) as: xyz.integer = 2134; xyz.floatingpoint = 234.34;

Similarly, we could have a pointer to a structure object as: struct abc* abcPtr; // Declared a pointer of a structure type abcPtr = xyz; // Pointer is pointing to xyz object now

We can access the individual data member as: abcPtr->integer = 2134; abcPtr->floatingpoint = 234.34;

We can have pointers to different data structures, similarly, pointer to a class object. Here we are going to discuss about Pointers, Classes and Objects.

Let’s start by talking about memory allocation. We introduced few functions of memory allocation in C: malloc() , calloc() and realloc(). Using these functions, memory is allocated while the program is running. This means while writing your program or at compile time, you don’t need to know the size of the memory required. You can allocate memory at runtime (dynamically) that has many benefits. The classic example will be of an array declared to store a string. If the length of the actual string is lesser than the size of the array, then the part that remains unoccupied will be wasted. Suppose we declare an array of length 6 to contain student name. It is alright if the student name is let’s say Jamil but what will happen for the student named Abdul Razzaq. This is a case where dynamic memory allocation is required. In C language, the region of memory allocated at runtime is called heap. However, in C++, the region of available memory is called free store. We have different functions to manipulate memory in both C and C++. You know that while using malloc() , we have to tell the number of bytes required from memory like: malloc(number of bytes required to be allocated);

docsity.com

Memory Allocation in C++ The memory allocation in C++ is carried out with the use of an operator called new. Notice that new is an operator while the malloc() was a function. Let’s see the syntax of new operator through the following example.

new int;

In the above statement, the new operator is allocating memory for an int and returns a pointer of int type pointing to this region of memory. So this operator not only allocated required memory but also spontaneously returned a pointer of required type without applying a cast.

In our program, we can write it as: int *iptr; iptr = new int;

So while using new operator, we don’t need to supply the number of bytes allocated. There is no need to use the sizeof operator and cast the pointer to the required type. Everything is done by the new operator for us. Similarly, new operator can be used for other data types like char , float and double etc.

The operator to free the allocated memory using new operator is delete. So whenever, we use new to allocate memory, it will be necessary to make use of ‘delete’ to de- allocate the allocated memory. delete iptr;

The delete operator frees the allocated memory that is returned back to free store for usage ahead.

What if we want to allocate space for any array? It is very simple. Following is the syntax: new data_type [number_of_locations];

For example, we want to allocate an array of 10 int s dynamically. Then the statement will be like this: int *iptr; iptr = new int[10];

What it does is, it tries to occupy memory space for 10 int s in memory. If the memory is occupied successfully, it returns **int *** that is assigned to iptr.

Whenever we allocate memory dynamically, it is allocated from free store. Now we will see what happens if the memory in the free store is not sufficient enough to fulfill the request. malloc() function returns NULL pointer if the memory is not enough. In C++, 0 is returned instead of NULL pointer. Therefore, whenever we use new to allocate memory, it is good to check the returned value against 0 for failure of the new operator.

docsity.com

Remember, new is an operator,it is not a function. Whenever we use new , we don’t use parenthesis with it, no number of bytes or sizeof operator is required and no cast is applied to convert the pointer to the required type.

delete operator is used to free the memory when the allocation is done by using new as shown below: int *iptr; iptr = new int [10]; // Memory for 10 int s is allocated dynamically. delete iptr; // Allocated is freed and returned to the free store.

Can we apply the concept of dynamic memory allocation/deallocation while using new/delete with classes and objects? The answer is obviously yes.

new Operator and Classes

As we declare a pointer to a primitive datatype, similarly, we can have a pointer to a class object. Date *dptr; // dptr is a pointer to an object of type Date. Now, we create the object using the new operator. Remember, the basic definition of a class i.e. it is a user-defined data type. In other words, the language has been extended to a programmer to have user defined data types. When we use them in our programs, these are used in the same manner as the primitive data types.

dptr = new Date;

Whatever amount of memory is required for a Date object, is allocated from the free store. A pointer to of Date type is returned back and assigned to the dptr pointer variable. Is this all what new is doing? If it is so, can we use malloc() function by

Process (Program in memory)

Date class

main ( ) { Date* = new Date();

... ... }

BasicData class

dptr

Free Store

0 0 0

docsity.com

int * iptr; iptr = new int [10];

This new statement allocates contiguous space for an array of 10 int s and returns back pointer to the first int. Can we do this operation for objects of a class? The answer to this question is yes. The syntax in this case will be identical. To create an array of 10 objects of Date type, following code is written:

Date * dptr; dptr = new Date [10]; int day = dptr->getDay(); Here the new operator allocates memory for 10 Date objects. It calls the default or parameter-less constructors of the Date class and returns the pointer to the first object, assigned to the dptr variable. Arrow operators (->) is used while accessing functions or data members from the pointer variable.

Example Program 1 /* Following program demonstrates the new operator. This program has the problem of memory leak because delete operator is not called for the allocated memory. */

#include <iostream.h>

class MyDate { public: // public members are below

/* Parameterless constructor of MyDate class */ MyDate( ) { cout << "\n Parameterless constructor called ..."; month = day = year = 0; // all data member initialized to 0 }

/* Parameterized constructor of MyDate class. It assigns the parameter values to the ……..data members of the class */ MyDate(int month, int day, int year) { cout << "\n Constructor with three int parameters called ..."; this->month = month; // Notice the use of arrow operator ( -> ) this->day = day; this->year = year; }

/* Destructor of the MyDate class */ ~MyDate ( ) { cout << "\n Destructor called ...";

docsity.com

/* Setter function for the month data member. It assigns the parameter value to the month data member */ void setMonth ( int month ) { this->month = month; }

/* Setter function for the day data member. It assigns the parameter value to the day data member */ void setDay ( int day ) { this->day = day; }

/* Setter function for the year data member. It assigns the parameter value to the year data member */ void setYear ( int year ) { this->year = year; }

/* Getter function for the day data member. It returns the value of the day data member */ int getDay ( ) { return this->day; }

/* Getter function for the month data member. It returns the value of the month data member */ int getMonth ( ) { return this->month; }

/* Getter function for the year data member. It returns the value of the year data member */ int getYear ( ) { return this->year; }

/* A function to set all the attributes (data members) of the Date object */ void setDate ( int day, int month, int year ) { this->day = day; this->month = month; this->year = year;

docsity.com

Another good practice is to write public or private keywords only once in the class or structure declaration, though there is no syntactical or logical problem in writing them multiple times. Also remember while writing a class or a structure that once a keyword is written, say public, the declarations falling below this keyword will be public until the private keyword is mentioned. There is another keyword protected. We are not using this keyword in this course because that deals with inheritance that is a part of Object Oriented Programming, a separate course.

new Operator and Constructors

It is clear that whenever new operator is called to create an object, the constructor is also called for that object. What will happen if we have to call new from inside a constructor function. Can we do that? The answer is definitely yes. There are times when we have to do dynamic memory allocation or create new objects from inside a constructor. For example, we have a Student class with attributes i.e. roll number , age , height and name. The attributes like roll number , age and height can be contained in int s or float s but the name attribute will require a string. Because of the nature of this attribute (as it can have different lengths for different students), it is better to use dynamic memory allocation for this. So we will use new operator from within the constructor of Student class to allocate memory for the name of the

student.

We know whenever we use new to allocate memory, it is our responsibility to de- allocate the memory using the delete operator. Failing which, a memory leak will happen. Remember, the memory allocated from free store or heap is a system resource and is not returned back to the system ( even if the allocating program terminates ) unless explicitly freed using delete or free operators.

Now, we will see how the delete works for objects and what is the syntax.

Student { publi Studen { srcp }

... }

class c: t(char* name) = new char (strlen(name)+1) ; y( this->name, name) ;

main ( ) {

... } BasicData class

Process (Program in memory)

Free Store

J

m

a

i l \

this->name

docsity.com

delete Operator and Classes

As in our Student class, as we will be allocating memory from within the constructor of it. Therefore, there is a need to call delete to de-allocate memory. What is the appropriate location inside the class Student to call delete operator to de-allocate memory? In normal circumstances, the location is the destructor of a class ( Student class’s destructor in this case). The destructor is used to de-allocate memory because it is called when the object is no more needed or going to be destroyed from the program’s memory. So this is the real usefulness of destructors that these are used to release the system resources including memory occupied by the objects. As a thumb rule , whenever there is a pointer data member inside our class and pointer is being used by allocating memory at runtime. It is required to provide a destructor for that class to release the allocated memory. A constructor can be overloaded but not a destructor. So there is only one destructor for a class. That one destructor of a class must do house keeping before the object is destroyed. Normal data members int , char , float and double , not allocated using malloc() or new operator, don’t need to be de-allocated using free() or delete. These are automatically destroyed.

Let’s be sure that free() is used with malloc() function while delete operator with new operator. Normally, new will be called in a constructor. However, delete will be called in the destructor.

Example Program 2

/* Following program demonstrates the new and delete operators. It deallocates the memory properly before terminating. */

#include <iostream.h>

class MyDate { public: //public members are below

/* Parameterless constructor of MyDate class */ MyDate( ) { cout << "\n Parameterless constructor called ..."; month = day = year = 0; // all data member initialized to 0 }

/* Parameterized constructor of MyDate class. It assigns the parameter values to the ……..data members of the class */ MyDate(int month, int day, int year) { cout << "\n Constructor with three int parameters called ..."; this->month = month; // Notice the use of arrow operator ( -> ) this->day = day;

docsity.com

/* A function to set all the attributes (data members) of the Date object */ void setDate ( int day, int month, int year ) { this->day = day; this->month = month; this->year = year; }

private: // private members are below int month, day, year; };

main(void) { MyDate *dptr; // Declared a pointer dptr to MyPointer class object dptr = new MyDate [10]; // Created 10 objects of MyDate and assigned the // pointer to the first object to dptr pointer variable.

delete [] dptr; // Deleted (freed) the assigned memory to the objects }

The output of this example program is as follows: Parameterless constructor called ... Parameterless constructor called ... Parameterless constructor called ... Parameterless constructor called ... Parameterless constructor called ... Parameterless constructor called ... Parameterless constructor called ... Parameterless constructor called ... Parameterless constructor called ... Parameterless constructor called ... Destructor called ... Destructor called ... Destructor called ... Destructor called ... Destructor called ... Destructor called ... Destructor called ... Destructor called ... Destructor called ... Destructor called ...

It is very clear from the output that the destructor for all the objects is called to avert any memory leak. The memory allocated using new operator is being de-allocated using the delete operator. Notice the syntax of delete while de-allocating an array, the brackets ([]) precedes the name of the array after the delete operator.

docsity.com

new, delete outside Constructors and Destructors

Can new be called from some location other than constructor? The answer is yes and we usually need to do that. Suppose, we have an object of Student class. The name of the student is: Abdul Khaliq. So for the name attribute, the space is allocated dynamically to store the string Abdul Khaliq. When our program is running and we have already allocated space for the Abdul Khaliq string using the new operator, after sometime, we are required to increase the size of the string. Let’s say we want to change the string to Abdul Khaliq Khan now. So what we can do, without destroying this student object: De-allocate the name previously occupied string using the delete operator, determine the size of memory required with the help of strlen() function, allocate the memory required for the new string Abdul Khaliq Khan using the new operator and finally assign the returned pointer to the name data member.

Hence, we can call new and delete operators, not only outside the class to create objects but also within the class. The objects of the same class can have different sizes of memory space like in case of objects of Student class, student 1 object can be of one size and student 2 object can be of an another size, primarily varying because of string name. But independent of this object size, the destructor of the object remains the same and de-allocates memory for different objects regardless of their different sizes. delete operator is used from within the destructor to deallocate the memory. We call delete operator to determine the size of the memory required to be de-allocated and only provide it a pointer pointing to it.

Please note that C functions like malloc() and free() functions can also be used from within C++ code. But while writing classes inside C++ code, we prefer to use new and delete operators as they are designed to work with classes and objects.

main() Function and Classes

We used to discuss about main() function a lot while writing our programs in C. You might have noticed that while discussing about classes and objects, we are not talking about the main() function. This does not mean that main() function is not there in C++. It is there but it does not contain as much code in C++. But as you go along and write your own classess, you will realize that almost 90% of your program’s code lies inside the class definitions. So firstly we write our classes and main() function is written after classes have been defined. That is why the main() function is very small. Our example programs clearly depict this fact.

Class Abstraction

Whenever we write a class, we think about its users. Who are the ones going to use this class? The users are not only the main() function of the program but also our colleagues around us. Remember, we only expose interface to our users and not the class implementation. All what users need to know is provided in the interface, the

docsity.com

constructor function to initialize the data members to some meaningful values.

  • The usual practice is to use constructor to allocate memory or system resources and destructors to de-allocate or return the resources back to the system.
  • In C language, the region of memory allocated at runtime is called heap. However, in C++, the region of available memory is called free store. There are different functions in C and C++ to manipulate memory at runtime. However, all C functions are useable in C++ code.
  • The memory allocated from free store or heap is a system resource and is not returned back to the system unless explicitly freed using delete or free operators.
  • If the memory in the free store is not sufficient enough to fulfill the request, malloc() function returns NULL pointer. Similarly, the new function returns 0 in case the request could not be fulfilled.
  • Whenever we use new operator, the returned value from the new should be checked against 0 for any possible failures.
  • While writing classes, good programming practice is to write private keyword explicitly, despite the fact that this is the default scope. Additionally, the good practice is to write public or private keywords only once in the class or structure definitions, though there is no syntactical or logical problems in writing them multiple times.

docsity.com