ELEC / COMP 177 Fall Some slides from Kurose and Ross, Computer Networking, 5 th Edition

Similar documents
Computer Systems and Networks. ECPE 170 Jeff Shafer University of the Pacific. C Programming

Computer Systems and Networks. ECPE 170 University of the Pacific

Computer Systems and Networks

Computer Systems and Networks

CCE1111 Programming for Engineers [C Programming Language]

C mini reference. 5 Binary numbers 12

In Java we have the keyword null, which is the value of an uninitialized reference type

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Computer Systems and Networks

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

C Characters and Strings

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

C for C++ Programmers

CS 222: Pointers and Manual Memory Management

Lecture 17. Strings II. CptS 121 Summer 2016 Armen Abnousi

o Code, executable, and process o Main memory vs. virtual memory

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

COMP1917: 09 Arrays and Strings

Memory Management. CSC215 Lecture

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013

ECE551 Midterm Version 2

today cs3157-fall2002-sklar-lect05 1

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

211: Computer Architecture Summer 2016

Object-Oriented Programming

Dynamic memory. EECS 211 Winter 2019

Memory Allocation in C C Programming and Software Tools. N.C. State Department of Computer Science

C Program Development and Debugging under Unix SEEM 3460

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

Chapter 8: Character & String. In this chapter, you ll learn about;

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

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

CS 103 Lab 6 - Party Like A Char Star

Chapter 9 Strings. With this array declaration: char s[10];

8. Characters, Strings and Files

Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters,

Homework #3 CS2255 Fall 2012

Create a Program in C (Last Class)

CS 103 Lab - Party Like A Char Star

C Programming Basics II

Quiz 0 Review Session. October 13th, 2014

Dynamic memory allocation (malloc)

C++ Arrays. Arrays: The Basics. Areas for Discussion. Arrays: The Basics Strings and Arrays of Characters Array Parameters

EE 355 Lab 4 - Party Like A Char Star

Class Information ANNOUCEMENTS

Memory Allocation. General Questions

CS242 COMPUTER PROGRAMMING

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

Exercise 1.1 Hello world

Computer Security. Robust and secure programming in C. Marius Minea. 12 October 2017

Programming. Pointers, Multi-dimensional Arrays and Memory Management

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

Dynamic Memory CMSC 104 Spring 2014, Section 02, Lecture 24 Jason Tang

This code has a bug that allows a hacker to take control of its execution and run evilfunc().

ONE DIMENSIONAL ARRAYS

CSE 12 Spring 2016 Week One, Lecture Two

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

COSC Software Engineering. Lecture 16: Managing Memory Managers

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Course organization. Course introduction ( Week 1)

Should you know scanf and printf?

Basic Buffer Overflows

CSC 1600 Memory Layout for Unix Processes"

Dynamic Memory. Dynamic Memory Allocation Strings. September 18, 2017 Hassan Khosravi / Geoffrey Tien 1

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

Getting Started. Project 1

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

ECE551 Midterm Version 1

CSE2301. Dynamic memory Allocation. malloc() Dynamic Memory Allocation and Structs

Strings. Steven R. Bagley

CSE 509: Computer Security

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Compiling and Running a C Program in Unix

Language Design COMS W4115. Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science

Characters, c-strings, and the string Class. CS 1: Problem Solving & Program Design Using C++

Intermediate Programming, Spring 2017*

CSCE150A. Introduction. Basics. String Library. Substrings. Line Scanning. Sorting. Command Line Arguments. Misc CSCE150A. Introduction.

ECS 153 Discussion Section. April 6, 2015

ch = argv[i][++j]; /* why does ++j but j++ does not? */

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 9. Strings. Notes. Notes. Notes. Lecture 07 - Strings

Dynamic Allocation in C

Undefined Behaviour in C

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

Computer Science & Engineering 150A Problem Solving Using Computers

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso

Lecture 8 Dynamic Memory Allocation

Hacking in C. Pointers. Radboud University, Nijmegen, The Netherlands. Spring 2019

Approximately a Test II CPSC 206

Computer Programming: Skills & Concepts (CP) Strings

C Syntax Arrays and Loops Math Strings Structures Pointers File I/O. Final Review CS Prof. Jonathan Ventura. Prof. Jonathan Ventura Final Review

Pointers, Arrays, and Strings. CS449 Spring 2016

CS201 Some Important Definitions

Chapter 8 C Characters and Strings

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

Fundamentals of Programming

TI2725-C, C programming lab, course

CSC231 C Tutorial Fall 2018 Introduction to C

Transcription:

ELEC / COMP 177 Fall 2012 Some slides from Kurose and Ross, Computer Networking, 5 th Edition

Prior experience in programming languages C++ programming? Java programming? C programming? Other languages? Prior experience in Linux / FreeBSD /Unix Yes? No?

For C++ Programmers

No classes / object- oriented programming No new / delete No stream operators (<< and >>), cin, cout, No C++ Standard Libraries (e.g. iostream) bool keyword Added back in C99 standard #include <stdbool.h> Declare variables anywhere inside function Added back in C99 standard

printf("this is a string\n"); printf("the integer is %i\n", num); printf("the floating-point values are %g and %g\n", num1, num2); Lots of formatting options # of digits before / after decimal point? Pad with zeros?

d or i u x f or g c s Format Type Code Corresponding Variable Type int (interpret as signed 2 s comp) int (interpret as unsigned) int (print as hexadecimal) float/double char string (null- terminated array of chars) Prefix with l or ll (i.e. long or long long for larger 64- bit data types)

Input from console scanf("%d %c", &myint, &mychar) Requires the address of the destination variable Use the & operator to obtain address char myarray[256]; scanf("%255s", myarray); Note here that myarray is already a pointer

Man(ual) pages exist for common programming functions too unix> man printf unix> man scanf Or just google

struct database { int id_number; int age; float salary; }; int main() { database employee; employee.age = 22; employee.id_number = 1; employee.salary = 12000.21; }

There is no such thing as a string in C! What do you get? An array of characters Terminated by the null character '\0' Must manipulate element by element Not enough room in the array? Need a bigger array

char phrase[]="math"; phrase phrase[0] phrase[1] phrase[2] phrase[3] phrase[4] M A T H \0 Null terminator character (End of string)

phrase char phrase[8]="math"; phrase[0] phrase[1] phrase[2] phrase[3] phrase[4] phrase[5] phrase[6] phrase[7] M A T H \0????????? printf("%s\n", phrase); Prints until it reaches the \0 character!

#include <string.h> Useful functions strcpy - String copy strcmp - String compare strlen - String length strcat - String concatenate

phrase1 char phrase1[] = "Math"; char phrase2[8]; strcpy(phrase2, phrase1); phrase1[0] phrase1[1] phrase1[2] phrase1[3] phrase1[4] M A T H \0 phrase 2 phrase2[0] phrase2[1] phrase2[2] phrase2[3] phrase2[4] phrase2[5] phrase2[6] phrase2[7] M A T H \0?????????

phrase1 char phrase1[8] = Comp ; char phrase2[] = Sci ; strcat(phrase1, phrase2); phrase1[0] phrase1[1] phrase1[2] phrase1[3] phrase1[4] phrase1[5] phrase1[6] phrase1[7] C O M P S C I \0 phrase 2 phrase2[0] phrase2[1] phrase2[2] phrase2[3] S C I \0 You cannot do this: phrase2= phrase1+phrase2;

Useful for character manipulation #include <ctype.h> toupper(char) / tolower(char) Converts character to uppercase or lowercase Example: char c = toupper('a'); printf("%c", c); // A

isalpha(char) Is the character a letter? isdigit(char) Is the character a number 0-9? isspace(char) Is the character whitespace? (space or newline character) ispunct(char) Is the character punctuation? (technically, a visible character that is not whitespace, a letter, or a number) and several other variations

#include <stdlib.h> void * malloc(int size) Allocate region in memory (aka new ) Argument: Size of region in bytes to allocate Return value: Pointer to the region void free(void * ptr) De- allocate region in memory (aka delete ) Argument: Pointer to the region

void * calloc(int count, int size) Basically the same as malloc! Imagine you want an array of elements Argument 1: # of elements to allocate Argument 2: Size of each element in bytes Return value: Pointer to the region

void * realloc(void *ptr, int size); Resize a dynamic region of memory Note that it might move to a new address! Argument: Pointer to the original region Argument 2: Desired size in bytes of new region Return value: Pointer to the new region It might be at the same address if you made it smaller It might be at a new address if you made it larger

Who implemented malloc()? C Standard Library: #include <stdlib.h> There are different C Standard Library implementations! Android: Bionic Apple: BSD- based / Proprietary Microsoft: Proprietary C Runtime Library Linux: GNU C Library (glibc) http://www.gnu.org/software/libc/

Where does the malloc() memory come from? The Heap: A region of memory for dynamic memory allocation Per- process each program gets its own heap Managed by malloc() and related functions Different from the stack, which is for static variables (known at compile- time)

Why do we need to call free() after calling malloc()? Memory leak Malloc() cannot re- use that space ever, because its internal bookkeeping still thinks that region is used Will only be recovered upon terminating program Operating system wipes out all the memory allocated to your process (stack, heap, etc )

What is a buffer overflow bug? char buf1[8]; char buf2[8]; strcat(buf1, excessive ); End up overwriting two characters beyond buf1!

Why is a buffer overflow bug dangerous? What is beyond my buffer in memory? Other variables and data? (probably buf2) The stack? (further out) The return address to jump to after my function finishes? If app is running as administrator, attacker now has full access!

Limitless opportunities in C for errors regarding memory L Forgetting to free() some dynamic memory Trying to free() dynamic memory more than once Losing a pointer to dynamic memory (memory is lost ) Accessing array elements past the end of the array Mis- calculating array pointers that miss their desired target Suggestion: Learn Valgrind to analyze your program and detect / trace errors See link on class Resources page

char *a = malloc(128*sizeof(char)); char *b = malloc(128*sizeof(char)); b = a; free(a); free(b); http://www.yolinux.com/tutorials/c++memorycorruptionandmemoryleaks.html

char *a = malloc(128*sizeof(char)); datalen = <some value...> // Copy datalen bytes // starting at *data to *a memcpy(a, data, datalen); http://www.yolinux.com/tutorials/c++memorycorruptionandmemoryleaks.html

ptr = (char *) malloc(strlen(string_a)); strcpy(ptr, string_a); http://www.yolinux.com/tutorials/c++memorycorruptionandmemoryleaks.html

int *get_ii() { int ii = 2; // Local stack variable return &ii; } main() { int *ii; ii = get_ii();... Do stuff using ii pointer } http://www.yolinux.com/tutorials/c++memorycorruptionandmemoryleaks.html

http://xkcd.com/371/

What s a NULL pointer? Pointer value is 0x000000000 Meaning is that the pointer is not pointing anywhere What happens if you deference a NULL pointer? Telling the computer to read from (or write) to the value stored in the pointer, which is 0x000000000 Behavior undefined and generally unpleasant on various computer systems

Segfault = Segmentation Fault Your program tried to read or write a virtual memory address that is not allowed Tried to read memory outside of program bounds? Tried to write read- only memory regions? (used for program data)

Segmentation was the name of an old system (back before Intel 386 processors) used to divide physical computer memory into many virtual address regions, one per application process The Segfault name stuck even though we now use paging to manage virtual memory

Discuss assignment What are we doing? What are the requirements? Discuss possible search algorithm ( pivot point ) Where to get programming help? Class resources page: http://ecs- network.serv.pacific.edu/ecpe- 177/resources Friends Honor code: Homework is done individually Allowed: Planning solution strategies, helping each other to debug programs Not allowed: Copying code from classmates

See Handout