Computer Graphics 3 Lecture 1: Introduction to C/C++ Programming Benjamin Mora 1 University of Wales Swansea Pr. Min Chen Dr. Benjamin Mora.

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
You can do more than what you think ……… If you believe you can.
Dynamic Memory Management
Names and Bindings.
Chapter 7:: Data Types Programming Language Pragmatics
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
Introduction of Memory Allocation. Memory Allocation There are two types of memory allocations possible in c. Compile-time or Static allocation Run-time.
DYNAMIC MEMORY MANAGEMENT. int x; When the source code containing this statement is compiled and linked, an executable file is generated. When the executable.
Kernighan/Ritchie: Kelley/Pohl:
Memory and Files Dr. Andrew Wallace PhD BEng(hons) EurIng
Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015 Some slides.
Malloc Recitation Section K (Kevin Su) November 5 th, 2012.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
CS 536 Spring Run-time organization Lecture 19.
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Run-time Environment and Program Organization
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles C/C++ Emery Berger and Mark Corner University of Massachusetts.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Computer Organization and Assembly Language C/C++ Pointers and Memory Allocation.
Language Evaluation Criteria
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
Introduction to Java University of Sunderland CSE301 Harry R. Erwin, PhD.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
Object Oriented Programming Elhanan Borenstein copyrights © Elhanan Borenstein.
February 11, 2005 More Pointers Dynamic Memory Allocation.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Stack and Heap Memory Stack resident variables include:
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts.
Pointers OVERVIEW.
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
Object-Oriented Programming in C++
Dynamic memory allocation and Pointers Lecture 4.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Copyright © Curt Hill Structured Data What this course is about.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
C/C++ Basics. Basic Concepts Basic functions of each language: Input, output, math, decision, repetition Types of errors: Syntax errors, logic errors,
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
Topics memory alignment and structures typedef for struct names bitwise & for viewing bits malloc and free (dynamic storage in C) new and delete (dynamic.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
CHAPTER 4 VARIABLES & BINDING SUNG-DONG KIM DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
CSCI206 - Computer Organization & Programming
Memory Allocation CS 217.
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Computer Graphics 3 Lecture 1: Introduction to C/C++ Programming Benjamin Mora 1 University of Wales Swansea Pr. Min Chen Dr. Benjamin Mora

Content 2 Benjamin Mora University of Wales Swansea Why C/C++ for Graphics? Differences With Java. Memory Allocation. Operator Overloading. Not seen here: –Multiple Inheritance. –Standard Template Libraries (STLs). –Templates (Genericity)

Why C/C++ for Graphics? 3 Benjamin Mora University of Wales Swansea

Why C/C++? 4 Benjamin Mora University of Wales Swansea Why not! –Java? –Pascal? –Lisp? –Prolog? C/C++ generates efficient code. –Close to assembly code… –With some experience, programmers can guess what the compiled code will look like. Easier to optimize. –C/C++ Compilers heavily optimized!

Why C/C++? 5 Benjamin Mora University of Wales Swansea Rendering times are crucial in Computer Graphics! –C/C++ much faster than Java. –Real-Time renderings. –Non real-time renderings. –C/C++ much used by the Graphics community. Programming skills and chosen algorithms will also make a big difference. –However, code readability and simplicity should not be neglected. The Object-Oriented side of C++ can favour simplicity. –Using classes may be slower.

Differences with Java 6 Benjamin Mora University of Wales Swansea Compiled vs Interpreted. Portability. –Not as Portable as Java, although the code remains the same! Preprocessor Stage (C/C++). –#directive –Replace code by directive. Memory management –Programmer’s role in C/C++ Very costly in Java. –Memory Leaks. –Pointers. –Usually no bound checking. Unsecure if not well-programmed.

Differences with Java 7 Benjamin Mora University of Wales Swansea Struct, unions and class types versus class only types. Multiple inheritance with C/C++. –Can lead to problems. –Java “Interface”. Strings. Operator Overloading. –Though a few issues sometime. Implicit casting.

Memory Management 8 Benjamin Mora University of Wales Swansea

Memory Management 9 Benjamin Mora University of Wales Swansea Programmer’s role. –Program must keep track of every piece of memory dynamically allocated by the program. –Allocation functions: malloc and free (C) new, delete, new[] and delete[] (C++) Elements stored on the heap. Handled by the OS. Process Control Block Program Heap -Global Variables -Allocated Data Stack Local Variables (function calls) Top of Stack Address

Memory Management 10 Benjamin Mora University of Wales Swansea Why dynamic allocation? –Because memory allocation cannot always be determined at compilation time. Must be done very carefully –Memory leaks. –Releasing allocated memory too many times. –See VectorND Example. In conclusion, rigorous programming is required.

Pointers 11 Benjamin Mora University of Wales Swansea A pointer is an integer variable pointing at a specific address. char *myPointer=“Hello”; char *myPointer2=myPointer; int i=0; int *intPointer=&i; (* intPointer)++;//*intPointer=Pointed object The pointer size depends on the OS: –32 bits OS => 4 bytes –64 bits OS => 8 bytes 0xab myPointer ‘H’‘e’‘l’ ‘o’0 0xab myPointer2 = 0xab123456

Pointers 12 Benjamin Mora University of Wales Swansea char *myPointer =new char; char *myPointer2 =new char[5]; myPointer=NULL; //very bad:loosing the reference Delete[] myPointer2; Delete[] myPointer2; //Now wrong myPointer myPointer2 0xef01234a ? 0xab ????? 0xef01234a 0xab

Using Multi-Dimensional Arrays in C/C++ 13 Benjamin Mora University of Wales Swansea

Linearized arrays in C/C++ 14 Benjamin Mora University of Wales Swansea Problem: how to store a multidimensional array? –Example: A grey-level image (256 levels) made of 640*480 pixels –A possible solution: unsigned char image[640][480]; –Accessing the pixel value at the (i,j) location: image[i][j] A huge drawback: – The code can only process images of that size !!! Smaller images can actually be processed, but coding this way is not really recommended (especially for maintenance)

Linearized arrays in C/C++ 15 Benjamin Mora University of Wales Swansea In C/C++, arrays are linearly (consecutively) stored in memory (every array allocates one memory block of the size of the array). –The “unsigned char image[640][480]” declaration will allocate a 640*480*sizeof(unsigned char) (usually 1 byte) bytes in memory. –The image is stored as a sequence of consecutive rows (every row is made of 480 pixels) –The “image[i][j]” value is in fact located at the memory address: Address(image)+480*i+j

Linearized arrays in C/C++ 16 Benjamin Mora University of Wales Swansea The extension to a multidimensional array is the same: –example: float volume[size_z] [size_y] [size_x] –Allocation size: size_z*size_y*size_x *sizeOf(float) bytes () –the “volume[k][j][i]” value is located at the memory address: Address(volume)+(i+j*size_x+k*size_x*size_y)*sizeOf(float) –Or in an Horner’s scheme like formulation Address(volume)+(i+(j+size_y*k)*size_x) *sizeOf(float) In conclusion, in order to handle multidimensional arrays, it is best to simulate the indexing by using a 1D array, and also to use dynamic allocations!

Linearized arrays in C/C++ 17 Benjamin Mora University of Wales Swansea Examples: –Declaration: float *volume; –Allocation: volume=(float *) malloc (sizeOf(float)*size_x*size_y*size_z);/*c*/ volume=new float[size_x*size_y*size_z] //c++ –Use: volume[(k*size_y+j)*size_x+i] –Do not forget to de-allocate!!! –In case of polymorphic data (either char, float,…), void * can be used instead of float here

Overloading 18 Benjamin Mora University of Wales Swansea

Operator Overloading 19 Benjamin Mora University of Wales Swansea C++ allows overloading common operators: – New, delete[], … – +, -, /, *, *=, … – >>, <<, &, &&, |, ^, … – >, <, == – =, [], … Can be tricky sometimes. The programmer must be careful. –E.g., redefining operator + on integers. a=b stands for a.operator=(b)

Operator Overloading 20 Benjamin Mora University of Wales Swansea Very useful when logically done: Complex C1(1,0), C2(2,2); C1=C1*C2; But also requires more CPU resources at run-time. –Avoid classes and overloading if code must be as efficient as possible. –The right balance between speed and code readability is an issue of CG.

Example 21 Benjamin Mora University of Wales Swansea

class VectorND 22 Benjamin Mora University of Wales Swansea class VectorND //.h file { public: VectorND(void); VectorND(int n); ~VectorND(void); void operator=(const VectorND &v); float &operator[](const int &i); VectorND operator+(const VectorND &v); int Length() {return size;}; protected: private: int size; float *pointer; void resize (int n); };

class VectorND 23 Benjamin Mora University of Wales Swansea VectorND::VectorND(void) { size=0; pointer=NULL; } VectorND::VectorND(int n) { pointer=NULL; size=0; resize(n); }

class VectorND 24 Benjamin Mora University of Wales Swansea VectorND::~VectorND(void) { if (pointer!=NULL) delete[] pointer; } void VectorND::resize(int n) { if (pointer!=NULL) delete[] pointer; pointer=new float[n]; if (pointer!=NULL) size=n; else size=0; }

class VectorND 25 Benjamin Mora University of Wales Swansea void VectorND::operator=(const VectorND &v) { int i; if (this==&v) //case v=v; return; resize(v.size); for (i=0;i<v.size;i++) { pointer[i]=v.pointer[i];//Duplicate the Data } }

class VectorND 26 Benjamin Mora University of Wales Swansea float &VectorND::operator[] (const int &i) { return pointer[i]; }; VectorND VectorND::operator+(const VectorND &v) { int i; static VectorND tmp; tmp.resize(v.size); if (v.size!=size) return v; //Not necessarily needed for (i=0;i<v.size;i++) tmp[i]=pointer[i]+v.pointer[i]; return tmp; }