Pointers & Dynamic Memory Review C Pointers Introduce C++ Pointers

Similar documents
Call The Project Dynamic-Memory

Functions & Memory Maps Review C Programming Language

OOP- 4 Templates & Memory Management Print Only Pages 1-5 Individual Assignment Answers To Questions 10 Points - Program 15 Points

2 2

Function Terminology

C:\Temp\Templates. Download This PDF From The Web Site

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

TI2725-C, C programming lab, course

OOP-1-Review-HW-(Part B).docx CSCI 2320 Initials P a g e 1. Print Name Time Required =. Hrs. Signature (pledged) Static Array Review

RECOMMENDATION. Don't Write Entire Programs Unless You Want To Spend 3-10 Times As Long Doing Labs! Write 1 Function - Test That Function!

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Pointers. Addresses in Memory. Exam 1 on July 18, :00-11:40am

CS102 Software Engineering Principles

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

Recitation 2/18/2012

CS 322 Operating Systems Practice Midterm Questions

C-String Library Functions

Incoming Exam. CS 201 Introduction to Pointers. What is a Pointer? Pointers and Addresses. High Speed Memory (RAM) Size of Variable Types.

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

Lab # 4. Files & Queues in C

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

Dynamic Allocation of Memory

EL2310 Scientific Programming

Memory, Arrays, and Parameters

Number Review. Lecture #3 More C intro, C Strings, Arrays, & Malloc Variables. Clarification about counting down

Fundamentals of Programming

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Dynamic Data Structures. CSCI 112: Programming in C

PRINCIPLES OF OPERATING SYSTEMS

COMP26120: Algorithms and Imperative Programming. Lecture 5: Program structuring, Java vs. C, and common mistakes

The University of Nottingham

The University of Nottingham

CSCI 2132 Software Development. Lecture 29: Dynamic Memory Allocation

Week 5, continued. This is CS50. Harvard University. Fall Cheng Gong

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

Structures and Unions in C

Lab 8. Follow along with your TA as they demo GDB. Make sure you understand all of the commands, how and when to use them.

Common Misunderstandings from Exam 1 Material

C for Java Programmers 1. Last Week. Overview of the differences between C and Java. The C language (keywords, types, functies, etc.

CS 237 Meeting 19 10/24/12

C++ ARRAYS POINTERS POINTER ARITHMETIC. Problem Solving with Computers-I

Managing Memory. (and low level Data Structures) Lectures 22, 23. Hartmut Kaiser.

CSE 12 Spring 2016 Week One, Lecture Two

THE GOOD, BAD AND UGLY ABOUT POINTERS. Problem Solving with Computers-I

Pointers, Dynamic Data, and Reference Types

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

CS 241 Data Organization Binary Trees

When you add a number to a pointer, that number is added, but first it is multiplied by the sizeof the type the pointer points to.

Recitation: C Review. TA s 20 Feb 2017

Lecture 15a Persistent Memory & Shared Pointers

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

CpSc 1010, Fall 2014 Lab 10: Command-Line Parameters (Week of 10/27/2014)

C++ for Java Programmers

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

IT 252 Computer Organization and Architecture Introduction to the C Programming Language

Binghamton University. CS-211 Fall Dynamic Memory

COSC345 Software Engineering. The Heap And Dynamic Memory Allocation

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; }

unsigned char memory[] STACK ¼ 0x xC of address space globals function KERNEL code local variables

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Lecture 2, September 4

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.

COMP 2355 Introduction to Systems Programming

Foundations of Network and Computer Security

COMP26120: Linked List in C (2018/19) Lucas Cordeiro

C PROGRAMMING Lecture 5. 1st semester

1. Which of the following best describes the situation after Line 1 has been executed?

These problems are provided to you as a guide for practice. The questions cover important concepts covered in class.

Memory Corruption 101 From Primitives to Exploit

Memory Management. a C view. Dr Alun Moon KF5010. Computer Science. Dr Alun Moon (Computer Science) Memory Management KF / 24

HW1 due Monday by 9:30am Assignment online, submission details to come

EL2310 Scientific Programming

CA341 - Comparative Programming Languages

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Self-referential Structures and Linked List. Programming and Data Structure 1

Dynamic arrays / C Strings

Dynamic memory allocation

DYNAMIC ARRAYS; FUNCTIONS & POINTERS; SHALLOW VS DEEP COPY

Welcome! COMP s1. Programming Fundamentals

Instructions: Submit your answers to these questions to the Curator as OQ02 by the posted due date and time. No late submissions will be accepted.

Kurt Schmidt. October 30, 2018

Dynamic Allocation of Memory

RECOMMENDATION. Don't Write Entire Programs Unless You Want To Spend 3-10 Times As Long Doing Labs! Write 1 Function - Test That Function!

CS61, Fall 2012 Section 2 Notes

COMP 2355 Introduction to Systems Programming

Class Information ANNOUCEMENTS

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

Memory Allocation in C

CMPSC 497 Other Memory Vulnerabilities

Array Initialization

Announcements. Lecture 05a Header Classes. Midterm Format. Midterm Questions. More Midterm Stuff 9/19/17. Memory Management Strategy #0 (Review)

CSCB09: Software Tools and Systems Programming. Announcement. The address space. Memory model

LAB #8. GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:

# 1. Objectives. Dangling Pointers FirstName & LastName - Pointers Reference Memory Incorrect Memory! Not A Good Constructor!

CS 322 Operating Systems Programming Assignment 4 Writing a memory manager Due: April 5, 11:30 PM

CSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community

Transcription:

Pointers & Dynamic Memory Review C Pointers Introduce C++ Pointers Data Abstractions CSCI-2320 Dr. Tom Hicks Computer Science Department

c http://carme.cs.trinity.edu/ thicks/2320/schedule.html http://carme.cs.trinity.edu/thicks/2320/schedule.html 2

c 3

What You Should Do For Next Class Review C Dynamic Memory malloc & free Do C++ Dynamic Memory new & delete Begin C++ Classes

c 5

What You Should Do For Next Class Read Course Outline Complete the two-page questionnaire. Install Visual Studio 2017 Professional On Your Computer (if you have not done so) Complete OOP-1 Homework. (It is the longest lab form that you will do all semester - but only two pages of it have not been in the review recommendations that I sent out to those registered during the break) It Is Important That We Make Sure That The Review Material Does Not Prevent You From Completing The CSCI 2320 Material.

Check Web Site For Quizzes Practice - Review For Upcoming Quiz 1 Using Visual Studio Check The Class Schedule Daily! I Will Either Announce Quizzes In Class Or Post On The Web Site Two Days In Advance

8

9

Call The Project Dynamic-Memory 3

11

Add This Code To Main # include "Utilities.hpp" int main(int argc, char * argv[]) { puts ("-------------- Start Of Main --------------\n"); short int *PtrNo; printf("ptrno = %ld\n", PtrNo); puts("--------------- End Of Main ---------------\n"); } HitCarriageReturnToContinue(); return (0); Run The Program

Most C/C++ Compilers Will Generate No Errors Visual Studio Provides A Number Of Safeguards That Is Not Incorporated Into Other Compilers Memory gets_s, strcpy_s, etc.

What Is The Problem? short int *PtrNo; Unknown Value In &1000? 4 bytes * short int?? *PtrNo &1000

15 15

Valid Memory Address? short int *PtrNo; 4 bytes * short int?? *PtrNo &1000 1 GB = 1,048,576 bytes If Your Computer Has 8 GB of RAM, Valid Memory Addresses Would Be 8,387,743 <=?? <= 0 8,387,743 On most C/C++ compilers, if the?? Garbage memory address is in the Valid Range, the compiler continues: 0 (*PtrNo) = 5 Would Change Something It may mess up your word processor it may mess up your ability to access the Internet it may mess up your ability to print it may mess up another part of the program, etc.

InValid Memory Address? short int *PtrNo; 4 bytes * short int?? *PtrNo &1000 1 GB = 1,048,576 bytes If Your Computer Has 8 GB of RAM, Valid Memory Addresses Would Be 8,387,743 <=?? <= 0 8,387,743 On most C/C++ compiler(*ptrno) = 5 Would Crash The System. 0 This means that a program might compile one moment (because?? is in the valid memory range) and not compile the next moment.

18 18

Better Practice Avoid Dangling Pointers # include "Utilities.hpp" NULL int main(int argc, char * argv[]) &1000 { puts ("-------------- Start Of Main --------------\n"); short int *PtrNo = NULL; printf("ptrno = %ld\n", PtrNo); 4 bytes * short int PtrNo puts("--------------- End Of Main ---------------\n"); } HitCarriageReturnToContinue(); return (0); Hopefully You Learned This In Your C Class

20 20

short int *PtrNo = NULL; Review Malloc & Free (from C) - 1 PtrNo 4 bytes * short int 924240 NULL &1000 PtrNo = (short int *) malloc(sizeof(short int)); free(ptrno); 2 bytes short int This malloc call requests, of the compiler, a contiguous block of memory that is 2 bytes in size.?? &924240 If the compiler is unable to provide this, then PtrNo is filled with NULL. Function free must return the allocated memory back to the operating system; failure to do that results in a "memory leak".

Review Malloc & Free (from C) - 2 short int *PtrNo = NULL; PtrNo 4 bytes * short int &1000 PtrNo = (short int *) malloc(1000000* sizeof(short int)); free(ptrno); NULL This malloc call requests, of the compiler, a contiguous block of memory that is 2,000,000 bytes in size. If the compiler is unable to provide this, then PtrNo is filled with NULL ASSUME THAT IS THE CASE! Function free will create a problem when it tries to return 2,000,000 bytes of memory beginning at &0!

Review Malloc & Free (from C) - 3 short int *PtrNo = NULL; PtrNo 4 bytes * short int NULL &1000 PtrNo = (short int *) malloc(1000000* sizeof(short int)); if (PtrNo!= NULL) free(ptrno); I hope you have been taught that you should chase each and every request for dynamic memory with a test to verify that malloc was successful.

short int *PtrNo = NULL; Review Malloc & Free (from C) - 4 PtrNo 4 bytes * short int 15298032 NULL &1000 2 bytes short int PtrNo = (short int *) malloc(sizeof(short int)); printf("ptrno = %ld\n\n", PtrNo); if (PtrNo!= NULL) free(ptrno);?? &1529803 printf("ptrno = %ld\n\n", PtrNo); PtrNo = NULL; printf("ptrno = %ld\n\n", PtrNo); Note that when free is called, the compiler does not automatically assign NULL to the value this should be done by the programmer.

short int *PtrNo = NULL; Review Malloc & Free (from C) - 5 PtrNo 4 bytes * short int 15298032 NULL &1000 2 bytes short int 127?? &1529803 PtrNo = (short int *) malloc(sizeof(short int)); if (PtrNo!= NULL) { (*PtrNo) = 127; } free(ptrno); PtrNo = NULL; Note that when free is called, the compiler does not automatically assign NULL to the value this should be done by the programmer.

26 26

27 27

Review New & Delete (from C++) - 1 short int *PtrNo = NULL; PtrNo 4 bytes * short int 10165528 NULL &1000 PtrNo = new short int; if (PtrNo!= NULL) { (*PtrNo) = 127; printf("ptrno = %ld\n\n", PtrNo); printf("*ptrno = %hi\n\n", *PtrNo); delete PtrNo; PtrNo = NULL; } 2 bytes short int 127?? &10165528

Review New & Delete (from C++) - 1 # define MAX 10 short int *PtrNo = NULL; PtrNo PtrNo = new short [MAX]; if (PtrNo!= NULL) { 4 bytes * short int 10165528 NULL &1000 for (int Pos = 0; Pos < MAX; Pos++) PtrNo[Pos] = 10 * Pos; for (int Pos = 0; Pos < MAX; Pos++) cout << setw(5) << PtrNo[Pos]; 20 bytes 10 short int 9 8 7 6 5 4 3 2 1 0???????????????????? 90 80 70 60 50 40 30 20 10 0 &10165528 } delete [] PtrNo; PtrNo = NULL;

30 30

struct Part { char Name[24]; long No; }; Create A Struct Called Part 24 character Name long No

Create A Dynamic Memory Pointer, Called BB, That Is Of Part Type struct Part { char Name[24]; long No; }; BB 4 bytes * Part NULL &1000 main (int argc, char argv[]) { Part *BB = NULL;

Allocate A Block Of Dynamic Memory For One Part struct Part { char Name[24]; long No; }; 4 bytes * Part BB 10165528 NULL &1000 24 bytes Part Name 24 bytes?? &10165528 No 4 bytes?? main (int argc, char argv[]) { Part *BB = NULL; BB = new Part;

struct Part { char Name[24]; long No; }; Place "Basketball" In Part Name BB main (int argc, char argv[]) { Part *BB = NULL; BB = new Part; 4 bytes * Part 10165528 NULL &1000 24 bytes Part Name 24 bytes Basketball?? &10165528 strcpy_s(bb->name, "Basketball"); or strcpy_s((*bb).name, "Basketball"); No 4 bytes??

Place 10021 In Part Name struct Part { 4 bytes * Part char Name[24]; long No; BB 10165528 NULL &1000 Name 24 bytes }; Basketball main (int argc, char argv[]) &10165528 { Part *BB = NULL; BB = new Part; strcpy_s(bb->name, "Basketball"); St->No = 10021; or (*St).No = 10021; 24 bytes Part?? No 4 bytes 10021??

Display struct Part { 4 bytes * Part char BB 10165528 NULL Name[24]; long &1000 No; }; Name 24 bytes main (int argc, char argv[]) { &10165528 Part *BB = NULL; BB = new Part; strcpy_s(bb->name, "Basketball"); (*St).No = 10021; } 24 bytes Part No 4 bytes Basketball?? 10021?? printf("name.. = %s\n", BB->Name); printf("no... = %ld\n\n", (*BB).No); delete BB;

37 37

main (int argc, char argv[]) { Part *BB = NULL; BB = new Part; if (BB!= NULL) { strcpy_s(bb->name, "Basketball"); (*St).No = 10021; } Always Check Dynamic Memory Allocation! } printf("name.. = %s\n", BB->Name); printf("no... = %ld\n\n", (*BB).No); delete BB; struct Part { char Name[24]; long No; };