Lists, Strings & Multi-dimensional arrays

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
Kernighan/Ritchie: Kelley/Pohl:
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Pointers and dynamic objects COMP171 Fall Pointers and dynamic objects/ Slide 2 Topics * Pointers n Memory addresses n Declaration n Dereferencing.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Dynamic memory allocation and Pointers Lecture 4.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
More About Data Types & Functions. General Program Structure #include statements for I/O, etc. #include's for class headers – function prototype statements.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
1 Dynamic Memory Allocation. 2 In everything we have done so far, our variables have been declared at compile time. In these slides, we will see how to.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Multidimensional Arrays. Syntax Type name [d1][d2][d3] {init list}; int X [3] [4] [5]; Leftmost subscript varies most slowly – known as row major order.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
CSE 220 – C Programming malloc, calloc, realloc.
Dynamic Allocation in C
Stack and Heap Memory Stack resident variables include:
Computer Organization and Design Pointers, Arrays and Strings in C
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Fundamentals of Characters and Strings
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
ENEE150 Discussion 07 Section 0101 Adam Wang.
Module 2 Arrays and strings – example programs.
Computer science C programming language Lesson 5
Arrays in C.
Programming Languages and Paradigms
Lecture 6 C++ Programming
Lists.
14th September IIT Kanpur
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Lists.
Object Oriented Programming COP3330 / CGS5409
Pointers and dynamic objects
7 Arrays.
Pointers, Dynamic Data, and Reference Types
More About Data Types & Functions
Pointers and Pointer-Based Strings
Dynamic Memory Allocation
Memory Allocation CS 217.
Outline Defining and using Pointers Operations on pointers
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
Dynamic Allocation in C
CPS120: Introduction to Computer Science
Dynamic Objects.
Data Structures & Algorithms
Pointers and dynamic objects
Programming Languages and Paradigms
Pointers and dynamic objects
Pointers, Dynamic Data, and Reference Types
CECS 130 Final Exam Review Session
Module 13 Dynamic Memory.
Dynamic Data Structures
Presentation transcript:

Lists, Strings & Multi-dimensional arrays

Data Storage Static Dynamic compiled into the disk-image of program always takes up space on disk & RAM usage depends on program logic Dynamic requested at runtime no space taken on disk usage as needed

Static Storage Examples: static int X; // initially=0 static int Y=5; extern int Z; // initially=0 Compiler reserves space for these Location for Y contains the 5 no runtime code used to put it there All KEEP their values, even after termination of the function

Dynamic Storage Examples: int X; int Y=5; // re-initialized at each call auto int Z; // "auto" is redundant // cannot use "auto" w/specification of a value auto int Q=5; // ILLEGAL Compiler adds code in function-start to REQUEST RAM for automatic variables Location for Y contains the 5 at fcn startup runtime code IS used to put it there

Dynamic Storage - 2 int X; int * xptr; xptr = &X; //xptr points to X (holds the RAM address of X) (NOT the value of X) printf ("X=%d", *xptr); (print the value of what xptr points to)

Dynamic lists Each node is a struct Struct data elements are created at "malloc" time Rules for static & auto apply Only static, const, integer variables may be initialized in a struct do it yourself at "malloc" time

Double-linked lists Simplifies traversals Slightly slower for insert/remove No need for temp ptr for internal insert/remove Uses more storage (trivial vs. data size) data bptr fptr first

Structure struct dl_list { mydatatype X; dl_list * prev; dl_list * next; };

List creation dl_list * start; // only creates a pointer start = (*dl_list) malloc (sizeof (dl_list)); // creates a SINGLE element start -> prev = &start; // pt BACK to home start -> next=NULL; // end of list // now add on a new node start -> next = (*dl_list) malloc (sizeof (dl_list)); start -> next -> next=NULL; // new end of list start -> next -> prev=start->next; //<- old end of list

Linked List Performance (textbook's names) O(n) Clear_list deletes the whole list Delete_list deletes ONE entry (one node) Insert_list inserts ONE entry (new node) Retreive_list gets ONE value (w/o harm) Replace_list replace ONE value (destructive) O(k) CreateList ListEmpty/Full state of the list = empty (t/f) ListSize # nodes

Other useful operations Insert/Delete_First Insert/Delete_Last Swap_entries Copy_List Problematic for single-linked list having fptrs: Traverse_back Reverse_List

Strings

basics strings are arrays Example stored like arrays accessed "like" arrays always a collection of "char" compiler adds the 0x00 ending IF initialized Example char * mystring = "aha"; // length=4, incl 0x00 compiler allocates space, sets ptr to it

Library functions strcopy – copies a string strncpy – copies 'n' chars strcat – appends string 2 to end of string 1 strcmp – compares 2 strings strch – finds a char in string strlen - returns length of string (w/o 0x00)

Danger!!! You must always ensure the 0x00 is there likewise, when searching, must be careful of actual max length compiler does NOT generate protective code does NOT check for overruns but DOES do it for arrays char * Z

Multidimensional Arrays

Syntax Type name [d1][d2][d3] {init list}; int X [3] [4] [5]; Leftmost subscript varies most slowly – known as row major order

Arrays as arguments #define SIZE 10 function X (int x, int y, int a [ ] [SIZE]) ; New standard as of 1999 implemented function X (int a[ ] [SIZE]) { ... } highest dimension is required

Dynamic arrays (C) Requires the use of the malloc function Syntax: p=malloc (amount) Return space with free p; int * ptr; // a ptr to an array of int's ptr= malloc (5*sizeof(int)); // constant size if (ptr==NULL) … is better than if (ptr==0)…

Dynamic arrays (C-2) Alternately, use the calloc function Syntax: p=calloc (amount, sizeof(..)) Return space with free p; int * ptr; // a ptr to an array of int's ptr= calloc (5,sizeof(int)); // constant size if (ptr==NULL) … is better than if (ptr==0)…

Dynamic arrays (C++) Requires the use of the new operator Syntax: new Type [amount] Return space with delete p; int * ptr; // a ptr to an array of int's ptr= new int[5]; // note constant size if (ptr==NULL) … is better than if (ptr==0)…

Dynamic arrays (C++2) int y; cin>>y; int * ptr; // a ptr to an array of int's ptr= new int[y]; // note size determined at //run time Return space with delete ptr; BUT: the size of the array is still FIXED (i.e.; size does not vary during execution)

Deleting storage Given the previous array: delete ptr; // deletes a SINGLE datum the one memory loc that ptr refers to Even if it's an array (only deletes array[0]) delete [ ] ptr; // deletes the whole array

Strings C-style strings (array of char) C++ style (true) strings char *x; x=new char [n] Or Using namespace std; #include <cstring> C++ style (true) strings #include <string.h> or using namespace std; #include <string>