06 C Programming 2
06 C Programming 2
C Programming 2
The slides are credited to Dr. Shafer
2
ì
C-Strings (Arrays of Characters)
Computer Systems and Networks Fall 2021
3
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 Fall 2021
8
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
ì
File I/O
Computer Systems and Networks Fall 2021
12
#include <stdio.h>
int main()
{
FILE *ptr_file;
char buf[1000];
ptr_file = fopen("input.txt","r");
if (!ptr_file)
return 1;
fclose(ptr_file);
return 0;
}
Pointer Arithmetic
ì Example
ì If an integer pointer, iptr holds address 32, then
after the expression iptr++, iptr will hold 36
(assuming integer is 4 bytes).
Problem 1
The name of the array is actually a pointer pointing to the first element of the array.
ì
Dynamic Memory Management
Computer Systems and Networks Fall 2021
18
ì #include <stdlib.h>
Malloc – 1D
address: 60 64 68 72 76
value: array[0] array[1] array[2] array[3] array[4]
Malloc – 2D
Allocate 4x5 integers (Hint for lab 4)
int **array; //a double pointer
for(i=0;i<4;i++)
array[i] = (int *)malloc(sizeof(int)*5);
array of ints
array of ints
array of ints
array of ints
Malloc – 3D
int ***array; //a triple pointer
a ‘cuboid’ of integers
an array of a matrix of
double pointers single pointers
Problem 2
P2
Computer Systems and Networks Fall 2021
25
ì
Memory Management Internals
Computer Systems and Networks Fall 2021
26
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:
Memory Management
Memory Management
0xFFFFFFFFFFFFFFFF (32 or 64 bit)
ì OS creates virtual
memory space for
process when started
0x0000000000000000
Computer Systems and Networks Fall 2021
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 Fall 2021
32
Memory Management
0xFFFFFFFFFFFFFFFF (32 or 64 bit)
ì Stack created to Stack
track program
function calls
and local
variables
0x0000000000000000
Computer Systems and Networks Fall 2021
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 Fall 2021
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 Fall 2021
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 Fall 2021
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 Fall 2021
37
Memory Management
0xFFFFFFFFFFFFFFFF (32 or 64 bit)
ì Program Stack
terminates
0x0000000000000000
Computer Systems and Networks Fall 2021
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 Fall 2021
43
http://www.yolinux.com/TUTORIALS/C++MemoryCorruptionAndMemoryLeaks.html
http://www.yolinux.com/TUTORIALS/C++MemoryCorruptionAndMemoryLeaks.html
Computer Systems and Networks Fall 2021
45
http://xkcd.com/371/
Memory Management
Memory Management
ì
Structures
Computer Systems and Networks Fall 2021
49
Structures
struct database
{
Useful way to group
int id_number; related variables!
int age;
float salary;
};
int main()
{
struct database employee;
employee.age = 22;
employee.id_number = 1;
employee.salary = 12000.21;
}
Computer Systems and Networks Fall 2021
50
Problem
Declare a structure called board that contains:
• a double character pointer matrix
• two integer variables height and width denoting the
number of rows and columns in the matrix.
Problem
Continue with the code from Problem 3.
free() is actually a reverse operation of malloc. The steps you use for free are opposite
of the steps for malloc. Free the dynamically allocated 2D matrix you created in
Problem 3.
You’re ready to
for Lab 4!
Computer Systems and Networks Fall 2021