06-C_structs_and_memory_model

06-C_structs_and_memory_model - Computer Science 136: David...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Computer Science 136: David R. Cheriton School of Computer Science Elementary Algorithm Design and Data Abstraction Winter 2012 Lecture 6 C Variables, Memory, Pointers This is an intense lecture: fasten your seat belts. We introduce some C equivalents (and subtleties) on accessing variables in other modules. We introduce structures in C We discuss variable scoping, access, and persistence We talk about dynamic memory allocation and pointers CS 136 Winter 2012 06 C Variables, Memory, Pointers 1/23 Moving forward Last time: Functions in C Book: Chapter 2 C Fundamentals Chapter 15 Writing Large Programs CS 136 Winter 2012 06 C Variables, Memory, Pointers 2/23 Scope Global variables File times5.h : int times5( int ); File times5.c : #include "times5.h" // "Global" variable int i = 0; int times5( int n) { // "Local" variable int acc = 0; i = 0; while (i < 5) { acc = acc + n; } return acc; } int reseti( void ) {i = 0;} CS 136 Winter 2012 06 C Variables, Memory, Pointers 3/23 Scope Global variables File times5.h : int times5( int ); File times5.c : #include "times5.h" // "Global" variable int i = 0; int times5( int n) { // "Local" variable int acc = 0; i = 0; while (i < 5) { acc = acc + n; } return acc; } int reseti( void ) {i = 0;} File: times5user.c : #include <stdio.h> #include "times5.h" int main( void ) { printf("%d\n", times5(3)); reseti(); printf("%d\n",times5(2)); } CS 136 Winter 2012 06 C Variables, Memory, Pointers 3/23 Scope Global variables File times5.h : int times5( int ); File times5.c : #include "times5.h" // "Global" variable int i = 0; int times5( int n) { // "Local" variable int acc = 0; i = 0; while (i < 5) { acc = acc + n; } return acc; } int reseti( void ) {i = 0;} File: times5user.c : #include <stdio.h> #include "times5.h" int main( void ) { printf("%d\n", times5(3)); reseti(); printf("%d\n",times5(2)); } File: times5cheater.c : #include <stdio.h> #include "times5.h" extern int i; int main( void ) { printf("%d\n", times5(3)); i=3; printf("%d\n",times5(2)); } CS 136 Winter 2012 06 C Variables, Memory, Pointers 3/23 Variable and function hiding with static #include <stdio.h> int i = 5; // Visible to whole program via ' extern ' static int j = 5; // Visible only within file // Visible to whole program // via function declaration int foo( int bar) { // Stuff } // Visible only within file static int helper( int stuff) { // Stuff } CS 136 Winter 2012 06 C Variables, Memory, Pointers 4/23 Another meaning for static #include <stdio.h> #include <limits.h> int runningmax( int i) { // Initialization below happens once static int runmax = INT_MIN; if (i > runmax) { runmax = i; } return runmax; // Value of runmax persists } static has even more meanings in Java and C++......
View Full Document

This note was uploaded on 02/22/2012 for the course CS cs136 taught by Professor Cormack during the Winter '10 term at Waterloo.

Page1 / 32

06-C_structs_and_memory_model - Computer Science 136: David...

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

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