05 C Programming
05 C Programming
C Programming
2
Lab Schedule
Activities Deadlines
ì This Week ì Lab 3 – Feb 6th 2017
ì Intro to C by 5am
ì Intro to Build Tools and
ì Lab 4 – Feb 20th 2017
Makefiles
by 5am
ì Lab 3 – Build Tools
ì Next Week
ì Lab 4 – C Programming
Project
ì Creator of C programming
language
ì Co-creator of Unix
(with Ken Thompson, Brian
Kernighan, and others at Bell
Labs)
ì 9/9/1941—10/12/2011
Dennis Ritchie and Ken Thompson use a teletypewriter to run a program on a UNIX-based
computer system they co-founded at Bell Labs in New Jersey. Their development work
more than 40 years ago facilitated the realization of the Internet.
Computer Systems and Networks Spring 2017
6
ì
C Programming
Computer Systems and Networks Spring 2017
7
ì No new / delete
ì bool keyword
ì Added in C99 standard
ì printf("This is a string\n");
Documentation
Structures
struct database
{
int id_number;
int age;
float salary;
};
int main()
{
struct database employee;
employee.age = 22;
employee.id_number = 1;
employee.salary = 12000.21;
}
ì
C-Strings (Arrays of Characters)
Computer Systems and Networks Spring 2017
14
C Strings
Arrays of Characters
ì char phrase[]="Math";
phrase
M A T H \0
Arrays of Characters
ì char phrase[8]="Math";
phrase
ì #include <string.h>
ì Useful functions
ì strcpy - String copy
ì strcmp - String compare
ì strlen - String length
ì strcat - String concatenate
String Copy
ì char phrase2[8];
ì strcpy(phrase2, phrase1);
phrase phrase1[0] phrase1[1] phrase1[2] phrase1[3] phrase1[4]
1
M A T H \0
2
M A T H \0 ??? ??? ???
Computer Systems and Networks Spring 2017
19
String Concatenation
ì strcat(phrase1, phrase2);
phrase phrase1[0] phrase1[1] phrase1[2] phrase1[3] phrase1[4] phrase1[5] phrase1[6] phrase1[7]
1
C O M P S C I \0
ctype Library
ì #include <ctype.h>
char c = toupper('a');
printf("%c", c); // A
ctype Library
ì
Memory Management
Computer Systems and Networks Spring 2017
23
ì #include <stdlib.h>
Memory Management
Memory Management
ì The Heap:
ì A region of memory for dynamic memory allocation
ì Per-process – each program gets its own heap
ì Managed by malloc() and related functions
ì Different from the stack, which is for static variables
(known at compile-time)
Memory Management
ì malloc() outline:
1. Call malloc() and request memory
Memory Management
Memory Management
0xFFFFFFFFFFFFFFFF (32 or 64 bit)
ì OS creates virtual
memory space for
process when started
0x0000000000000000
Computer Systems and Networks Spring 2017
31
Memory Management
0xFFFFFFFFFFFFFFFF (32 or 64 bit)
ì OS loads in the
program from
disk
ì “Text” region
ì Program code
ì “Data” region
ì Program fixed
data Data (Program data)
0x0000000000000000
Computer Systems and Networks Spring 2017
32
Memory Management
0xFFFFFFFFFFFFFFFF (32 or 64 bit)
ì Stack created to Stack
track program
function calls
and local
variables
0x0000000000000000
Computer Systems and Networks Spring 2017
33
Memory Management
0xFFFFFFFFFFFFFFFF (32 or 64 bit)
ì Heap created to Stack
store dynamic
memory from
malloc()and
(Unused / unmapped virtual memory)
related functions
ì Not to scale –
this unused Heap
region is huge!
Data (Program data)
0x0000000000000000
Computer Systems and Networks Spring 2017
34
Memory Management
0xFFFFFFFFFFFFFFFF (32 or 64 bit)
ì Program starts Stack
running
ì malloc()
allocates some (Unused / unmapped virtual memory)
memory
Heap
0x0000000000000000
Computer Systems and Networks Spring 2017
35
Memory Management
0xFFFFFFFFFFFFFFFF (32 or 64 bit)
ì Original heap Stack
space eventually
fills up
(Unused / unmapped virtual memory)
ì malloc()
requests New
space
additional space Heap
from the kernel
by using brk()
system call Data (Program data)
0x0000000000000000
Computer Systems and Networks Spring 2017
36
Memory Management
0xFFFFFFFFFFFFFFFF (32 or 64 bit)
ì free() Stack
deallocates
blocks from the
heap (Unused / unmapped virtual memory)
Heap
0x0000000000000000
Computer Systems and Networks Spring 2017
37
Memory Management
0xFFFFFFFFFFFFFFFF (32 or 64 bit)
ì Program Stack
terminates
0x0000000000000000
Computer Systems and Networks Spring 2017
38
Memory Management
char *a = malloc(128*sizeof(char));
char *b = malloc(128*sizeof(char));
b = a;
free(a);
free(b);
http://www.yolinux.com/TUTORIALS/C++MemoryCorruptionAndMemoryLeaks.html
char *a = malloc(128*sizeof(char));
http://www.yolinux.com/TUTORIALS/C++MemoryCorruptionAndMemoryLeaks.html
Computer Systems and Networks Spring 2017
43
http://www.yolinux.com/TUTORIALS/C++MemoryCorruptionAndMemoryLeaks.html
http://www.yolinux.com/TUTORIALS/C++MemoryCorruptionAndMemoryLeaks.html
Computer Systems and Networks Spring 2017
45
http://xkcd.com/371/
Memory Management
Memory Management