Dynamic Memory Allocation using a Custom Implemented Single-Linked List

Similar documents
C++ INTERVIEW QUESTIONS

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

The C Programming Language course syllabus associate level

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

1 Abstract Data Types Information Hiding

Introduction to Data Structures

An Incomplete C++ Primer. University of Wyoming MA 5310

Integrating the C++ Standard Template Library Into the Undergraduate Computer Science Curriculum

Common Data Structures

Object Oriented Software Design II

Object Oriented Software Design II

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

CSI33 Data Structures

C++ Language Tutorial

DATA STRUCTURES USING C

Tutorial on C Language Programming

CS107L Handout 04 Autumn 2007 October 19, 2007 Custom STL-Like Containers and Iterators

El Dorado Union High School District Educational Services

10CS35: Data Structures Using C

Sources: On the Web: Slides will be available on:

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Java Interview Questions and Answers

Paper Creating Variables: Traps and Pitfalls Olena Galligan, Clinops LLC, San Francisco, CA

PES Institute of Technology-BSC QUESTION BANK

Chapter 5 Names, Bindings, Type Checking, and Scopes

Curriculum Map. Discipline: Computer Science Course: C++

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Passing 1D arrays to functions.

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

Basics of C++ and object orientation in OpenFOAM

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

C++ Overloading, Constructors, Assignment operator

BCS2B02: OOP Concepts and Data Structures Using C++

Basics of I/O Streams and File I/O

Classes and Pointers: Some Peculiarities (cont d.)

C++ Programming Language

C++FA 3.1 OPTIMIZING C++

Lecture 12 Doubly Linked Lists (with Recursion)

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Moving from CS 61A Scheme to CS 61B Java

C++FA 5.1 PRACTICE MID-TERM EXAM

Cours de C++ Utilisations des conteneurs

Analysis of a Search Algorithm

recursion, O(n), linked lists 6/14

Module 2 Stacks and Queues: Abstract Data Types

Cpt S 223. School of EECS, WSU

Sequences in the C++ STL

Linked List as an ADT (cont d.)

Linked Lists: Implementation Sequences in the C++ STL

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

7.1 Our Current Model

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

N3458: Simple Database Integration in C++11

CS193D Handout 06 Winter 2004 January 26, 2004 Copy Constructor and operator=

Illustration 1: Diagram of program function and data flow

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Java Application Developer Certificate Program Competencies

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

Variable Base Interface

A Summary of Operator Overloading

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

sqlpp11 - An SQL Library Worthy of Modern C++

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

Sequential Program Execution

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

Why you shouldn't use set (and what you should use instead) Matt Austern

Syllabus OBJECT ORIENTED PROGRAMMING C++

Top 72 Perl Interview Questions and Answers

MPLAB TM C30 Managed PSV Pointers. Beta support included with MPLAB C30 V3.00

CSC230 Getting Starting in C. Tyler Bletsch

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Functions and Parameter Passing

ECS 165B: Database System Implementa6on Lecture 2

Stacks. Linear data structures

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

CSE 211: Data Structures Lecture Notes VII

Calling the Function. Two Function Declarations Here is a function declared as pass by value. Why use Pass By Reference?

CSE 373: Data Structure & Algorithms Lecture 25: Programming Languages. Nicki Dell Spring 2014

A brief introduction to C++ and Interfacing with Excel

CORBA Programming with TAOX11. The C++11 CORBA Implementation

Data Structures using OOP C++ Lecture 1

Class 16: Function Parameters and Polymorphism

Semantic Analysis: Types and Type Checking

Demonstrating a DATA Step with and without a RETAIN Statement

6.S096 Lecture 1 Introduction to C

Chapter 3: Restricted Structures Page 1

C Dynamic Data Structures. University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Persistent Binary Search Trees

Transcription:

Dynamic Memory Allocation using a Custom Implemented Single-Linked List Anders Eiler April 8, 2013 1 Introduction The objective of this paper is to describe how to dynamically allocate memory at runtime and store any given data type in a custom implementation of a Single-Linked List in the C++ programming language. I will start by talking briefly about the fundamentals of memory allocation, dynamic memory allocation, pointers, iterators, templates and operator overloading to give an understanding of the elements behind the final implementation of the Single-Linked List. Following this is a description of my implementation of a Single-Linked List. Finally, I will compare my findings with experience from PHP, a scripting language that is probably the most opposite to C++ one can find. But first, let us start with some thoughts on memory allocation in general and why this is an interesting topic. Memory allocation Usually, the size of an array has to be declared at compile time, meaning it has to be defined in the source code. 1 int ary [3]; // gives us an array named ary with 3 slots to contain ints This can lead to a number of potential issues: It can be difficult to conditionally declare a variable. The size of any array must be determined at compile-time. As an example, trying to use the same approach as in the previous Listing to dynamically set the size of an array during runtime would fail: 1

1 std :: cout << " Type in the size of your array : "; 2 int arysize ; 3 cin >> arysize ; 4 5 int ary [ arysize ]; // this fails, as the size of an array must be constant! Any C++ programmer needs to be aware of the memory usage of his program. The fact that we can t dynamically set the size of an array or resize it dynamically during runtime is often inappropriate, as we may not know the actual size of an array at compile-time. Consider a case where we want to save the zip-code of anybody shopping at a grocery store for one day in C++. We do not know in advance how many shoppers there will be. One solution could be to make the array so big, that we know there is enough space: 1 int shopperszipcodes [10000]; That, however, is also inconvenient for a number of reasons: If there are any less than 10000 shoppers that day, it will be a waste of memory to allocate enough space in the array for 10000. If there are more than 10000 shoppers in one day, it can lead to either an artificial buffer where any shopper after no. 10000 is not saved, or it may lead to buffer overflow which will cause the program to crash. The answer to this question is usage of dynamic memory allocation which, in C++, is natively implemented using the new and delete. I will be using these in my implementation of the Single-Linked list (which I ll get back to later), but first, let us take a look at the basics of pointers, Dynamic memory allocation, iterators, templates and operator overloading. 2 Pointers When you want to save some value in some variable, it is stored in the memory, and the variable you have at hand points to the value that you have assigned to it in the memory. As soon as a variable is declared, the appropriate amount of space (depending on the type) is allocated in the memory. All memory cells has an address. The actual addresses are different from system to system, but let us assume our address cells are named from 1000 and on forth. To get the address of the memory cell that the value of a variable is stored in, the & (ampersand) operator is used. 2

1 int firstvar = 10; // a regular declration of a variable that is assigned the value. 2 cout << " The address of firstvar is: " << & firstvar ; // gets the address in the memory. In our case, it returns "1000". In the same way that & (ampersand) gets the address in the memory, the * dereferences a pointer. A pointer always contains the address in the memory of the variable it is pointing to. If you want the content of that memory cell, the pointer has to be dereferenced using the *. The * is also used when declaring a new pointer. It is the same sign used in two different contexts. It is shown in the following example: 1 # include <iostream > 2 using namespace std ; 3 4 int main () { 5 int valone, valtwo ; 6 int * ptr ; 7 8 ptr = & valone ; // assign the address of valone to the pointer. 9 * ptr = 10; // set the value of the memory cell that the pointer is pointing to = 10. 10 ptr = & valtwo ; // now set the pointer to point to the address of valtwo 11 * ptr = 20; // same as before, set the value to 20. 12 13 cout << " The value of valone = " << valone << "\ n"; // prints 10 14 cout << " The value of valtwo = " << valtwo << "\ n"; // prints 20 15 16 return 0; 17 } Pointers can be manipulated using Pointer arithmetics. If you have a pointer p1 that points to the address 1010, then p1+1 would make the pointer point to address 1011. This allows us to iterate tough an array using pointers. Pointers can also be parsed as arguments to functions. This is very powerful, compared to parsing variables by value, which would parse a copy of the value to the function. In that case, the value will be limited to the scope of the function. When a pointer is parsed instead, the function is able to modify the value of the pointer in the global scope. Now we have the basics of memory allocation and pointers in place. accelerate and look at Dynamic Memory Allocation! Let us 3

3 Dynamic Memory Allocation In the case that we need dynamic memory allocation, we need to use the new operator: 1 // generic notation : 2 // pointer = new type ; 3 // pointer = new type [ number_of_elements ]; 4 5 int * ptr ; // create a new pointer. 6 ptr = new int ; // allocate memory for an integer and point ptr to the address of that integer. 7 * ptr = 10; The new operator always returns the address of the object it has allocated. Any dynamically allocated memory is placed in the heap of the system. The address that is returned can be saved in a pointer, which can later be used to dereference the pointer to gain access to the actual value. In C++, one must free any memory that has been dynamically allocated. This is done using the delete operator. 1 delete ptr ; The procedure is almost the same when dynamically allocating arrays compared to dynamically allocating single variables: 1 int arysize = 20; 2 int * aryptr = new int [ arysize ]; 3 aryptr [15] = 1; 4 delete [] aryptr ; The main differences is that, when working with arrays, the delete[] must be used to free the memory. Once the array is initiated, it can be handled like any other array in C++. I will use dynamic memory allocation to assign new nodes to my Single-Linked List [SLL], which is what enables the dynamic addition of new nodes at runtime (and thereby dynamic memory allocation) in the SLL. 4 Operator Overloading, Iterators & Templates This section will briefly cover each of the three topics, as these are also some of the building blocks of my custom SLL implementation. 4

Operator Overloading From regular mathematics we are used to working with operators such as (+), (-), (=) etc. We also know how to use these in programming languages, comparing using (==) etc. The concept of Operator Overloading allows an operator to be extended to work with not just the built-in types, but also custom types and classes. In my SLL I need to compare node elements, increment iterators etc. To make this as easy as possible, operator overloading is used with the iterator class. 1 iterator operator ++( int ) { 2 iterator itr_old = * this ; 3 ++(* this ); 4 return itr_old ; 5 } The Listing above is a snippet from the SLL implementation. It shows an operator overloading of the (++) operator. It is used to increment an iterator (covered in the Section 4). As iterators in my SLL are pointers to nodes, the regular (++) operator, which per default is build to work on incrementing integers, would fail. The operator overloading states that, whenever the (++) operator is used on an object of the iterator class, it means what is defined on line 2-4, namely to increment a pointer (which we saw in Section 2 is possible) and return the updated iterator. Iterators An iterator is an object that has the ability to iterate through a range of elements. It will point to an element in the range, and using a defined set of operators such as (++), (- -) and (*) (dereference), it can iterate through the range. Such a range can be a list, an array or similar. An iterator is often implemented as a pointer that is pointing to an element in e.g. an array. It can iterate through the array using (++) to increment the pointer. Iterators are fundamental to the implementation of my SLL for two reasons: 1) it allows the client program to iterate through the list without having to deal with the internals of its implementation and 2) by following the standard of iterators, it allows the client program to switch between my SLL and other container libraries. The following Listing shows the implementation of an iterator in my SLL. It is based on the base class of an iterator and extends the default iterator class. 1 class iterator : public std :: iterator < std :: forward_iterator_tag, T, size_t, T*, T& > { 2 5

3 public : 4 Node * list_curpos ; // an internal pointer to the current position in the list 5 typedef T* pointer ; 6 typedef T& reference ; 7 iterator ( Node * cur_node = NULL ) : list_curpos ( cur_node ) {} 8 9 // some operator overloading that allows us to work with the linked list 10 reference operator *() { return list_curpos - > value ; } 11 pointer operator - >() { return &(** this ); } 12 iterator & operator ++() { list_curpos = list_curpos - > next ; return * this ; } // prefix incremental ( no argument ) 13 iterator operator ++( int ) { iterator itr_old = * this ; ++(* this ); return itr_old ; } // postfix incremental ( hence the int argument ) 14 bool operator ==( iterator const & emt ) { return list_curpos == emt. list_curpos ; } 15 bool operator!=( iterator const & emt ) { return!(* this == emt ); } 16 }; Templates C++ is a type-strong programming language. My SLL should be designed such that its nodes can contain elements of any given value type, as long as all elements in a list is of the same type. Instead of defining the functions of the SLL with a specific type, they are defined as Function Templates a special function that can operate on generic types. This means that a Function Template can be used on more than one type or class without having to repeat the entire function code for each data type that it is supposed to work with. 1 template < typename T> 2 class LinkedList { 3 4 private : 5 struct Node { 6 Node (T val ) : next ( NULL ), value ( val ) {} 7 Node * next ; 8 T value ; 9 }; 10 11 public : 12 void push_front ( T const & element ); 13 void push_back ( T const & element ); 14 void insert_after ( iterator itr, T const & element ); 15 }; The Listing above shows a snippet of the class definition of my LinkedList class which is the basis of the SLL. Line 1 tells that the typename will be identified T in the class. Line 5-9 defines the nodes that the SLL is made up of. Its values are of the type <T>, meaning it can be whatever is chosen upon initialization of 6

the SLL. The same goes for the three methods on line 12-15, where the datatype <T> is used. 1 LinkedList <int > mylist ; The Listing above shows how to initialize a SLL containing integers. Any data type can be used here - build in ones or custom made ones, which is the case in my implementation. Now that all the basics are in place, let us look at the actual Single-Linked List. 5 Implementation of a Single Linked List C++ comes with a build-in type std::list, which is a double-linked list. This SLL implementation is mostly a proof of concept, showing how a SLL that can take any type object can be implemented. In most cases, a double-linked list is preferable as it is faster to traverse and update. However, in some cases it can be efficient to use a SLL over a double-linked list. A SLL uses less memory as it only has one pointer per node (to the next element in the list), and under certain circumstances it can be preferred. The SLL consists of Node objects. Each node object has a value of type <T> and a pointer to the next node in the list. The value contains an object of the type that is defined upon initialization of the list. The SLL is implemented using Function Templates to ensure that it can contain any type of objects. Iterators are implemented to hide the internal functionality of the list, and only present the client program with the iterator for navigation and manipulation of the list. Operator Overloading is used to enable the list to manipulate the iterators. Here I will present selected code snippets of the SLL. The parts regarding operator overloading, iterators and templates was presented in Section 4. The rest of the code follows the same idea by using the Template <typename T>, changing only the body of each function. These can be seen in the attached files. 1 template < typename T> 2 typename LinkedList <T >:: iterator LinkedList <T >:: begin () { 3 return iterator ( list_head ); 4 } This first Listing shows the begin() method that returns an iterator pointing to the head of the list. This is very useful in several cases, e.g. when traversing the list. It once again shows how a template is used to use the type T (the generic type). It returns an iterator which is constructed, and points to list_head, a pointer that always points to the node in the head of the list. 1 template < typename T> 2 void LinkedList <T >:: push_front ( T const & element ) { 7

3 Node * newnode = new Node ( element ); 4 newnode - > next = list_head ; 5 list_head = newnode ; 6 7 if( list_tail == NULL ) { 8 list_tail = newnode ; 9 } 10 11 list_size ++; 12 } This next Listing shows the push_front() method. It takes an element (once again of type T) and inserts it at the head of the list. A new node is created on line 3, and the head of the list is set to point to this node. The next pointer of this node is set to point to the old head, and this new node is thereby inserted as the head of the list. 1 dataelement * e1 = new dataelement ; 2 e1 -> name = " Element #1"; 3 e1 - > phone = 1111; 4 5 // (...) 6 7 dataelement * e5 = new dataelement ; 8 e5 -> name = " Element #5"; 9 e5 - > phone = 5555; 10 11 LinkedList < dataelement > elst ; 12 LinkedList < dataelement >:: iterator eit ; 13 elst. push_front (* e1); 14 elst. push_back (* e2); 15 elst. push_back (* e3); 16 17 eit = elst. begin (); eit ++; 18 elst. insert_after (eit, *e4); 19 elst. push_back (* e5); 20 21 for ( eit = elst. begin (); eit!= NULL ; ++ eit ) { 22 std :: cout << eit -> name << " - " << eit -> phone << "\n"; 23 } 24 25 // Output : 26 // Element #1-1111 27 // Element #2-2222 28 // Element #4-4444 29 // Element #3-3333 30 // Element #5-5555 This final Listing shows a part of the client-program that comes with the SLL. It creates 5 objects of the type dataelement, which is a type defined as a struct to be the value of the nodes. The 5 dataelement objects are added to the list using different methods, some are then removed and it is finally printed. 8

This client program is static, but another client program that takes user input is able to add new nodes to the SLL in the same way, and thereby dynamically assigning memory at runtime. 6 Comparison with Other Programming Languages - PHP My background is based on various scripting languages such as PHP and Python. The steps in going from such a scripting language to a programming language like C++ are enormous. Amongst the most noticeable things that I have found are: Pointers: References etc. does exists in PHP, but is not used nearly as much as in C++. Having to wrap your mind around using pointers for everything is a new but very powerful concept, compared to e.g. PHP scripting. Object types & Templates: PHP is type coerced, meaning that it doesn t really care if a variable contains an int, string or double. Having to determine the type of everything at the time it is initiated forces you to think further, which I personally see as a good thing. Operator overloading: This does not exists in PHP, and is therefore a completely new concept when coming from such a background. However, looking at C++ as a whole, the fact that it is type safe etc., means that operator overloading is very useful, and it is used a lot in the implementation of my SLL. Memory management: This concept doesn t really exists in PHP either. Of course you can make a script that consumes all available memory, but having to think about memory management, garbage collecting in regular programming is a new - but also good - aspect of writing software. The four bullets above are just some of the things I noticed the most when I had to switch from a scripting language to doing advanced programming in C++. It is possible to implement a SLL in PHP, but the way you must think when programming (not scripting!) in C++ is - in my eyes - a much better approach to software engineering, as it forces you to consider and think about more deep aspects of how you want your program to work. 9

The following references have been used as inspiration for the theory, examples and thoughts on the topics of memory allocation, pointers, iterators, templates, operator overloading and linked lists: References [1] Alex. Dynamic memory allocation with new and delete, 2007. URL: http://www.learncpp.com/cpp-tutorial/ 69-dynamic-memory-allocation-with-new-and-delete/. [2] David Bolton. C++ tutorial - advanced pointers, 2007. URL: http:// cplus.about.com/od/learning1/ss/pointers2.htm. [3] computerquip cplusplus.com. Linked lists, 2011. URL: http://www. cplusplus.com/articles/lw6ac542/. [4] cplusplus.com. Dynamic memory. URL: http://www.cplusplus.com/ doc/tutorial/dynamic/. [5] cplusplus.com. Lists. URL: http://www.cplusplus.com/reference/ list/list/. [6] cplusplus.com. Pointers. URL: http://www.cplusplus.com/doc/ tutorial/pointers/. [7] cplusplus.com. Templates. URL: http://www.cplusplus.com/doc/ tutorial/templates/. [8] Nasif M. How to create linked list using c/c++, 2008. URL: http://www.codeproject.com/articles/24684/ How-to-create-Linked-list-using-C-C. [9] Bjarne Stroustrup. The C++ Programming Language - Iterators, p. 549. 1997. [10] Bjarne Stroustrup. The C++ Programming Language - Operator Overloading, p. 261. 1997. [11] Bjarne Stroustrup. The C++ Programming Language - Pointers, Arrays, and Structures, p. 87. 1997. [12] Bjarne Stroustrup. The C++ Programming Language - Templates, p. 327. 1997. [13] Varun Sud. Pointers usage in c++: Beginners to advanced, 2005. URL: http://www.codeproject.com/articles/11560/ Pointers-Usage-in-C-Beginners-to-Advanced#c. 10