G52CPP C++ Programming Lecture 6. Dr Jason Atkin

Similar documents
G52CPP C++ Programming Lecture 8. Dr Jason Atkin

G52CPP C++ Programming Lecture 9

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

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

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

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

CSE 374 Programming Concepts & Tools

EL6483: Brief Overview of C Programming Language

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

CE221 Programming in C++ Part 1 Introduction

COMP322 - Introduction to C++

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

C and C++ 2. Functions Preprocessor. Alan Mycroft

Object-Oriented Programming, Iouliia Skliarova

Fundamentals of Programming

C++ for Java Programmers

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

Modifiers. int foo(int x) { static int y=0; /* value of y is saved */ y = x + y + 7; /* across invocations of foo */ return y; }

Informatica e Sistemi in Tempo Reale

Variables and literals

Control flow and string example. C and C++ Functions. Function type-system nasties. 2. Functions Preprocessor. Alastair R. Beresford.

G52CPP C++ Programming Lecture 13

Final CSE 131B Spring 2004

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

Lectures 5-6: Introduction to C

Contents Lecture 3. C Preprocessor, Chapter 11 Declarations, Chapter 8. Jonas Skeppstedt Lecture / 44

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

The University of Nottingham

COSC 2P91. Bringing it all together... Week 4b. Brock University. Brock University (Week 4b) Bringing it all together... 1 / 22

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #54. Organizing Code in multiple files

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

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Topic 6: A Quick Intro To C

Interview Questions of C++

Kurt Schmidt. October 30, 2018

ELEC 377 C Programming Tutorial. ELEC Operating Systems

Fast Introduction to Object Oriented Programming and C++

Binghamton University. CS-211 Fall Variable Scope

Binghamton University. CS-211 Fall Variable Scope

Binghamton University. CS-211 Fall Variable Scope

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

Short Notes of CS201

CSE 333 Midterm Exam July 24, Name UW ID#

COMP26120: Algorithms and Imperative Programming. Lecture 5: Program structuring, Java vs. C, and common mistakes

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

#include. Practical C Issues: #define. #define Macros. Example. #if

C Programming Review CSC 4320/6320

Intermediate Programming, Spring 2017*

CS201 - Introduction to Programming Glossary By

A Fast Review of C Essentials Part I

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

Ch. 3: The C in C++ - Continued -

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

Practical C Issues:! Preprocessor Directives, Multi-file Development, Makefiles. CS449 Fall 2017

The University of Nottingham

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

Page 1. Agenda. Programming Languages. C Compilation Process

Macros and Preprocessor. CGS 3460, Lecture 39 Apr 17, 2006 Hen-I Yang

C: Program Structure. Department of Computer Science College of Engineering Boise State University. September 11, /13

Chapter IV Introduction to C for Java programmers

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 C: Linked list, casts, the rest of the preprocessor (Thanks to Hal Perkins)

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

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

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

G52CPP C++ Programming Lecture 10. Dr Jason Atkin

CODE TIME TECHNOLOGIES. Abassi RTOS MISRA-C:2004. Compliance Report

Have examined process Creating program Have developed program Written in C Source code

BLM2031 Structured Programming. Zeyneb KURT

Lecture 2: C Programm

More on C programming

Review of the C Programming Language for Principles of Operating Systems

Symbols, Compilation Units, and Pre-Processing

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

CSE 333 Lecture 2 Memory

C introduction: part 1

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

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

6.096 Introduction to C++ January (IAP) 2009

#include <stdio.h> int main() { printf ("hello class\n"); return 0; }

CMPSC 311- Introduction to Systems Programming Module: Build Processing

Pointers and References

Lecture 2, September 4

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011

The University of Nottingham

Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Memory Corruption 101 From Primitives to Exploit

0x0d2C May your signals all trap May your references be bounded All memory aligned Floats to ints round. remember...

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

G52CPP C++ Programming Lecture 12

Syntax and Variables

In addition to name. Each variable in C program is characterized by its Type Scope Storage Class. Type Have already discussed

Programming in C - Part 2

Model Viva Questions for Programming in C lab

Variables. Data Types.

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.


Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

Transcription:

G52CPP C++ Programming Lecture 6 Dr Jason Atkin 1

Last lecture The Stack Lifetime of local variables Global variables Static local variables const (briefly) 2

Visibility is different from lifetime Just because a variable exists, doesn t mean that you can access it Globals : access from anywhere May be shadowed by parameters or local variables In C++ (not C) you can use Scope Resolution to access globals when they are shadowed Can be hidden within a file Static local variables Only access from inside the function Exist all of the time, like globals Do not use a pointer to something, if the thing it points to no longer exists 3

String literals again String literals should not be changed i.e. use const pointers Should use: const char* str = Hello ; Not: char* str = Hello ; Compiler should give warnings otherwise 4

const pointers Read backwards with * meaning pointer to float * const pcf = &f; Constant pointer to a float The pointer is constant constant float* float const * cpf = &f; Pointer to constant float const float * cpf = &f; (same as float const *) Pointer to float which is constant The float pointed at cannot be changed through the pointer const float * const cpcf = &f; Constant pointer to float which is constant Neither the pointer nor the thing it points at can be changed 5

This lecture C-type structs C++ builds on these and adds features -> operator The C/C++ Pre-processor 6

structs Without any methods (for the moment) 7

structs We will start with C-type structs C++ structs and classes (introduced later) can be considered to be extensions of C structs, e.g. allowing member functions, inheritance etc Structs and classes are virtually the same thing in C++ These group related data together Examples: Group three integers together to specify a time: struct Time { struct.cpp }; int hour; int minute; int second; Shorter version, for day, month, year: struct Date { int d, m, y }; Note the ; at the end! 8

Creating a struct on the stack Create objects of type struct using the name Need to say struct <name> in C Example: struct Date { int d, m, y; }; struct.cpp int main( int argc, char* argv[] ) { struct Date dob = { 1, 4, 1990 }; printf( DOB: %02d/%02d/%04d\n, dob.d, dob.m, dob.y ); dob.d = 2; return 0; Creates a struct on the stack } Note: no new operator is used!!! 9

Accessing members of a struct Use the. operator to access members Exactly as for Java classes Example: struct Date { int d, m, y; }; struct.cpp int main( int argc, char* argv[] ) { struct Date dob = { 1, 4, 1990 }; printf( DOB: %02d/%02d/%04d\n, dob.d, dob.m, dob.y ); } dob.d = 2; return 0; Access values Initialisation Like an array 10

structs act like any other type Once defined, you can use structs as any other type You can take the address of a variable of type struct and store it in a struct pointer, e.g. struct Date* pdob = &dob; Note: C++ does not need this struct keyword You can embed a struct as a member of another struct You can create an array of structs You can ask for the sizeof() a struct 11

Creating an initialised struct Date struct Date { char d, m; short y; }; Date singledate = { 1, 2, 2000 }; printf( "Initialised singledate is:%02d/%02d/%04d\n", singledate.d, singledate.m, singledate.y ); 12

Creating an initialised struct Date struct Date { char d, m; short y; }; 1) Define the type struct Date Date singledate = { 1, 2, 2000 }; 2) Create and initialise a variable of type struct Date printf( "Initialised singledate is:%02d/%02d/%04d\n", singledate.d, singledate.m, singledate.y ); Initialised singledate is : 01/02/2000 13

Array of structs (on the stack) Date arrayofdatesonstack[5]; for ( i=0 ; i < 5 ; i++ ) Array of 5 elements printf( "arrayofdatesonstack[%d] is : %02d/%02d/%04d\n", i, arrayofdatesonstack[i].d, arrayofdatesonstack[i].m, arrayofdatesonstack[i].y ); 14

Array of structs (on the stack) Date arrayofdatesonstack[5]; for ( i=0 ; i < 5 ; i++ ) printf( "arrayofdatesonstack[%d] is : %02d/%02d/%04d\n", i, arrayofdatesonstack[i].d, arrayofdatesonstack[i].m, arrayofdatesonstack[i].y ); arrayofdatesonstack[0] is : 00/00/0000 arrayofdatesonstack[1] is : 02/00/0000 arrayofdatesonstack[2] is : -104/-51/0034 arrayofdatesonstack[3] is : -41/53/24833 arrayofdatesonstack[4] is : -71/-74/24854 Values are uninitialised!!! 15

Array of dates (on the stack) /* Uses array initialiser and struct initialiser */ Date initarrofdatesonstack[] = { {1,1,2001}, {2,2,2002}, {3,3,2003}, {4,4,2004}, {5,5,2005} }; for ( i=0 ; i < 5 ; i++ ) printf( "initarrayofdatesonstack[%d] is : %02d/%02d/%04d\n", i, initarrayofdatesonstack[i].d, initarrayofdatesonstack[i].m, initarrayofdatesonstack[i].y ); initalisedarrayofdatesonstack[0] is : 01/01/2001 initalisedarrayofdatesonstack[1] is : 02/02/2002 initialisedarrayofdatesonstack[2] is : 03/03/2003 initialisedarrayofdatesonstack[3] is : 04/04/2004 initialisedarrayofdatesonstack[4] is : 05/05/2005 16

Position of data Like arrays, the positions of the members inside a struct are known Elements will be placed sequentially in memory, in the order they are defined in the structure (sometimes this matters) So you CAN use the ordering to determine where parts will be in memory Except there may be padding too (gaps) You can specify alignment/padding More on sizeof(structs), and positions in a struct later (and bit structs and unions) 17

Arrays of structs struct Date { char d, m; short y; } struct Date char d char m short y dobs[0] dobs[1] dobs[2] dobs[3] dobs[4] Date dobs[5]; d m y d m y d m y d m y d m y Notes: Syntax is the same as for arrays of basic types, e.g. int Elements are one after another in memory (like other arrays) 18

Passing structs into functions struct Date dob = {1, 4, 1990}; Either pass the struct A (bit-wise) copy of the struct is put on the stack You can change this, using C++ copy constructor see later Any changes made inside the function affect the copy void foo(struct Date dob) { dob.m = 3; } foo( dob ); Or a pointer to the struct A copy of the pointer is put on the stack You can use the pointer to access the original copy void bar(struct Date* pdob){(*pdob).m =3;} bar( &dob ); Use. to access struct members For a pointer you could use (*pdob).m 19

X->Y means (*X).Y struct time { int hour, minute, second; }; struct time t; t.hour = 12; t.minute = 34; t.second = 14; struct time* pt = &t; pt->hour = 11; /* = (*pt).hour */ pt->minute = 13; /* = (*pt).minute */ pt->second = 5; /* = (*pt).second */ printf( "The time is %02d:%02d:%02d\n", t.hour, t.minute, t.second ); 20

The return statement Functions can return only ONE value The returned value is copied! The value may be: a basic type (e.g. int) a pointer (or C++ reference, see later) The address is copied (same for references) a struct, union (see later) or object (C++ only) The struct, union, object etc is copied May create a temporary variable in calling function, to store the returned value 21

Stack reminder These structs were created on the stack (i.e. as local variables) Remember: Data on the stack vanishes when the stack frame that contains it is removed from the stack i.e. when the function/block in which it is defined ends Do not return a pointer to one of these! 22

The C/C++ pre-processor 23

The C/C++ Preprocessor Runs BEFORE passing code to the compiler Compiler will only see the code after the preprocessor has changed it It affects statements beginning with # Examples: #include #define, #undef #if, #ifdef, #ifndef, #else, #endif #pragma 24

#include Replaces this statement by the text of the specified file For example, to include function declarations E.g. #include <stdio.h> Include the file with standard input/output function declarations in it (e.g. printf) Looks in the directories on the include path Normally used for system header files Note: C++ standard header files may differ but same effects E.g. #include myheader.h The usually means look in the project path as well as the main include path Normally used for your own, project-specific header files Do not confuse with Java s import : import defines the packages to look in for resolving class names (more like the C++ keyword using, but still different) #include replaces the line, potentially with function declarations 25

Using multiple files 26

Reminder Declare functions before usage Called function prototyping Definitions are also declarations So, sorting functions into reverse order works - all declared before use e.g.: int myfunc1(int); int myfunc2(int); int main( int argc, char* argv[] ) { return myfunc1(argc); } int myfunc1( int i1 ) { return myfunc2(i1) + 1; } int myfunc2( int i2 ) { return 1 + i2; } Note: no parameter names are needed. The return type, function name and parameter types must be specified 27

Sharing things between files In general, you can put functions (and classes) in any files you wish (the filename is totally unimportant) Global variables and functions are always accessible from anywhere within the same file You can hide them from other files by using the static keyword, e.g. : static int g_hidden = 1; They are then accessible everywhere within the same file but not from other files If not static (i.e. hidden), then: You can access global functions from other files Just declare them and the linker will do the work You can access global variables from other files Use the keyword extern in a declaration extern changes a definition into a declaration 28

Visibility (Linkage) What can be seen where? File1.cpp static int hidden_in_file; int visible_from_outside; static void myintfunc() { } void myextfunc( char c ) { int local_var = 2; static int persistant = 4; myintfunc(); } File2.cpp extern int visible_from_outside; void myextfunc( char ); void myfunc() { char c = 4; myextfunc( c ) } 29

Key Idea: Encapsulation The idea of hiding the internals the data Give access to the data to as few things as possible i.e. hide it as much as possible Controlling the interface which can be seen Why? Helps with debugging and structure You can see what can alter each thing C encapsulation can be performed using files External interface (global functions) Internal functions (static global functions) C++/Java use classes (much more control) 30

Summary: the static keyword static is used for three different things For local variables static means the value is maintained between function calls For global variables and functions static limits visibility/access to within the file For C++ (not C!) classes: Method or variable is associated with the class not the object (one copy per class, no this pointer) The same as Java for this one 31

Compiling and linking 32

Types of files Source code files, named.cpp or.c Contain your functions and classes Header files, named.h or.hpp Declarations for all functions which you want to make available to other files i.e. function name, return type, parameter types Declarations for classes, in C++ Any constants you want to make available Any #defines to apply to other files Anything else you want to share Library files, named.o,.lib, Already compiled Contain implementation of library functions 33

Compiling and linking Source Code (.c or.cpp) Header Files (.h or.hpp) gcc <.c files> -o <output_file> Static libraries (.o or.lib) Preprocessor Compiler Object code files (Optional:.o,.obj, ) Linker Executable Shared/dynamic library 34

Compiling and linking Source Code (.c or.cpp) Header Files (.h or.hpp) Preprocessor Compiler Object code files (Optional:.o,.obj, ) You can just compile to object code files (not link): gcc c <.c files> -o <output_file> 35

Compiling and linking You can then link the files by passing the.o files to gcc (instead of the.c files) gcc <.o files> -o <output_file> Static libraries (.o or.lib) Object code files (Optional:.o,.obj, ) Linker Executable Shared/dynamic library 36

Compiling with gcc gcc uses the file extension to determine file type when compiling/linking:.c for C files.cpp (and others) for C++ files.o for object code files (just need linking) Standard C library is linked by default when compiling C code When compiling C++ you need to link in the standard C++ library files manually e.g. use -lstdc++ on gcc command line or (often) can use g++ instead of gcc 37

Introducing #define Will return to #define in lecture 8 38

#define An semi-intelligent find and replace facility Often considered bad in C++ code (useful in C) const is used more often, especially for members Template functions are better than macros Example: define a constant : #define MAX_ENTRIES 100 Replace occurrences of MAX_ENTRIES by the text 100 (without quotes), e.g. in: if ( entry_num < MAX_ENTRIES ) { } Remember: Done by the pre-processor! E.g. NOT actually a definition of a constant Constant #defines usually written in CAPITALS 39

Conditional compilation You can remove parts of the source code if desired Done by the pre-processor (not compiled) E.g. Only include code if some name has been defined earlier (in the code or included header file) #ifdef <NAME_OF_DEFINE> <Include this code if it was defined> #else <Include this code if it was not defined> #endif To include only if not defined use #ifndef More in lecture 8 important for header files 40

Next lecture Dynamic memory allocation Linked lists in C/C++ 41