UFS003C3 Lecture 15 Data type in C & C++ Using the STL.

Slides:



Advertisements
Similar presentations
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Advertisements

Linked Lists.
Data Structures Linked Lists Linked List Basics. Array Disadvantages Arrays, although easy to understand have lots of disadvantages Contiguous Memory.
Dynamic memory allocation
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.
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Dynamic Memory Management
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts : Introduction to Computer Systems 17 th Lecture, Oct. 21, 2010 Instructors: Randy Bryant.
Chapter 6 Data Types
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Chris Riesbeck, Fall 2007 Dynamic Memory Allocation Today Dynamic memory allocation – mechanisms & policies Memory bugs.
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.
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:
Informática II Prof. Dr. Gustavo Patiño MJ
CS100A, Fall 1997, Lecture 241 CS100A, Fall 1997 Lecture 24, Tuesday 25 November (There were no written notes for lecture 23 on Nov. 20.) Data Structures.
Dynamic Objects. COMP104 Lecture 31 / Slide 2 Static verses Dynamic Objects * Static object n Memory is acquired automatically  int A[10]; n Memory is.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Chapter 19 Data Structures Data Structures A data structure is a particular organization of data in memory. We want to group related items together.
CSIS 123A Lecture 6 Strings & Dynamic Memory. Introduction To The string Class Must include –Part of the std library You can declare an instance like.
The Rest of the Story.  Constructors  Compiler-generated  The Initializer List  Copy Constructors  Single-arg (conversion ctors)  The Assignment.
Outline Midterm results Static variables Memory model
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
February 11, 2005 More Pointers Dynamic Memory Allocation.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
CS 11 C++ track: lecture 4 Today: More on memory management the stack and the heap inline functions structs vs. classes.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Pointers review Let a variable aa be defined as ‘int *aa;’, what is stored in aa? Let a variable aa be defined as ‘int ** aa;’ what is stored in aa? Why.
1 Data Structures CSCI 132, Spring 2014 Lecture 10 Dynamic Memory and Pointers.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
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.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
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.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Dynamic Memory Allocation Suppose we defined the data type: struct custrec.
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
More C++ Features True object initialisation
ECE Application Programming
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
11 Introduction to Object Oriented Programming (Continued) Cats.
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 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.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
CMSC 341 Lecture 2 – Dynamic Memory and Pointers (Review)
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
group work #hifiTeam
Pointers, Dynamic Data, and Reference Types
Dynamic Memory Allocation
Dynamic Memory A whole heap of fun….
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
Dynamic Memory A whole heap of fun….
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
Presentation transcript:

UFS003C3 Lecture 15 Data type in C & C++ Using the STL

Arrays in C Arrays are a very simple form of storage in C –int list[20]; –char word[30]; –struct thing mylist[10]; Access is simple via an index –x = list[9]; –printf(“%c”,word[29]); –mylist[2].element=x; Always remember C arrays start at 0 and go to N-1!

Pointers and arrays Pointers can easily be used with C arrays –Char *cp, word[20]; –strcpy(word,”hello borld”); –cp=word; //or &word[0] –*(cp+7)=‘w’; //same as word[7]=‘w’ Always remember –char *cp is not the same as –char word[20];

Arrays in C Positive points –Arrays are a very convenient & fast access method of data storage –They can be randomly accessed –Very easy to use data structure Negative points –They are of fixed size at compilation time.i.e they can’t grow/shrink –It is difficult to insert/delete data

Dynamic memory access in C Frequently a program will require memory to be dynamically created and deleted during the program’s execution time. The data must be requested through a library called malloc and released by free. These routines use pointers to memory and will create or free a requested size of memory.

Using malloc() malloc is used to request memory from a system area called the ‘heap’ –char *cp; –cp = (char *) malloc(80); –if ( cp != NULL ) ……. The pointer cp now points to a block of 80 characters. NULL can be returned if there is no or not enough memory.

Using free() Free is used to free up a block of memory previously allocated by malloc(). –char *cp; –cp = (char *) malloc(80); –……..doing something with the memory –free(cp) Data in freed memory will be inaccessible and overwritten. Can only free memory that has been ‘malloced’

Arrays and pointers in C++ C++ allows the full use of arrays and pointer access in the same way that C does. So any access in C is permissible in C++ However C++ has better ways of doing some more complex data structure access controls and more clearly defined memory allocation.

Memory allocation in C++ Dynamic memory allocation is simpler in C++ and often automatic With pre-created classes the memory allocation is dealt with by the constructor and other methods Explicit requests for heap storage can be made with the new keyword.

Implicit memory allocation When using a library class, such as string, for example, the constructors in that class will automatically do the memory allocation. –string s(“hello”), t, u; –t = “world”; –U = s + t; All the memory allocation is dealt with either by the constructor or the member functions.

Explicit memory allocation C++ has the new operator. This allocates heap memory for specified objects or classes of objects –int *p = new int; The memory can be released using the delete operator –delete p;

Explicit memory allocation The new operator can actually be used to create arrays which are dimensioned at run time. –int n; –cout << “enter an array length “ ; –cin >>n; –char * cp = new char[n]; The array is called with new and the dimension inputted.