Squishy Maps for Soft Body Modelling Using Generalised Chain Mail
KIT308/408 (Advanced) Multicore Architecture and Programming
Revision / Crash Course 2: C/C++
Dr. Ian Lewis
Discpline of ICT, School of TED
University of Tasmania, Australia
1
There are a number of concepts we use in this unit that it might be helpful to have a refresher of (or crash course in)
Number Systems
The Mandelbrot Set
C/C++
We’ll be using a variety of number systems in this unit
Decimal (I really hope you know this already), binary, octal (maybe), and hexadecimal
The tutorial work and assignments will be in C/C++
The major example we use in the tutorial work relies on code to calculate the Mandelbrot set
2
Lecture Content
C/C++
3
4
Header Files / Functions
// myheader.h
#ifndef __MYHEADER_H__
#define __MYHEADER_H__
// content of the header goes here
int calc(int inValue);
#endif // __MYHEADER_H__
// myheader.cpp
#include “myheader.h”
#include
int calc(int inValue)
{
int result = inValue;
// calc something dumb
for (int i = inValue – 1; i > 1; i–)
{
result *= i;
}
return result;
}
5
C++ Hello World
// helloworld.cpp
#include
/* main() in C++ takes argc (the number of arguments) and
argv (an array of arguments) */
int main(int argc, char** argv)
{
// “std” is the standard C++ namespace, like a Java package
std::cout << "Hello, World!" << std::endl;
return 0;
}
6
C/C++ Input / Output
#include
…
int number;
std::cin >> number;
std::cout << "You Typed: " <<
number << std::endl;
#include
…
int number;
scanf(“%d”, &number);
printf(“You Typed: %d\n”, number);
The C/C++ pre-processor is basically a copy-paste machine
#include “file.h” effectively pasted the contents of file.h into the current file
What values will x, y, and z get?
7
Preprocessor
#define MYCONSTANT 4
#define min(x, y) (x < y) ? x : y
int x = MYCONSTANT;
int y = min((5 + 8), 11);
int z = min(x++, y);
C++ doesn’t require the use of #define to use constants
8
#define Versus const
#define MYCONSTANT 4
const int MYCONSTANT2 = 4;
C/C++ provides pre- and post-increment operators (++ and --)
C/C++ also provides a number of shorthands for doing assignment and arithmetic on variables
e.g. x += y - z; is shorthand for x = x + (y – z);
9
Shorthand Operators and Increments
int x = 4, y = 5, z = 6;
x++;
y = ++z;
z *= (++x)--;
10
Pointers / Derefering
// Java / C#
MyObject obj = new MyObject(42);
MyObject obj2 = obj;
// C/C++
MyObject obj(42);
MyObject* obj2 = new MyObject(42);
MyObject* obj3 = obj2;
obj3 = &obj;
What are the values of a & b at the end?
11
Pointers / Derefering
int a = 8, b = 4;
int* p = &a;
int** q = &p;
*p = 3;
**q = 5;
q = &p;
*q = &b;
*p = 1;
What are the values of a & b at the end?
12
References Versus Pointers
int a = 8, b = 3;
int* p = &a;
int& r = a;
const int& s = b;
*p = 9;
r = 2;
b = 66;
r = s;
Why ever use pointers instead of references?
13
References Versus Pointers
struct MyObj
{
int x;
float y;
};
MyObj obj;
MyObj* p = &obj;
MyObj& r = obj;
p->x = 10; //(*p).x = 10;
r.y = 3.14f;
What value would x have after calling each of these functions?
14
Values, References, and Const
void addOne(int i)
{
i++;
}
…
int x = 5;
addOne(x);
void addOne(int& i)
{
i++;
}
…
int x = 5;
addOne(x);
void addOne(const int& i)
{
i++;
}
…
int x = 5;
addOne(x);
What’s the difference between values allocated space on the stack and on the heap?
15
Dynamic Memory
int staticArray[100];
int* dynamicArray = new int[100];
delete[] dynamicArray;
int staticArray[100];
int* dynamicArray =
(int*) malloc(sizeof(int) * 100);
free(dynamicArray);
These two are identical in C++.
16
Structs and Classes
struct MyStruct
{
int x;
float y;
};
class MyClass
{
public:
int x;
float y;
};
What’s the value of u.x for both of these?
17
Unions
struct MyStruct
{
int x;
float y;
};
MyStruct s;
s.x = 4;
s.y = 1.1f;
union MyUnion
{
int x;
float y;
};
MyUnion u;
u.x = 4;
u.y = 1.1f;
18
Operator Overloading
struct Point
{
float x, y, z;
};
inline Point operator * (float c, const Point& p)
{
Point p2 = { p.x * c, p.y * c, p.z * c };
return p2;
}
inline Vector operator – (const Point& p1, const Point& p2)
{
Vector v = { p1.x – p2.x, p1.y – p2.y, p1.z – p2.z };
return v;
}
What value do all of these have?
19
Bitwise Operations
unsigned int x = 0xFF;
unsigned int y = 0x0A;
unsigned int z = 0x07;
unsigned int a = x & y;
unsigned int b = x && y;
unsigned int c = x | y;
unsigned int d = (x ^ y) & (y | z);
unsigned int e = x >> 4;
unsigned int f = y << 4;
What is going to be printed?
20
True / False
if (7)
{
printf("seven\n");
}
if (-5)
{
printf("minus seven\n");
}
int x = 1 && 2;
if (x)
{
printf("%d\n", x);
}
Sizeof outputs the size of the variable of type
What would these output?
21
sizeof
printf("%d\n", sizeof(char));
printf("%d\n", sizeof(int));
printf("%d\n", sizeof(int*));
printf("%d\n",
sizeof(long long unsigned int));
int s[100];
printf("%d %d\n", sizeof(s), sizeof(*s));
int* t = new int[100];
printf("%d %d\n", sizeof(t), sizeof(*t));
/docProps/thumbnail.jpeg