1 Intro and Pointers
Transcript of 1 Intro and Pointers
![Page 1: 1 Intro and Pointers](https://reader031.fdocuments.net/reader031/viewer/2022021922/577ccd2b1a28ab9e788baf29/html5/thumbnails/1.jpg)
Course Overview Introducing C History of C The C Abstract Model Memory in C Understanding Pointers
High Performance Computing in C/C++The C Abstract Machine Model
David Chisnall
February 14, 2011
![Page 2: 1 Intro and Pointers](https://reader031.fdocuments.net/reader031/viewer/2022021922/577ccd2b1a28ab9e788baf29/html5/thumbnails/2.jpg)
Course Overview Introducing C History of C The C Abstract Model Memory in C Understanding Pointers
Course Aims
• Introduce C programming
• Describe the differences between C, C++, and Java
• Explain programming styles appropriate to HPC
![Page 3: 1 Intro and Pointers](https://reader031.fdocuments.net/reader031/viewer/2022021922/577ccd2b1a28ab9e788baf29/html5/thumbnails/3.jpg)
Course Overview Introducing C History of C The C Abstract Model Memory in C Understanding Pointers
Why Should You Care About. . . C?
• Widely used in industry
• Many other languages inherit syntax and semantics
• 40 years of legacy code...
![Page 4: 1 Intro and Pointers](https://reader031.fdocuments.net/reader031/viewer/2022021922/577ccd2b1a28ab9e788baf29/html5/thumbnails/4.jpg)
Course Overview Introducing C History of C The C Abstract Model Memory in C Understanding Pointers
Why Should You Care About. . . HPC?
High-performance computing is a small (well-paid) niche, but‘High performance’ is relative.
Machine Performance Architecture Note
Cray-1 250MFLOPS SIMD First supercomputerIBM XT <1MFLOP SISD First PC
Cortex A8 10MFLOPS SISD + SIMD Smartphone CPUnVidia Fermi 1GFLOPS SIMD + MIMD Modern GPU
Many ‘HPC’ techniques eventually become useful elsewhere.
![Page 5: 1 Intro and Pointers](https://reader031.fdocuments.net/reader031/viewer/2022021922/577ccd2b1a28ab9e788baf29/html5/thumbnails/5.jpg)
Course Overview Introducing C History of C The C Abstract Model Memory in C Understanding Pointers
Assessment
50% Programming:
• 1 easy assignment: 15%
• 1 moderate assignment: 25%
• 1 hard assignment: 10%
50% Essay.
![Page 6: 1 Intro and Pointers](https://reader031.fdocuments.net/reader031/viewer/2022021922/577ccd2b1a28ab9e788baf29/html5/thumbnails/6.jpg)
Course Overview Introducing C History of C The C Abstract Model Memory in C Understanding Pointers
Why was C Created?
• UNIX was originally written in assembly language
• Porting it to a new architecture meant rewriting it
• C was created to be slightly more abstract than assembly, butalmost as fast
![Page 7: 1 Intro and Pointers](https://reader031.fdocuments.net/reader031/viewer/2022021922/577ccd2b1a28ab9e788baf29/html5/thumbnails/7.jpg)
Course Overview Introducing C History of C The C Abstract Model Memory in C Understanding Pointers
The C Abstract Model
• Very close to how a real machine works.
• Some simple abstractions
• ‘Close to the metal’
Every C language construct maps to a very short sequence ofmachine instructions.
![Page 8: 1 Intro and Pointers](https://reader031.fdocuments.net/reader031/viewer/2022021922/577ccd2b1a28ab9e788baf29/html5/thumbnails/8.jpg)
Course Overview Introducing C History of C The C Abstract Model Memory in C Understanding Pointers
Contrast: The Java Abstract Model
Contains many things that are not in the real hardware model:
• Automatic memory management
• Bounds-checked arrays
• Objects and classes
• Dynamic method lookup
• Typed memory and reflection
![Page 9: 1 Intro and Pointers](https://reader031.fdocuments.net/reader031/viewer/2022021922/577ccd2b1a28ab9e788baf29/html5/thumbnails/9.jpg)
Course Overview Introducing C History of C The C Abstract Model Memory in C Understanding Pointers
Why C is Fast
• A simple compiler can produce good code from C
• Easy for the programmer to understand performance
• CPU features are usually exposed directly, via laguageextensions
![Page 10: 1 Intro and Pointers](https://reader031.fdocuments.net/reader031/viewer/2022021922/577ccd2b1a28ab9e788baf29/html5/thumbnails/10.jpg)
Course Overview Introducing C History of C The C Abstract Model Memory in C Understanding Pointers
C Memory Model
• Flat array of bytes
• Each byte is addressable
Contrast with Java:
• Split into objects
• Objects accessed by (opaque) reference
• Access control on object fields
![Page 11: 1 Intro and Pointers](https://reader031.fdocuments.net/reader031/viewer/2022021922/577ccd2b1a28ab9e788baf29/html5/thumbnails/11.jpg)
Course Overview Introducing C History of C The C Abstract Model Memory in C Understanding Pointers
Pointers Vs Java References
C/C++ Pointers Java ReferencesNumbers indicating an addressin memory
Opaque type with only assign(copy) operation defined.
Can be created as the result ofarithmetic
Can only be created from validobjects.
Can point to invalid locations Can only point to valid objectsor NULL.
Point to memory. User mustknow type of destination.
Point to objects which knowtheir own type.
Java implements a variant of Smalltalk’s object memory model. Cexposes a flat memory model.
![Page 12: 1 Intro and Pointers](https://reader031.fdocuments.net/reader031/viewer/2022021922/577ccd2b1a28ab9e788baf29/html5/thumbnails/12.jpg)
Course Overview Introducing C History of C The C Abstract Model Memory in C Understanding Pointers
No Garbage Collection
Memory must be explicitly allocated and deallocated. Potentialproblems include:
• Memory leaks (don’t free after last use)
• Dangling pointers (use after free)
• Buffer overflows (access out of bounds)
![Page 13: 1 Intro and Pointers](https://reader031.fdocuments.net/reader031/viewer/2022021922/577ccd2b1a28ab9e788baf29/html5/thumbnails/13.jpg)
Course Overview Introducing C History of C The C Abstract Model Memory in C Understanding Pointers
Code is Data
1. C code is compiled to machine code
2. Machine code is loaded into memory
3. Everything in memory is accessible to C code
4. Pointers can point to functions as well as data
![Page 14: 1 Intro and Pointers](https://reader031.fdocuments.net/reader031/viewer/2022021922/577ccd2b1a28ab9e788baf29/html5/thumbnails/14.jpg)
Course Overview Introducing C History of C The C Abstract Model Memory in C Understanding Pointers
Example Memory Layout
0x00000000
0xffffffff
Program Code0x00001000
0x0000f000
Library Code
0x00100000
0x00140000
Static Data
0x000fe000
Heap Memory
0xf0000000
stack Memory
0xffffffff
![Page 15: 1 Intro and Pointers](https://reader031.fdocuments.net/reader031/viewer/2022021922/577ccd2b1a28ab9e788baf29/html5/thumbnails/15.jpg)
Course Overview Introducing C History of C The C Abstract Model Memory in C Understanding Pointers
C Pointers
• Contain a memory address
• Can point anywhere in memory
• Do not store the type of the data pointed to
![Page 16: 1 Intro and Pointers](https://reader031.fdocuments.net/reader031/viewer/2022021922/577ccd2b1a28ab9e788baf29/html5/thumbnails/16.jpg)
Course Overview Introducing C History of C The C Abstract Model Memory in C Understanding Pointers
Example: Casting
Listing 1: Incorrect Casting in Java�class Cast
{
public static void main(String [] args)
{
String str = "12345";
Object obj = str;
Number num = (Number)obj;
}
} � �$ javac Cast.java && java Cast
Exception in thread "main"
java.lang.ClassCastException:
java.lang.String cannot be cast to java.lang.Number
at Cast.main(Cast.java:7)
![Page 17: 1 Intro and Pointers](https://reader031.fdocuments.net/reader031/viewer/2022021922/577ccd2b1a28ab9e788baf29/html5/thumbnails/17.jpg)
Course Overview Introducing C History of C The C Abstract Model Memory in C Understanding Pointers
Example: Casting
Listing 2: Incorrect Casting in C�int main(void)
{
int a = 12;
int *ptr = &a;
float *ptr2 = (float *)ptr;
*ptr2 = 1123 e12;
return a;
} � �$ c99 cast.c && ./a.out ; echo $?
87 Wrong result, but no error report!
![Page 18: 1 Intro and Pointers](https://reader031.fdocuments.net/reader031/viewer/2022021922/577ccd2b1a28ab9e788baf29/html5/thumbnails/18.jpg)
Course Overview Introducing C History of C The C Abstract Model Memory in C Understanding Pointers
Example: Pointers are Numbers
�#include <stdio.h>
int main(void)
{
int a = 12;
int b = (int)&a;
printf("a: 0x%x, b: 0x%x\n", a, b);
return 0;
} � �$ c99 ptr.c && ./a.out
a: 0xc, b: 0xbffff8fc
![Page 19: 1 Intro and Pointers](https://reader031.fdocuments.net/reader031/viewer/2022021922/577ccd2b1a28ab9e788baf29/html5/thumbnails/19.jpg)
Course Overview Introducing C History of C The C Abstract Model Memory in C Understanding Pointers
Pointers are Low Level
• Machine memory is indexed by a numerical address
• CPUs have load and store instructions for accessing memory
• This abstraction is exposed in C
![Page 20: 1 Intro and Pointers](https://reader031.fdocuments.net/reader031/viewer/2022021922/577ccd2b1a28ab9e788baf29/html5/thumbnails/20.jpg)
Course Overview Introducing C History of C The C Abstract Model Memory in C Understanding Pointers
Pointer Example
// Integer
int a = 12;
// Pointer to integer
int *b = &a;
// Pointer to pointer
to integer
int **c = &b;
a: 0x000000000c
0xbffff8ff
0xbffff8fc
b: 0xbffff8fc
0xbffff8f8
c: 0xbffff8f8
0xbffff8f4
![Page 21: 1 Intro and Pointers](https://reader031.fdocuments.net/reader031/viewer/2022021922/577ccd2b1a28ab9e788baf29/html5/thumbnails/21.jpg)
Course Overview Introducing C History of C The C Abstract Model Memory in C Understanding Pointers
What Does int a Really Mean?
• Allocate enough space for an integer (2, 4, or 8 bytes,depending on the platform)
• Keep track of where this memory is
• Replace references to a with this address
a = 12 means store the value 12 in memory at the address reservedfor the variable a.
![Page 22: 1 Intro and Pointers](https://reader031.fdocuments.net/reader031/viewer/2022021922/577ccd2b1a28ab9e788baf29/html5/thumbnails/22.jpg)
Course Overview Introducing C History of C The C Abstract Model Memory in C Understanding Pointers
What Are Pointers For?
• Connecting data structures
• Aliasing data (two pointers to the same data)
• Anything you use object references for in Java