CSCI 350: Getting Started with C Written by: Stephen Tsung-Han Sher June 12, 2016

Similar documents
Section 7: Scheduling and Fairness

cs 140 project 1: threads 9 January 2015

Computer Science 162, Fall 2014 David Culler University of California, Berkeley Midterm 1 September 29, 2014

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

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

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

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

First of all, it is a variable, just like other variables you studied

Chapter 1 Getting Started

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

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

Procedures, Parameters, Values and Variables. Steven R. Bagley

Pintos: Threads Project. Slides by: Vijay Kumar Updated by Godmar Back Presented by: Min Li

CS2141 Software Development using C/C++ C++ Basics

COMP322 - Introduction to C++

EL6483: Brief Overview of C Programming Language

CS349/SE382 A1 C Programming Tutorial

MPATE-GE 2618: C Programming for Music Technology. Unit 4.1

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

Data Storage. August 9, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 August 9, / 19

BSM540 Basics of C Language

EMBEDDED SYSTEMS PROGRAMMING Language Basics

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Introduction to Programming Using Java (98-388)

CSC C69: OPERATING SYSTEMS

MIDTERM EXAM. CS 217 October 28, Name: Precept: Honor Code: Score: Problem Score Max

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

Compilers CS S-05 Semantic Analysis

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

Writing Functions in C

Quiz 0 Review Session. October 13th, 2014

CMPE-013/L. Introduction to C Programming

Functions in C C Programming and Software Tools

ProgOS UE. Daniel Prokesch, Denise Ratasich. basierend auf Slides von Benedikt Huber, Roland Kammerer, Bernhard Frömel

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

Midterm Exam Answers Instructor: Randy Shepherd CSCI-UA.0201 Spring 2017

PROGRAMMING IN C++ KAUSIK DATTA 18-Oct-2017

Kurt Schmidt. October 30, 2018

A Fast Review of C Essentials Part I

Programming refresher and intro to C programming

C for C++ Programmers

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

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

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

PRINCIPLES OF OPERATING SYSTEMS

Dynamic Memory Allocation and Command-line Arguments

Presented By : Gaurav Juneja

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

QUIZ. What are 3 differences between C and C++ const variables?

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Midterm Exam 2 Solutions C Programming Dr. Beeson, Spring 2009

Data Representation and Storage

a data type is Types

General Syntax. Operators. Variables. Arithmetic. Comparison. Assignment. Boolean. Types. Syntax int i; float j = 1.35; int k = (int) j;

Data Representation and Storage. Some definitions (in C)

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

Dynamic Data Structures. CSCI 112: Programming in C

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Topic 6: A Quick Intro To C

Intermediate Programming, Spring 2017*

Chapter 15 - C++ As A "Better C"

C++ Tutorial AM 225. Dan Fortunato

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

Lecture 2: C Programm

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

Laboratory work in TDDB68 Pintos Assignments 2, 3. Viacheslav Izosimov

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

edunepal_info

CSE 374 Programming Concepts & Tools

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

C++ Undefined Behavior

A Crash Course in C. Steven Reeves

CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

QUIZ. What is wrong with this code that uses default arguments?

G52CPP C++ Programming Lecture 6. Dr Jason Atkin

APT Session 4: C. Software Development Team Laurence Tratt. 1 / 14

C Formatting Guidelines

Contents of Lecture 3

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

Intermediate Programming, Spring 2017*

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

C Programming Review CSC 4320/6320

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Part V. Memory and pointers. Philip Blakely (LSC) C++ Introduction 145 / 370

Chapter IV Introduction to C for Java programmers

Pointers, Dynamic Data, and Reference Types

QUIZ. Source:

Linked lists Tutorial 5b

Short Notes of CS201

CSCI 350: Pintos Guide Written by: Stephen Tsung-Han Sher November 5, 2016

Lecture 10: Potpourri: Enum / struct / union Advanced Unix #include function pointers

Work relative to other classes

Pointers and Arrays 1

Introduction to C++ Systems Programming

More on C programming

BLM2031 Structured Programming. Zeyneb KURT

CS 318 Principles of Operating Systems

Lecture 03 Bits, Bytes and Data Types

ECE 15B COMPUTER ORGANIZATION

CS201 - Introduction to Programming Glossary By

Transcription:

CSCI 350: Getting Started with C Written by: Stephen Tsung-Han Sher June 12, 2016 Introduction As you have been informed, your work with Pintos will be almost exclusively with C. Since you have taken CSCI-103 and CSCI-104 in C++, C is not too foreign of a language for you to pick up. Nonetheless, there are still a few confusing differences when transitioning to C. This guide serves to help you understand some differences between C++ and C for you to start working on your project. Contents 1 Printing to Console 2 1.1 Simple printf......................................... 2 1.2 Advanced printf....................................... 2 2 Dynamic Memory Allocation 4 3 Structs & Classes 4 3.1 Structs............................................. 4 3.2 Classes............................................. 5 4 Use of Void & Aux for Function Parameters 6 4.1 Void Parameters........................................ 6 4.2 Auxiliary Parameters..................................... 6

1 Printing to Console The all time favorite function in C++ is std::cout; unfortunately the iostream library is unavailable for C. In C, you will need to use printf to print to the console. printf may be a bit confusing at first, but after a few practices you ll understand it without problem. 1.1 Simple printf If you want to just print out text, simply call the printf function with a string as the parameter: Source: // stdio.h for printf #include <stdio.h> printf("hello World\n"); Output: ~$ Hello World Note that endl is no longer available, so you ll need to make sure you use \n for an endline operation. 1.2 Advanced printf Suppose you want to add in numbers, floats, pointers and whatnot, in C++ you will do something similar to: Source: #include <iostream> float foo = 3.50; int* fooptr = &foo; std::cout << "Hello World " << foo << " " << fooptr << std::endl; Output: ~$ Hello World 3.50 0x08048000 2

In C you will not be able to concatenate console outputs this easily. Instead you ll need to use a %specifier in your string and the specific variable you want to output as latter arguments in respective order. Formally the %specifier prototype is: %[flags][width][.precision][length]specifier However you usually won t need to worry about the flags, width, precision, nor length. Here is a short example: Source: // stdio.h for printf #include <stdio.h> float foo = 3.50; int* fooptr = &foo; printf("hello World %f %p", bar, fooptr); Output: ~$ Hello World 3.50 0x08048000 Note that the order of the arguments after the string must match the order of your %specifiers. If you don t you ll get a compiler errors regarding some kind of mismatched variable type. Here s a list of the specifiers you ll most likely use in Pintos, a full list can be found at cplusplus.com. Specifier Output Example d or i Signed decimal integer 350 x Unsigned hexadecimal decimal 15e f Decimal floating point 3.50 s String of characters CSCI350 p Pointer address 0x08048000 3

2 Dynamic Memory Allocation You will not be able to use the keyword new; instead you will need to use malloc. malloc takes in the number of bytes of memory you want to request and returns you a pointer to the beginning of the section of memory you requested. malloc is often used in conjunction with sizeof, which returns the number of bytes of the data type you pass in as the argument: struct mystruct* foo = malloc(sizeof(struct mystruct)); And instead of delete, you use free. Simply pass in the pointer of the block of memory you wish to free. //Memory allocate struct mystruct* foo = malloc(sizeof(struct mystruct)); //Free the memory allocation free(foo); To use malloc, make sure to #include "threads/malloc.h". 3 Structs & Classes Structs and classes are paramount to object-oriented programming languages such as C and C++. The key difference between C++ and C is that classes are not supported by C. 3.1 Structs Structs work effectively the exact same way in C as it does in C++ except one syntactical difference. Consider the following struct (which is a struct you will get intimately familiar with in Pintos): struct thread /* Owned by thread.c. */ tid_t tid; /* Thread identifier. */ enum thread_status status; /* Thread state. */ char name[16]; /* Name (for debugging purposes). */ uint8_t *stack; /* Saved stack pointer. */ int priority; /* Priority. */ struct list_elem allelem; /* List element for all threads list. */ /* Shared between thread.c and synch.c. */ struct list_elem elem; /* List element. */ #ifdef USERPROG /* Owned by userprog/process.c. */ uint32_t *pagedir; /* Page directory. */ #endif 4

; /* Owned by thread.c. */ unsigned magic; /* Detects stack overflow. */ In C, whenever you want to use the thread struct, you will need to prepend the word thread with the word struct to indicate that it is a struct: #include "threads/threads.h" #include "threads/malloc.h" #include <list.h> // Defining a function that takes a struct as a parameter void threadfunction(struct thread mythread, struct thread* threadptr) // do something with mythread //Normal instantiation of a struct struct thread mythread; //Dynamic instantiation of a struct struct thread* newthread = malloc(sizeof(struct thread)); //Passing a struct as an argument threadfunction(mythread, newthread); It is also important to note that the order in which you define the members of a struct is the order in which the compiler will place these members on the memory. This is particularly important for the unsigned magic member at the bottom of the thread struct definition. The magic member is used to determine the boundaries of the struct, thus if you define members below the definition of magic, you will get a lot of page faults that you will not be happy with. tl;dr: Do NOT define anything below unsigned magic. 3.2 Classes As mentioned before, classes are not supported in C. Thus if you use a function associated with a struct, this function is called from a global scope. One quick workaround is to pass in the pointer of the struct you wish for the function to act on: struct MyStruct int member; void MyStruct_SetMember(struct MyStruct* ms, int m) ms->member = m; 5

Since the function exist in a global scope, it is usually a good practice to prepend the name of the struct on the function name in order to indicate which struct this function intends to act upon. For example, look at the list implementation under src/lib/kernel/list.h; every function acts upon either the list struct itself or on one of the list s elements, all taken in as pointers. also note how every function begin with list_. /* List traversal. */ struct list_elem *list_begin (struct list *); struct list_elem *list_next (struct list_elem *); struct list_elem *list_end (struct list *); struct list_elem *list_rbegin (struct list *); struct list_elem *list_prev (struct list_elem *); struct list_elem *list_rend (struct list *); struct list_elem *list_head (struct list *); struct list_elem *list_tail (struct list *); 4 Use of Void & Aux for Function Parameters 4.1 Void Parameters In C++, if you have a function with no arguments, you simply omit any declaration of arguments: void MyFunction(); In C, you ll have to indicate that a function s arguments are void: //Declaration void MyFunction(void); //Usage MyFunction(); Defining a function with no arguments indicates that this function takes an unspecified number of arguments, which can produce unpredictable behavior. 4.2 Auxiliary Parameters In some functions you will use in Pintos, you will notice a void* aux as a parameter. //In src/lib/kernel/list.h struct list_elem *list_max (struct list *, list_less_func *, void *aux); struct list_elem *list_min (struct list *, list_less_func *, void *aux); 6

The aux parameter is an extra parameter in which its usage varies depending on the function. The behavior of this extra parameter is usually defined in the function s documentation. If you do not want to use this extra parameter, you can pass in a NULL, which is usually the case. 7