Professional Documents
Culture Documents
8 CPS393 MemoryC
8 CPS393 MemoryC
8 CPS393 MemoryC
Memory Management in C:
Static arrays, strings
Pointers, addresses
Dynamic allocation
Memory (un)safety in C
c wholeNum1 realNum2
int number
scanf( , &number);
6356771 Memory
6356764 6356771
Dereferencing
* Operator
- A
CI
c2 A
char
=
ptr c1 c2
&c1 Memory
int i, j, *p, *q; // pointers and ints together
p = &i; p=
adress of i
// value of p is the address of i
q = &j; 9
= adress of j // value of q is the address of j
*p = 5; put
s where
i S=
points at
// store 5 at the address stored in p
*q = *p + i; // dereference p, add i, store at
u
9point
at what
+
1
is the
5 +
+
5
8
=
j
of
10
points S
Output? i = 5, j = 10
int i, j, *p, *q; // pointers and ints together
p = &i; // value of p is the address of i
q = &j; // value of q is the address of j
*p = 5; // store 5 at the address in p
*q = *p + i; // dereference p, add i, store at
// address in q
printf( i = %d, j = %d\ , i, j);
5 10
You Need Help!
More
Pointer
Examples
int x = 57, y = 0;
int *a = &y, *b = &x;
address of
address of x
*a = 12; y 12
=
// y = 12
b = a; ↓ and a both point
to
// b and a both point to y
y
*b = 15; y
15
=
// y = 15
x = 57, y = 15
int x = 57, y = 0;
int *a, *b = &y; ↑
-
points
↓
the address
of
*b = 7; y
7
=
// y = 7
a = &x; a = adness of x
// a points to x
x = *a - *b; // x = x y
the value the value
-
where a
points o *b points
X -
7 Sp
SF
- =
x = 50, y = 7
int x, y, z, *p1, *p2, *p3, *p4;
the X
address of
p1 = &x; // p1 points to x
p4 = p1; points
pup)
// at X
p4 points to x
p2 = p4; p1cPEIEx // p2 points to x
*p4 = 5; // x
G
=
x = 5
y = x; y //
= 5
y = 5
x = 5, y = 5
char c1, c2, c3, *ptr;
ptr c1 c2 c3
int x, *p1;
p1 = &x;
printf("Please enter an integer: ");
scanf("%d", p1); /* Missing & */
printf("x = %d\n", x);
double int
num whole
#include <stdio.h>
int main()
{
int nums[100]; An array of 100 integers
return 0;
}
Number of
type array_name [length];
elements
Memory
2 4 6 8 10 Memory
2 4 6 8 10 Memory
return 0;
}
The usual, C indexing starts at 0
© Alex Ufkes, 2020, 2021 39
#include <stdio.h>
int main()
{
int nums[5];
Address of first element
scanf( , &nums[0]);
scanf( , &nums[1]); Elements of an integer
scanf( , &nums[2]); array can be treated like
scanf( , &nums[3]); any other integer!
scanf( , &nums[4]);
return 0;
}
© Alex Ufkes, 2020, 2021 40
IMPORTANT!
#include <stdio.h>
int main() This will compile but may
{ crash during run-time.
int nums[5];
nums[5] = 57; /* BAD! Out of bounds! */
return 0;
}
#include <stdio.h>
int main()
{
int nums[5];
nums[5] = 57; /* BAD! Out of bounds! */
return 0;
}
57 Memory
0 1
© Alex Ufkes, 2020, 2021 2 3 4 42
We can overrun the array by
999*4 bytes and still be in within
int x; Declaration
printf( );
Other statements
scanf( , &x);
int nums[x]; OK! Another Declaration
return 0;
int x, nums[100];
nums = nums[200]; BAD!
return 0;
0 1 2 3 4
14 7 Memory
Pointer Arithmetic
nums == &nums[0]
nums is the address of the first element.
nums + i == &nums[i]
*(nums + i) == nums[i] nums+i is the address of the ith element.
Addresses go up
by 4. Why?
Pointer Shenanigans
CAREFUL: It only works for static arrays declared in the same scope!
Why?
© Alex Ufkes, 2020, 2021 60
No! Will not compile!
One option?
Static arrays:
Stored on the stack, meaning size is limited (varies by compiler/OS)
Destroyed when they leave scope
Declare using int arr[], double arr[] etc.
Dynamic arrays:
calloc is much faster than malloc, for reasons that are fascinating
but beyond the scope of this course, and touch on CPS590 topics:
https://vorpus.org/blog/why-does-calloc-exist/
#include <stdio.h>
#include <stdlib.h>
int main () Is there a problem here?
{ What values are stored in the array?
int size, i;
calloc initializes the reserved
double *arr;
memory to zero. This is safe.
printf( );
scanf( , &size);
arr = (double*) calloc(size, sizeof(double));
for (i = 0; i < size; i++)
printf( , arr[i]);
Dynamically allocated memory
free(arr); must always be freed!
}
Console:
Each iteration, we allocate 1-million doubles.
Doubles from previous allocation are still on
the heap! Orphaned.
Very bad, no way to free at that point.
© Alex Ufkes, 2022 75
Dynamic arrays can be
returned from functions!
Remember to free them.
#include <stdio.h>
int main()
{
char city[] = { , , , , , , , \ };
printf( \ \ /* 0 on the asci table */
return 0;
}
#include <stdio.h>
int main()
{ What is the size of city?
char city[] = ;
return 0;
}
When we initialize this way, the null character is
automatically added to the end of the char array.
#include <stdio.h>
int main() Placeholder for string
{
char city[] = ;
printf( , city);
puts(city); puts is for strings only. Inserts a
return 0; newline after printing the string.
}
© Alex Ufkes, 2020, 2021 81
How Does printf
printf( , city);
#include <stdio.h>
int main()
{
char city1[] = { , i , , , \ };
char city2[] = { , , , };
printf( , city1);
printf( , city2); No null character!
return 0; What happens?
}
© Alex Ufkes, 2020, 2021 83
We get unpredictable junk, because the
system keeps reading characters until we
hit \ somewhere else.
© Alex Ufkes, 2020, 2021 84
#include <stdio.h>
int main()
{
char city[] = ;
Shell:
printf( , city); Taranta
return 0;
}
© Alex Ufkes, 2020, 2021 85
int i;
char city[] = ;
char city[10];
scanf( , city);
Console
North Bay Treated as two separate strings
North
© Alex Ufkes, 2020, 2021 90
© Alex Ufkes, 2020, 2021 91
char city[16];
gets(city);
printf( , city);
Console
North Bay
North Bay
© Alex Ufkes, 2020, 2021 92
char city[16]; What if the user enters string
gets(city); longer than 15 characters?
15 + \ = 16 total
printf( , city);
#include <string.h>
char city[] = ;
int length = strlen(city);
printf( \ , length);
char city1[8] = ;
char city2[8];
city2 = city1;
Found in string.h
char city1[8] = ;
char city2[8];
strcpy(city2, city1)
char city[8];
city = ;
Found in string.h
char city[8];
strcpy(city, );
Memory Management in C:
Static arrays, strings
Pointers, addresses
Dynamic allocation
Memory (un)safety in C