lec2 - CS240: Programming in C Lecture 2: Overview 1...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CS240: Programming in C Lecture 2: Overview 1 Monday, January 17, 2011
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Programming Model How does C view the world? code Memory Stack Globals 2 Monday, January 17, 2011
Background image of page 2
Programming Model Execution mediated via a stack function calls and returns local variables Variables name memory and globals contents of memory contain values or references (pointers) to other pieces of memory v1 v2 v3 stack frame “context” Memory 3 Monday, January 17, 2011
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Programming Model Imperative Program execution defined by effects on memory and globals Control-flow expressed primarily through: loops and iteration conditionals “heavyweight” function calls Variables are containers for values x = 3 -- x is a memory cell that holds 3; it’s not the same as 3 referential transparency (or lack thereof) Aggregate structures (records, arrays) define contiguous (potentially variable-sized) regions of memory 4 Monday, January 17, 2011
Background image of page 4
Types Abstractly, we can think of a type as a set of values and operations on those values in Java, a type is a class that represents a set comprising the fields of the object and its methods In C, types have a much less rigid definition a convenient way of reasoning about memory layout all values (regardless of their type) have a common representation as a sequence of bytes in memory 5 Monday, January 17, 2011
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Types Reflects underlying machine model There are base types (int, char, . ..) There are pointers to base types (*int, *char,. ..) There are pointers to pointers to base types (**int, **char, . ..) and so on Arrays and structs provide a way of aggregating and naming regions of memory Functions have types that reflect their input/output behavior: int f (char c[]) return type argument type We can have pointers to functions just as we have pointers to base types. But, functions are not values. ... 6 Monday, January 17, 2011
Background image of page 6
Why understanding types matter … Types define an abstraction or approximation of a computation. ... what is a specification? More practically, there are implicit conversions that take place and they may result in truncation, and . .. Some data types are not interpreted the same on different platforms, they are machine-dependent 7 Monday, January 17, 2011
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Type representation How is data represented in memory? Data unit is bytes, for representing positive
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 32

lec2 - CS240: Programming in C Lecture 2: Overview 1...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online