CS C Primer. Tyler Szepesi. January 16, 2013

Similar documents
A Brief C Primer for CS 350

A Brief C Primer for CS 350

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

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

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

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.

Memory, Data, & Addressing II CSE 351 Spring

Understanding Pointers

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

Data Representation and Storage. Some definitions (in C)

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

High Performance Programming Programming in C part 1

CS61, Fall 2012 Section 2 Notes

Hardware: Logical View

Final CSE 131B Spring 2005

Low-Level C Programming. Memory map Pointers Arrays Structures

The output: The address of i is 0xbf85416c. The address of main is 0x80483e4. arrays.c. 1 #include <stdio.h> 3 int main(int argc, char **argv) 4 {

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

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

Kurt Schmidt. October 30, 2018

Processes. Johan Montelius KTH

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

Class Information ANNOUCEMENTS

CS24 Week 2 Lecture 1

Final CSE 131B Spring 2004

A process. the stack

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

Lecture 8 Dynamic Memory Allocation

PRINCIPLES OF OPERATING SYSTEMS

CS 241 Data Organization Pointers and Arrays

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

Systems Programming and Computer Architecture ( )

CS360 Midterm 1 - February 21, James S. Plank. Put all answers on the answer sheet. In all of these questions, please assume the following:

Intermediate Programming, Spring 2017*

211: Computer Architecture Summer 2016

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

CS240: Programming in C

Lecture 3: C Programm

Final Exam. 11 May 2018, 120 minutes, 26 questions, 100 points

Today s Learning Objectives

211: Computer Architecture Summer 2016

Do not start the test until instructed to do so!

ECE 15B COMPUTER ORGANIZATION

DECLARAING AND INITIALIZING POINTERS

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Course organization. Course introduction ( Week 1)

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

Dynamic memory allocation (malloc)

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

Midterm Exam Review Slides

FUNCTIONS POINTERS. Pointers. Functions

Dynamic Memory Allocation and Command-line Arguments

Computer Organization & Systems Exam I Example Questions

Arrays and Pointers in C. Alan L. Cox

Problem 2 Add the two 2 s complement signed 8-bit values given below, and express your answer in decimal.

Arrays and Pointers. CSE 2031 Fall November 11, 2013

CS 322 Operating Systems Practice Midterm Questions

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

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

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

ECEN 449 Microprocessor System Design. Review of C Programming

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

CS 61c: Great Ideas in Computer Architecture

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Lecture 2: C Programm

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

Memory management. Johan Montelius KTH

C - Func(on, Pointer, Array. Zhaoguo Wang

Survey. Motivation 29.5 / 40 class is required

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

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

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

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

Pointers and File Handling

We would like to find the value of the right-hand side of this equation. We know that

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

CS102 Software Engineering Principles

Pointers, Dynamic Data, and Reference Types

Variables and literals

CS429: Computer Organization and Architecture

POINTER AND ARRAY SUNU WIBIRAMA

Programming in C - Part 2

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

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

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

CSE 351: The Hardware/Software Interface. Section 2 Integer representations, two s complement, and bitwise operators

CPSC 213. Introduction to Computer Systems. Procedures and the Stack. Unit 1e

Practical Malware Analysis

Operating systems. Lecture 7

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

Lecture 08 Control-flow Hijacking Defenses

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

15-213/18-243, Fall 2010 Exam 1 - Version A

EM108 Software Development for Engineers

Byte Ordering. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Final Exam. 12 December 2018, 120 minutes, 26 questions, 100 points

Page 1. Where Have We Been? Chapter 2 Representing and Manipulating Information. Why Don t Computers Use Base 10?

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

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

Transcription:

January 16, 2013

Topics 1 Why C? 2 Data Types 3 Memory 4 Files 5 Endianness 6 Resources

Why C? C is exteremely flexible and gives control to the programmer Allows users to break rigid rules, which are enforced by other languages (Java, C++, Scheme) C exposes the programmer to the underlying memory management C is lightweight, lots of external features Barebones functionality builtin

Data Types A data type characterizes the size and operations that are permitted on the data Also defines how data is stored in memory We are using a 32-bit MIPS machine unsigned/signed char = 1 byte unsigned/signed int = 4 bytes unsigned/signed long = 4 bytes unsigned/signed long long = 8 bytes All pointers = 4 bytes

Structs A Struct is a aggregation of other data types Also called a record type Its size depends on its member types Struct members are accessed with the. operator

Structs Example: struct foo_struct { unsigned int x; char a; char b; char c; char d; ; sizeof ( unsigned int ) = 4 sizeof ( struct foo_struct ) = 8 struct foo_struct inst ; inst.x = 1; inst.a = 2;

Structs Structs are usually layed out in the way they are defined Previous example First 4 bytes are reserved for the int, and then 4 * 1 byte for the chars The first byte of data will belong to the int

Pointers * C allows the programmer to manipulate memory locations directly For declaration, use the dereference operator * int *ptr; A pointer is a variable which holds the address of a memory location The data type of the memory location is inferred by the pointer type (ie.: a pointer to an integer)

Pointers Usually a pointer is assigned the address of a variable Easily done with the reference operator & int somevar = 1; int * ptr = & somevar ; Can also use casts to create custom pointers int * ptr2 = ( int *) (0 xffff0000 ); unsigned long var = 0 xffff0000 ; int * ptr2 = ( int *) var ;

Using Pointers The contents of a pointer is an address printf (" ->0x%x", ptr ); - >0 xff001200 To access the content of the location use the dereference operator * printf (" ->%d", * ptr ); ->1

Pointers and Functions To be able to use pass-by-reference semantics we must pass a pointer void foo (int *ptr, char *cptr); foo can now change the value of the variables which ptr and cptr reference int foo ( int * ptr, char * cptr ) { * ptr = 1; * cptr = a ; return 10; int a = 0; b = 0; char c = ; printf ("%d,%d,%c",b,a,c); b = foo (&a, &c); printf ("%d,%d,%c",b,a,c); =>0, 0, = >10,1,a

Arrays Arrays are a collection of elements of 1 data type int a[10]; This reserves an array of size 10 What is the size of this in bytes (on MIPS)? struct foo struct[10]; Much like the struct, each element is layed out contiguously in memory if & a [3] = 0x10, then & a [4] = 0x14, etc

Pointers and Arrays In C, arrays are managed with pointer arithmetic The name given to an array, is in reality a pointer The [] operator performs the following operation: int a [10]; a [4] == *( int )(&a[0] + 4 * sizeof ( int )) & a [0] == a // a is in reality of type ( int *) and contains the address of the first element

Pointers Example # include <stdio.h> # include < stdlib.h> struct foo_struct { unsigned int x; char a; char b; char c; char d; ; int main () { int i; char a [40]; unsigned int * iptr = ( unsigned int *) a; struct foo_struct * sptr = ( struct foo_struct *) a;

Pointers Example for (i =0; i <40; i ++) { a[i] = ( char ) i; for (i =0; i <10; i ++) { printf ("%2d = 0x %08 x\n", i, iptr [i]); printf ("x = 0x %08 x a = %d b = %d c = %d d = %d\n", sptr [0].x, ( int ) sptr [0].a, ( int ) sptr [0].b, ( int ) sptr [0].c, ( int ) sptr [0]. d); exit (0) ;

Pointers Answer 0 = 0 x00010203 1 = 0 x04050607 2 = 0 x08090a0b 3 = 0 x0c0d0e0f 4 = 0 x10111213 5 = 0 x14151617 6 = 0 x18191a1b 7 = 0 x1c1d1e1f 8 = 0 x20212223 9 = 0 x24252627 x = 0 x00010203 a = 4 b = 5 c = 6 d = 7

Strings C strings are arrays of type char with a trailing \0 character char str [12] = " Hello World "; { H, e, l, l, o,, W, o, r, l, d, \0 str [4] = o, str [11] = \0 ; printf ("%c", * str ) // => H ( Why?) You can create constant strings like this: char * cstr = " Some String "; GCC will create the string as a constant in.bss

Strings Example # include <stdio.h> # include < stdlib.h> static char * alpha = " abcdefghijklmnopqrstuvwxyz "; int main () { char array [12]; char * value = 0; int i; for (i =0; i <12; i ++) { array [i] = alpha [i];

Strings Example printf (" addr of array = %p\n", & array ); printf (" addr of array [0] = %p\n", & array [0]) ; printf ("* array = %c\n", * array ); printf (" addr of value = %p\n", & value ); printf (" addr of value [0] = %p\n", & value [0]) ; printf (" value = %p\n", value ); printf ("\n"); value = array ; printf (" addr of value = %p\n", & value ); printf (" addr of value [0] = %p\n", & value [0]) ; printf (" value = %p\n", value ); printf ("* value = %c\n", * value ); printf ("\n");

Strings Example value = & array [4]; printf (" addr of value = %p\n", & value ); printf (" addr of value [0] = %p\n", & value [0]) ; printf (" value = %p\n", value ); printf ("* value = %c\n", * value ); printf ("\n"); exit (0) ;

Strings Answer addr of array = 0 x7fffff80 addr of array [0] = 0 x7fffff80 * array = a addr of value = 0 x7fffff7c addr of value [0] = 0x0 value = 0x0 addr of value = 0 x7fffff7c addr of value [0] = 0 x7fffff80 value = 0 x7fffff80 * value = a addr of value = 0 x7fffff7c addr of value [0] = 0 x7fffff84 value = 0 x7fffff84 * value = e

Structs and Pointers A pointer can point to a struct struct foo_struct * sptr ; When accessing members of a struct via a pointer use either: (* sptr ). a // The brackets are required,. has a higher precedence then * operator sptr - >a // This is the shortcut for (* sptr ). a Note that you first dereference the struct and then access the member

Volatile Variables Declaring a variable volatile will disable value based optimizations by the compiler ie.: Dead Code elimination or value folding If variables memory is changed without compiler knowledge, we must use volatile Changing the value in another thread Hardware changing mapped variables

Volatile Variables int main ( int argc, char ** argv ) { int test ; for (i =0;i <10; i +=1) { test += 1; return test ; int main ( int argc, char ** argv ) { return 45; int main ( int argc, char ** argv ) { volatile int test ; for (i =0;i <10; i +=1) { test += 1; return test ;

Volatile Variables /* foo is memory mapped */ unsigned int foo int main ( int argc, char ** argv ) { foo = 0; while ( foo!= 255) ; /* foo is memory mapped */ unsigned int foo ; int main ( int argc, char ** argv ) { while ( true ); /* foo is memory mapped */ volatile unsigned int foo ; int main ( int argc, char ** argv ) { foo = 0; while ( foo!= 255) ;

Memory A program usually touches 3 sections fo memory Stack Local variables, determined at compile time Heap Dynamically allocated memory Global Data Constant values from the binary

Memory Stack grows down on MIPS As new stack frames are created, the stack pointer is moved down It is possible to run out of stack space As stack frames become obsolete, the stack is unwound (space is reclaimed) Heaps are managed by the OS

Stack Memory To use stack memory, allocate local variables or pass arguments void foo ( int b) { int a; The variable a and b is placed on the stack once foo is called Be careful though! Using addresses of local variables after the function exits is WRONG! Recall CS 241 stack frame setups

Dynamic Memory To use memory on the heap, a memory region has to be allocated via malloc void * malloc ( size_t s); char * str = ( char *) malloc ( sizeof ( char ) * 21) ; Storage allocated via malloc has to be freed once done using it free ( str ); This is important, as this memory region is otherwise claimed until the next restart

Stack Memory Example: int * foo () { int var = 3; return & var ; /* At this point the stack region will be unwound */ /* The address of var is no longer valid! */ void main () { int *a; a = foo (); printf ("% d", * a); /* This will produce erratic results */

Stack Memory Example: int * foo () { int var = 3; int * tmp = ( int *) malloc ( sizeof ( int )); * tmp = var ; return tmp ; void main () { int *a; a = foo (); printf ("%d", *a);

Writing Files (Binary) # include <stdio.h> # include < stdlib.h> # include < assert.h> # include <sys / types.h> # include <sys / stat.h> # include <fcntl.h> int main () { int i, rc, fd; unsigned int array [40]; for (i =0; i <40; i ++) { array [ i] = i; fd = open ("test - output ", O_WRONLY O_CREAT ); if (fd < 0) { exit (1) ;

Writing Files (Binary) rc = write (fd, array, sizeof ( array )); if (rc < 0) { exit (1) ; close (fd); exit (0) ; % cat test - output #@u!$%

Reading Files (Binary) # include <stdio.h> # include < stdlib.h> # include < assert.h> # include <sys / types.h> # include <sys / stat.h> # include <fcntl.h> # define PRE_ROW (4) int main () { int i, rc, fd; unsigned int array [40]; fd = open ("test - output ", O_RDONLY ); if (fd < 0) { exit (1) ; rc = read (fd, array, sizeof ( array )); if (rc < 0) { exit (1)

Reading Files (Binary) printf (" offset = %4d : ", 0); for (i =0; i <40; i ++) { printf ("0x %08 x " array [i]); if ((( i +1) % PER_ROW ) == 0) { printf ("\ noffset = %4d : ", (i +1) *( sizeof ( unsigned int ))); printf ("\n"); close (fd); exit (0) ;

Reading Files (Binary) offset = 0 : 0 x00000000 0 x00000001 0 x00000002 0 x00000003 offset = 16 : 0 x00000004 0 x00000005 0 x00000006 0 x00000007 offset = 32 : 0 x00000008 0 x00000009 0 x0000000a 0 x0000000b offset = 48 : 0 x0000000c 0 x0000000d 0 x0000000e 0 x0000000f offset = 64 : 0 x00000010 0 x00000011 0 x00000012 0 x00000013 offset = 80 : 0 x00000014 0 x00000015 0 x00000016 0 x00000017 offset = 96 : 0 x00000018 0 x00000019 0 x0000001a 0 x0000001b offset = 112 : 0 x0000001c 0 x0000001d 0 x0000001e 0 x0000001f offset = 128 : 0 x00000020 0 x00000021 0 x00000022 0 x00000023 offset = 144 : 0 x00000024 0 x00000025 0 x00000026 0 x00000027 offset = 160 :

Endianness System 161 uses Big-Endian semantics Intel x86 uses Little-Endian Endianness defines the byte order in memory Little-Endian has the least significant byte first Big-Endian has the most significant byte first Big-Endian is what we are used to

Endianness Example : x = 0 xdeadbeef /* 3735928559 */ Little - Endian : Least significant byte at lowest address Word addressed by address of least significant byte 0.. 7 8.. 15 16.. 23 24.. 31 [ ef ] [ be ] [ ad ] [ de ] Big - Endian : Most significant byte at lowest address Word addressed by address of most significant byte 0.. 7 8.. 15 16.. 23 24.. 31 [ de ] [ ad ] [ be ] [ ef ]

Reading Files (Binary): Little-Endian offset = 0 : 0 x00000000 0 x01000000 0 x02000000 0 x03000000 offset = 16 : 0 x04000000 0 x05000000 0 x06000000 0 x07000000 offset = 32 : 0 x08000000 0 x09000000 0 x0a000000 0 x0b000000 offset = 48 : 0 x0c000000 0 x0d000000 0 x0e000000 0 x0f000000 offset = 64 : 0 x10000000 0 x11000000 0 x12000000 0 x13000000 offset = 80 : 0 x14000000 0 x15000000 0 x16000000 0 x17000000 offset = 96 : 0 x18000000 0 x19000000 0 x1a000000 0 x1b000000 offset = 112 : 0 x1c000000 0 x1d000000 0 x1e000000 0 x1f000000 offset = 128 : 0 x20000000 0 x21000000 0 x22000000 0 x23000000 offset = 144 : 0 x24000000 0 x25000000 0 x26000000 0 x27000000 offset = 160 :

Kthxbye exit (0) Questions?

Useful Resources cplusplus.com Wikipedia Linux/Unix man pages man printf man open Library functions are in chapter 3 of man man 3 strcpy System calls are in chapter 2 of man man 2 open