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

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

a data type is Types

Motivation was to facilitate development of systems software, especially OS development.

Motivation was to facilitate development of systems software, especially OS development.

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Programming in C and C++

Lecture 3: C Programm

Programming in C and C++

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

Programming. Data Structure

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

Structure of this course. C and C++ Past Exam Questions. Text books

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Work relative to other classes

Object-Oriented Programming

CMPE-013/L. Introduction to C Programming

PRINCIPLES OF OPERATING SYSTEMS

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers

Kurt Schmidt. October 30, 2018

CSCI 171 Chapter Outlines

Introduction to C. Ayush Dubey. Cornell CS 4411, August 31, Geared toward programmers

introduction week 1 Ritsumeikan University College of Information Science and Engineering Ian Piumarta 1 / 20 imperative programming about the course

Final CSE 131B Spring 2004

EC 413 Computer Organization

Introduction to C. Zhiyuan Teo. Cornell CS 4411, August 26, Geared toward programmers

(heavily based on last year s notes (Andrew Moore) with thanks to Alastair R. Beresford. 1. Types Variables Expressions & Statements 2/23

CSE 303 Lecture 8. Intro to C programming

Programming in C. What is C?... What is C?

Types, Variables, and Constants

Lectures 5-6: Introduction to C

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Arrays and Pointers. CSC209: Software Tools and Systems Programming (Winter 2019) Furkan Alaca & Paul Vrbik. University of Toronto Mississauga

CS 61C: Great Ideas in Computer Architecture Introduction to C

Short Notes of CS201

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Topic 6: A Quick Intro To C

CS201 - Introduction to Programming Glossary By

CS133 C Programming. Instructor: Jialiang Lu Office: Information Center 703

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Introduction to C. Systems Programming Concepts

CS240: Programming in C

CSE 374 Programming Concepts & Tools

Class Information ANNOUCEMENTS

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

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

EL2310 Scientific Programming

Introduction to C An overview of the programming language C, syntax, data types and input/output

SWEN-250 Personal SE. Introduction to C

EL2310 Scientific Programming

Welcome to... CS113: Introduction to C

C - Basics, Bitwise Operator. Zhaoguo Wang

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

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

edunepal_info

TDDB68 Concurrent Programming and Operating Systems. Lecture 2: Introduction to C programming

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

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

Lecture 2: C Programm

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

CSC 1600 Memory Layout for Unix Processes"

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

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

Computer Programming

CS24 Week 2 Lecture 1

CS201 - Lecture 1 The C Programming Language

ECEN 449 Microprocessor System Design. Review of C Programming

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

Page 1. Agenda. Programming Languages. C Compilation Process

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

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Presented By : Gaurav Juneja

C Programming Review CSC 4320/6320

XSEDE Scholars Program Introduction to C Programming. John Lockman III June 7 th, 2012

Computers Programming Course 5. Iulian Năstac

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

PROGRAMMAZIONE I A.A. 2017/2018

COMP1917: Computing 1 1. Introduction

CSC231 C Tutorial Fall 2018 Introduction to C

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

COMP1917: Computing 1 1. Introduction

C Language, Token, Keywords, Constant, variable

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

CS 61c: Great Ideas in Computer Architecture

CS 261 Fall Mike Lam, Professor. Structs and I/O

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

A brief introduction to C programming for Java programmers

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

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Lectures 5-6: Introduction to C

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

1. Introduction. Course Web Site. COMP1917: Computing 1. Textbook. Occupational Health and Safety (OHS)

CS 241 Data Organization Pointers and Arrays

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Room 3P16 Telephone: extension ~irjohnson/uqc146s1.html

Transcription:

CS 261 Fall 2018 Mike Lam, Professor C Introduction Comparison w/ Java, Memory Model, and Pointers Please go to socrative.com on your phone or laptop, choose student login and join room LAMJMU

The C Language Systems language originally developed for Unix Imperative, compiled language with static typing High level at the time; now considered low-level Allows direct access to memory (subject to architecture) Many compilers and standards: we ll use GNU and C99 Ken Thompson (inventor of B language and Unix) Dennis Ritchie (inventor of C language and coauthor of C book) Brian Kernighan (coauthor of C book and contributor to Unix/C)

Review: Compilation usually combined

Review: Makefiles The compilation process is usually streamlined using a build system (we'll use Make) Provide a Makefile that contains targets, dependencies, and build commands Example Makefile: target dependency hello: hello.c gcc -g -O0 -o hello hello.c build command

Hello, World How is this different from Java? #include <stdio.h> int main() { printf("hello, world!\n"); return 0; }

Similarities to Java Semicolons! Comments (both // and /* */ styles) Basic types: int, char, float, double Char is just a number Blocks w/ curly braces Loops: do, while, for Switch statements Parameter must be integer Function definitions

Differences from Java Preprocessor macros (#include, #define) Interface (.h) vs implementation (.c) Functions must be declared before use New distinction: declaration vs. definition Booleans are bool (not built-in; must include stdbool.h) Actually integers: 0 is false, anything else is true No built-in string type (C strings are just arrays of chars) No classes, packages, or built-in exceptions Different I/O functions: printf, fgets, scanf (in stdio.h) For printf, embed variables in output using formatting codes E.g., use "%d" to embed an integer (see documentation for more codes)

Variables in C Declared by type and name like in Java Can be initialized when declared (this is recommended!) E.g., int file_counter = 0; If not initialized, contents are undefined until assigned Can be declared const Read-only, similar to final in Java must be initialized! Multiple declarations per line are allowed E.g., int x, y; E.g., int x = 0, y = 1; Mixed-initialization and multiple declarations is not recommended E.g., int x, y = 1; // only initializes y!

C data types Integer types: char and int Can be signed (default) or unsigned short, long, and long long modifiers for int Real types: float and double Floating-point representation Data type Size on stu (bytes) char / bool 1 1 byte = 8 bits short int 2 int 4 long int / long long int 8 float 4 double 8

Explicit-width integer types C standard doesn't mandate integer widths It only specifies a minimum This causes problems when different architectures or compilers provide different actual sizes More portable alternative: stdint.h types Basic format: XintY_t X can be empty (signed) or 'u' (unsigned) Y can be 8, 16, 32, or 64 (bits) Examples: int8_t, uint8_t, int32_t, uint64_t

Variable attributes (CS 430 preview) Name Value Type Address Scope Lifetime

Variable attributes (CS 430 preview) Name: identifier used to refer to the variable in code Value: current contents of a variable Type: range of values a variable can store Address: location of variable s value Most common locations: register, stack, heap, or static data We ll focus on the non-register locations for now Scope: code range where a variable is visible Global: visible anywhere in file (code module) Local: visible only inside a function or block Lifetime: time period when variable access is valid Static: allocated when program starts; de-allocated on exit Dynamic: allocated and de-allocated while program runs

C/Linux memory model Every process has its own virtual private memory called an address space. (randomized) 7fff00000000 local variables stack The address space is divided into regions. Some regions are static and do not change size while the process runs, while others are dynamic, changing size if necessary. Some regions begin at a randomized location (different on every run) for security reasons. The stack region expands when a function is called and shrinks when a function returns. The heap region expands when malloc() is called. dynamicallyallocated memory (randomized) 1000000 global variables & 'static' local variables 601000 400000 0 heap static data static code

Memory management The fundamental difference between C and Java is how they handle memory Java is a managed language, where the compiler and runtime handle memory management for the programmer and direct access to memory is difficult or impossible C is not a managed language, meaning we can directly access and manipulate memory using arbitrary addresses This makes it possible to do the kind of low-level experimentation we want to do in CS 261, and it also enables optimizations that are not possible using Java However, it is also far more dangerous! With great power comes great responsibility.

Pointers A pointer is a variable that contains a memory address Type modifier: * indicates one level of pointer int *p; int **p; // yes, this works Often initialized using the & operator ( address of ) int x; p = &x; Dereferenced with * operator ( follow the pointer ) *p = 7; Set a pointer to NULL to mark them as invalid C does NOT check pointers before dereferencing them! int *p = NULL; *p = 123; // this will segfault!

What will this C code print? int a = 42; int b = 7; int c = 999; int *t = &a; int *u = NULL; printf("%d %d\n", a, *t); Draw a picture of memory! Use arrows for pointers. c = b; u = t; printf("%d %d\n", c, *u); a = 8; b = 9; printf("%d %d %d %d\n", b, c, *t, *u); *t = 123; printf("%d %d %d %d %d\n", a, b, c, *t, *u);

Pointer declaration caveat The following code doesn't do what you think it does: int* c, d; Recommendation: put asterisk next to variable names in declarations int *c, *d; Exception: function declarations (since there can only be one return value) int* myfunc();

Types Pointers are variables, so they have a type The type describes what kind of data it points to An int has type int A pointer to an int has type int* A pointer to a pointer to an int has type int** Expressions also have a type If x has type int, then x+4 also has type int If x has type int, then &x has type int* If p has type int*, then *p has type int If p has type int*, then &p has type int**

Dynamic memory allocation If you do not know how much memory you need until after the program is running, you must allocate memory on the heap Allocate with malloc() function Pass it the number of bytes you need Often calculated using the sizeof operator Returns a pointer to the beginning of the allocated region De-allocate with free() when you are done Pass it a pointer to the beginning of the region you want to free Good code practice: set pointer to NULL afterwards Neglecting to free memory will result in a memory leak

C/Linux memory model int global_var; void foo() { static int foo_st_var; int foo_var; } For each of the following variables, classify them as static, stack, or heap: global_var foo_st_var foo_var main_var malloc_var *malloc_var int main() { } int main_var; int *malloc_var = (int*)malloc(sizeof(int)); foo(); return 0; Does this program leak memory? If so, where, and how would you fix it?

Variable summary Global variables Static data address, global scope, static lifetime Local variables (regular) Stack address, local scope, dynamic lifetime Valid while the function executes Local variables declared static Static data address, local scope, static lifetime Similar to global variable but with local scope Dynamically-allocated memory Heap address, local scope (via pointer), dynamic lifetime Valid from malloc until free Pointer(s) themselves are usually local variables (see above)