pointers and arrays - agh university of science and technologygawronski/2019_pp/lecture_08.pdf ·...

76
Pointers and arrays Lecture 8 November 26, 2019 (Lecture 8) Pointers and arrays November 26, 2019 1 / 19

Upload: others

Post on 24-Jul-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Pointers and arrays

Lecture 8

November 26, 2019

(Lecture 8) Pointers and arrays November 26, 2019 1 / 19

Page 2: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Outline

1 Pointersusing array name as a pointerusing pointer as array name

2 Dynamic storage allocation

3 Memory leak

(Lecture 8) Pointers and arrays November 26, 2019 2 / 19

Page 3: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Pointers - combining ∗ and ++ operators

C programmers often combine the * and ++ operators in statementsthat process array elements.

int a[10], i = 0, j = 10, *p = &a[0];

a[i++] = j;*p++ = j;

expression meaning*p++ or *(p++) value of expression is *p before increment

increment p later(*p)++ value of expression is *p before increment

increment *p later*++p or *(++p) increment p first

value of expression is *p after increment++*p or ++(*p) increment *p first

value of expression is *p after increment

(Lecture 8) Pointers and arrays November 26, 2019 3 / 19

Page 4: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Pointers - combining ∗ and ++ operators

C programmers often combine the * and ++ operators in statementsthat process array elements.

int a[10], i = 0, j = 10, *p = &a[0];

a[i++] = j;*p++ = j;

expression meaning*p++ or *(p++) value of expression is *p before increment

increment p later(*p)++ value of expression is *p before increment

increment *p later*++p or *(++p) increment p first

value of expression is *p after increment++*p or ++(*p) increment *p first

value of expression is *p after increment

(Lecture 8) Pointers and arrays November 26, 2019 3 / 19

Page 5: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Pointers - combining ∗ and ++ operators

C programmers often combine the * and ++ operators in statementsthat process array elements.

int a[10], i = 0, j = 10, *p = &a[0];

a[i++] = j;*p++ = j;

expression meaning*p++ or *(p++) value of expression is *p before increment

increment p later(*p)++ value of expression is *p before increment

increment *p later*++p or *(++p) increment p first

value of expression is *p after increment++*p or ++(*p) increment *p first

value of expression is *p after increment

(Lecture 8) Pointers and arrays November 26, 2019 3 / 19

Page 6: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Pointers - comparing pointers

Using relational operators to compare two pointers is meaningful onlywhen both point to elements of the same array.

The outcome of the comparison depends on the relative positions ofthe two elements in the array.

#define SIZE 10

int sump(int *start , int *end) {int total = 0;while (start < end)

total += *start ++;return total;

}...int tab[SIZE] = {20,10,5,39,4 ,16 ,19 ,26 ,31 ,20};int sum = sump(&tab[0], &tab[SIZE]);

(Lecture 8) Pointers and arrays November 26, 2019 4 / 19

Page 7: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Pointers - comparing pointers

Using relational operators to compare two pointers is meaningful onlywhen both point to elements of the same array.

The outcome of the comparison depends on the relative positions ofthe two elements in the array.

#define SIZE 10

int sump(int *start , int *end) {int total = 0;while (start < end)

total += *start ++;return total;

}...int tab[SIZE] = {20,10,5,39,4 ,16 ,19 ,26 ,31 ,20};int sum = sump(&tab[0], &tab[SIZE]);

(Lecture 8) Pointers and arrays November 26, 2019 4 / 19

Page 8: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Pointers - comparing pointers

Using relational operators to compare two pointers is meaningful onlywhen both point to elements of the same array.

The outcome of the comparison depends on the relative positions ofthe two elements in the array.

#define SIZE 10

int sump(int *start , int *end) {int total = 0;while (start < end)

total += *start ++;return total;

}...int tab[SIZE] = {20,10,5,39,4 ,16 ,19 ,26 ,31 ,20};int sum = sump(&tab[0], &tab[SIZE]);

(Lecture 8) Pointers and arrays November 26, 2019 4 / 19

Page 9: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Pointers - comparing pointers

#define SIZE 10

int a[SIZE], total = 0, *p = &a[0];for(;p<&a[SIZE]; p++)

total += *p;

Using &a[SIZE] in the for condition is perfectly safe, since the loopdoesn’t attempt to examine its value.

The loop terminates when p is equal to &a[SIZE].

(Lecture 8) Pointers and arrays November 26, 2019 5 / 19

Page 10: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Pointers - comparing pointers

#define SIZE 10

int a[SIZE], total = 0, *p = &a[0];for(;p<&a[SIZE]; p++)

total += *p;

Using &a[SIZE] in the for condition is perfectly safe, since the loopdoesn’t attempt to examine its value.

The loop terminates when p is equal to &a[SIZE].

(Lecture 8) Pointers and arrays November 26, 2019 5 / 19

Page 11: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Pointers - comparing pointers

#define SIZE 10

int a[SIZE], total = 0, *p = &a[0];for(;p<&a[SIZE]; p++)

total += *p;

Using &a[SIZE] in the for condition is perfectly safe, since the loopdoesn’t attempt to examine its value.

The loop terminates when p is equal to &a[SIZE].

(Lecture 8) Pointers and arrays November 26, 2019 5 / 19

Page 12: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Pointers - using array name as a pointer

Pointer arithmetic is one way in which arrays and pointers are related.

The name of an array can be used as a pointer to the first element inthe array.This relationship simplifies pointer arithmetic and makes both arraysand pointers more versatile.

int a[10];

*a = 12; //a[0]=12;*(a+1) = 16; //a[1]=16;

a+i is the same as &a[i] - both represent a pointer to element i of a.*(a+i) is the same as a[i] - both represent element i itself.Array subscripting can be viewed as a form of pointer arithmetic.

#define SIZE 10int a[SIZE], total = 0, *p = a;for(;p < a + SIZE; p++)

total += *p;

(Lecture 8) Pointers and arrays November 26, 2019 6 / 19

Page 13: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Pointers - using array name as a pointer

Pointer arithmetic is one way in which arrays and pointers are related.The name of an array can be used as a pointer to the first element inthe array.

This relationship simplifies pointer arithmetic and makes both arraysand pointers more versatile.

int a[10];

*a = 12; //a[0]=12;*(a+1) = 16; //a[1]=16;

a+i is the same as &a[i] - both represent a pointer to element i of a.*(a+i) is the same as a[i] - both represent element i itself.Array subscripting can be viewed as a form of pointer arithmetic.

#define SIZE 10int a[SIZE], total = 0, *p = a;for(;p < a + SIZE; p++)

total += *p;

(Lecture 8) Pointers and arrays November 26, 2019 6 / 19

Page 14: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Pointers - using array name as a pointer

Pointer arithmetic is one way in which arrays and pointers are related.The name of an array can be used as a pointer to the first element inthe array.This relationship simplifies pointer arithmetic and makes both arraysand pointers more versatile.

int a[10];

*a = 12; //a[0]=12;*(a+1) = 16; //a[1]=16;

a+i is the same as &a[i] - both represent a pointer to element i of a.*(a+i) is the same as a[i] - both represent element i itself.Array subscripting can be viewed as a form of pointer arithmetic.

#define SIZE 10int a[SIZE], total = 0, *p = a;for(;p < a + SIZE; p++)

total += *p;

(Lecture 8) Pointers and arrays November 26, 2019 6 / 19

Page 15: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Pointers - using array name as a pointer

Pointer arithmetic is one way in which arrays and pointers are related.The name of an array can be used as a pointer to the first element inthe array.This relationship simplifies pointer arithmetic and makes both arraysand pointers more versatile.

int a[10];

*a = 12; //a[0]=12;*(a+1) = 16; //a[1]=16;

a+i is the same as &a[i] - both represent a pointer to element i of a.*(a+i) is the same as a[i] - both represent element i itself.Array subscripting can be viewed as a form of pointer arithmetic.

#define SIZE 10int a[SIZE], total = 0, *p = a;for(;p < a + SIZE; p++)

total += *p;

(Lecture 8) Pointers and arrays November 26, 2019 6 / 19

Page 16: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Pointers - using array name as a pointer

Pointer arithmetic is one way in which arrays and pointers are related.The name of an array can be used as a pointer to the first element inthe array.This relationship simplifies pointer arithmetic and makes both arraysand pointers more versatile.

int a[10];

*a = 12; //a[0]=12;*(a+1) = 16; //a[1]=16;

a+i is the same as &a[i] - both represent a pointer to element i of a.

*(a+i) is the same as a[i] - both represent element i itself.Array subscripting can be viewed as a form of pointer arithmetic.

#define SIZE 10int a[SIZE], total = 0, *p = a;for(;p < a + SIZE; p++)

total += *p;

(Lecture 8) Pointers and arrays November 26, 2019 6 / 19

Page 17: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Pointers - using array name as a pointer

Pointer arithmetic is one way in which arrays and pointers are related.The name of an array can be used as a pointer to the first element inthe array.This relationship simplifies pointer arithmetic and makes both arraysand pointers more versatile.

int a[10];

*a = 12; //a[0]=12;*(a+1) = 16; //a[1]=16;

a+i is the same as &a[i] - both represent a pointer to element i of a.*(a+i) is the same as a[i] - both represent element i itself.

Array subscripting can be viewed as a form of pointer arithmetic.

#define SIZE 10int a[SIZE], total = 0, *p = a;for(;p < a + SIZE; p++)

total += *p;

(Lecture 8) Pointers and arrays November 26, 2019 6 / 19

Page 18: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Pointers - using array name as a pointer

Pointer arithmetic is one way in which arrays and pointers are related.The name of an array can be used as a pointer to the first element inthe array.This relationship simplifies pointer arithmetic and makes both arraysand pointers more versatile.

int a[10];

*a = 12; //a[0]=12;*(a+1) = 16; //a[1]=16;

a+i is the same as &a[i] - both represent a pointer to element i of a.*(a+i) is the same as a[i] - both represent element i itself.Array subscripting can be viewed as a form of pointer arithmetic.

#define SIZE 10int a[SIZE], total = 0, *p = a;for(;p < a + SIZE; p++)

total += *p;

(Lecture 8) Pointers and arrays November 26, 2019 6 / 19

Page 19: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Pointers - using array name as a pointer

Pointer arithmetic is one way in which arrays and pointers are related.The name of an array can be used as a pointer to the first element inthe array.This relationship simplifies pointer arithmetic and makes both arraysand pointers more versatile.

int a[10];

*a = 12; //a[0]=12;*(a+1) = 16; //a[1]=16;

a+i is the same as &a[i] - both represent a pointer to element i of a.*(a+i) is the same as a[i] - both represent element i itself.Array subscripting can be viewed as a form of pointer arithmetic.

#define SIZE 10int a[SIZE], total = 0, *p = a;for(;p < a + SIZE; p++)

total += *p;(Lecture 8) Pointers and arrays November 26, 2019 6 / 19

Page 20: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Pointers - using array name as a pointer

#define SIZE 8

int itab[SIZE], *pit=itab; //int *pit=&itab [0];double dtab[SIZE], *pdt=dtab;printf("%18s %15s\n", "int", "double");for (int i = 0; i < SIZE; i++)printf("ptr + %d %10p %10p\n", i, pit+i, pdt+i);

int doubleptr + 0 : 0x7fff36430c60 0x7fff36430c10ptr + 1 : 0x7fff36430c64 0x7fff36430c18ptr + 2 : 0x7fff36430c68 0x7fff36430c20ptr + 3 : 0x7fff36430c6c 0x7fff36430c28ptr + 4 : 0x7fff36430c70 0x7fff36430c30ptr + 5 : 0x7fff36430c74 0x7fff36430c38ptr + 6 : 0x7fff36430c78 0x7fff36430c40ptr + 7 : 0x7fff36430c7c 0x7fff36430c48

(Lecture 8) Pointers and arrays November 26, 2019 7 / 19

Page 21: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Pointers - using array name as a pointer

#define SIZE 8

int itab[SIZE], *pit=itab; //int *pit=&itab [0];double dtab[SIZE], *pdt=dtab;printf("%18s %15s\n", "int", "double");for (int i = 0; i < SIZE; i++)printf("ptr + %d %10p %10p\n", i, pit+i, pdt+i);

int doubleptr + 0 : 0x7fff36430c60 0x7fff36430c10ptr + 1 : 0x7fff36430c64 0x7fff36430c18ptr + 2 : 0x7fff36430c68 0x7fff36430c20ptr + 3 : 0x7fff36430c6c 0x7fff36430c28ptr + 4 : 0x7fff36430c70 0x7fff36430c30ptr + 5 : 0x7fff36430c74 0x7fff36430c38ptr + 6 : 0x7fff36430c78 0x7fff36430c40ptr + 7 : 0x7fff36430c7c 0x7fff36430c48

(Lecture 8) Pointers and arrays November 26, 2019 7 / 19

Page 22: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Pointers - using pointer as array name

We can use an array name as a pointer, we can use a pointer as if itwere an array name.

#define SIZE 10int a[SIZE], i = 0, total = 0, *p = a;for(;i < SIZE; i++)

total += p[i];

The compiler treats p[i] as *(p+i).

The compiler treats *(p+i) and *(i+p) the same, so ...

if *(p+i) is the same as p[i], then

if *(i+p) is the same as i[p].

Notation i[p] is perfectly correct but not recommended.

(Lecture 8) Pointers and arrays November 26, 2019 8 / 19

Page 23: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Pointers - using pointer as array name

We can use an array name as a pointer, we can use a pointer as if itwere an array name.

#define SIZE 10int a[SIZE], i = 0, total = 0, *p = a;for(;i < SIZE; i++)

total += p[i];

The compiler treats p[i] as *(p+i).

The compiler treats *(p+i) and *(i+p) the same, so ...

if *(p+i) is the same as p[i], then

if *(i+p) is the same as i[p].

Notation i[p] is perfectly correct but not recommended.

(Lecture 8) Pointers and arrays November 26, 2019 8 / 19

Page 24: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Pointers - using pointer as array name

We can use an array name as a pointer, we can use a pointer as if itwere an array name.

#define SIZE 10int a[SIZE], i = 0, total = 0, *p = a;for(;i < SIZE; i++)

total += p[i];

The compiler treats p[i] as *(p+i).

The compiler treats *(p+i) and *(i+p) the same, so ...

if *(p+i) is the same as p[i], then

if *(i+p) is the same as i[p].

Notation i[p] is perfectly correct but not recommended.

(Lecture 8) Pointers and arrays November 26, 2019 8 / 19

Page 25: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Pointers - using pointer as array name

We can use an array name as a pointer, we can use a pointer as if itwere an array name.

#define SIZE 10int a[SIZE], i = 0, total = 0, *p = a;for(;i < SIZE; i++)

total += p[i];

The compiler treats p[i] as *(p+i).

The compiler treats *(p+i) and *(i+p) the same, so ...

if *(p+i) is the same as p[i], then

if *(i+p) is the same as i[p].

Notation i[p] is perfectly correct but not recommended.

(Lecture 8) Pointers and arrays November 26, 2019 8 / 19

Page 26: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Pointers - using pointer as array name

We can use an array name as a pointer, we can use a pointer as if itwere an array name.

#define SIZE 10int a[SIZE], i = 0, total = 0, *p = a;for(;i < SIZE; i++)

total += p[i];

The compiler treats p[i] as *(p+i).

The compiler treats *(p+i) and *(i+p) the same, so ...

if *(p+i) is the same as p[i], then

if *(i+p) is the same as i[p].

Notation i[p] is perfectly correct but not recommended.

(Lecture 8) Pointers and arrays November 26, 2019 8 / 19

Page 27: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Pointers - using pointer as array name

We can use an array name as a pointer, we can use a pointer as if itwere an array name.

#define SIZE 10int a[SIZE], i = 0, total = 0, *p = a;for(;i < SIZE; i++)

total += p[i];

The compiler treats p[i] as *(p+i).

The compiler treats *(p+i) and *(i+p) the same, so ...

if *(p+i) is the same as p[i], then

if *(i+p) is the same as i[p].

Notation i[p] is perfectly correct but not recommended.

(Lecture 8) Pointers and arrays November 26, 2019 8 / 19

Page 28: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Pointers - using pointer as array name

We can use an array name as a pointer, we can use a pointer as if itwere an array name.

#define SIZE 10int a[SIZE], i = 0, total = 0, *p = a;for(;i < SIZE; i++)

total += p[i];

The compiler treats p[i] as *(p+i).

The compiler treats *(p+i) and *(i+p) the same, so ...

if *(p+i) is the same as p[i], then

if *(i+p) is the same as i[p].

Notation i[p] is perfectly correct but not recommended.

(Lecture 8) Pointers and arrays November 26, 2019 8 / 19

Page 29: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Dynamic storage allocation

C’s data structures are fixed in size.

In C99, the length of a variable-length array is determined at runtime, but it remains fixed for the rest of the array’s lifetime.

C supports dynamic storage allocation - the ability to allocate storageduring program execution.

Using dynamic storage allocation, we can design data structures thatgrow (or shrink) as needed.

Dynamic storage allocation is used most often for strings, arrays andstructures.

Dynamically allocated structures are of particular interest, since wecan link them together to form lists, trees, and other data structures.To allocate storage dynamically, we’ll need to call one of the threememory allocation functions declared in the <stdlib.h> header:

malloc - allocates a block of memory but doesn’t initialize it,calloc - allocates a block of memory and clears it,realloc - resizes a previously allocated block of memory.

(Lecture 8) Pointers and arrays November 26, 2019 9 / 19

Page 30: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Dynamic storage allocation

C’s data structures are fixed in size.

In C99, the length of a variable-length array is determined at runtime, but it remains fixed for the rest of the array’s lifetime.

C supports dynamic storage allocation - the ability to allocate storageduring program execution.

Using dynamic storage allocation, we can design data structures thatgrow (or shrink) as needed.

Dynamic storage allocation is used most often for strings, arrays andstructures.

Dynamically allocated structures are of particular interest, since wecan link them together to form lists, trees, and other data structures.To allocate storage dynamically, we’ll need to call one of the threememory allocation functions declared in the <stdlib.h> header:

malloc - allocates a block of memory but doesn’t initialize it,calloc - allocates a block of memory and clears it,realloc - resizes a previously allocated block of memory.

(Lecture 8) Pointers and arrays November 26, 2019 9 / 19

Page 31: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Dynamic storage allocation

C’s data structures are fixed in size.

In C99, the length of a variable-length array is determined at runtime, but it remains fixed for the rest of the array’s lifetime.

C supports dynamic storage allocation - the ability to allocate storageduring program execution.

Using dynamic storage allocation, we can design data structures thatgrow (or shrink) as needed.

Dynamic storage allocation is used most often for strings, arrays andstructures.

Dynamically allocated structures are of particular interest, since wecan link them together to form lists, trees, and other data structures.To allocate storage dynamically, we’ll need to call one of the threememory allocation functions declared in the <stdlib.h> header:

malloc - allocates a block of memory but doesn’t initialize it,calloc - allocates a block of memory and clears it,realloc - resizes a previously allocated block of memory.

(Lecture 8) Pointers and arrays November 26, 2019 9 / 19

Page 32: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Dynamic storage allocation

C’s data structures are fixed in size.

In C99, the length of a variable-length array is determined at runtime, but it remains fixed for the rest of the array’s lifetime.

C supports dynamic storage allocation - the ability to allocate storageduring program execution.

Using dynamic storage allocation, we can design data structures thatgrow (or shrink) as needed.

Dynamic storage allocation is used most often for strings, arrays andstructures.

Dynamically allocated structures are of particular interest, since wecan link them together to form lists, trees, and other data structures.To allocate storage dynamically, we’ll need to call one of the threememory allocation functions declared in the <stdlib.h> header:

malloc - allocates a block of memory but doesn’t initialize it,calloc - allocates a block of memory and clears it,realloc - resizes a previously allocated block of memory.

(Lecture 8) Pointers and arrays November 26, 2019 9 / 19

Page 33: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Dynamic storage allocation

C’s data structures are fixed in size.

In C99, the length of a variable-length array is determined at runtime, but it remains fixed for the rest of the array’s lifetime.

C supports dynamic storage allocation - the ability to allocate storageduring program execution.

Using dynamic storage allocation, we can design data structures thatgrow (or shrink) as needed.

Dynamic storage allocation is used most often for strings, arrays andstructures.

Dynamically allocated structures are of particular interest, since wecan link them together to form lists, trees, and other data structures.To allocate storage dynamically, we’ll need to call one of the threememory allocation functions declared in the <stdlib.h> header:

malloc - allocates a block of memory but doesn’t initialize it,calloc - allocates a block of memory and clears it,realloc - resizes a previously allocated block of memory.

(Lecture 8) Pointers and arrays November 26, 2019 9 / 19

Page 34: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Dynamic storage allocation

C’s data structures are fixed in size.

In C99, the length of a variable-length array is determined at runtime, but it remains fixed for the rest of the array’s lifetime.

C supports dynamic storage allocation - the ability to allocate storageduring program execution.

Using dynamic storage allocation, we can design data structures thatgrow (or shrink) as needed.

Dynamic storage allocation is used most often for strings, arrays andstructures.

Dynamically allocated structures are of particular interest, since wecan link them together to form lists, trees, and other data structures.

To allocate storage dynamically, we’ll need to call one of the threememory allocation functions declared in the <stdlib.h> header:

malloc - allocates a block of memory but doesn’t initialize it,calloc - allocates a block of memory and clears it,realloc - resizes a previously allocated block of memory.

(Lecture 8) Pointers and arrays November 26, 2019 9 / 19

Page 35: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Dynamic storage allocation

C’s data structures are fixed in size.

In C99, the length of a variable-length array is determined at runtime, but it remains fixed for the rest of the array’s lifetime.

C supports dynamic storage allocation - the ability to allocate storageduring program execution.

Using dynamic storage allocation, we can design data structures thatgrow (or shrink) as needed.

Dynamic storage allocation is used most often for strings, arrays andstructures.

Dynamically allocated structures are of particular interest, since wecan link them together to form lists, trees, and other data structures.To allocate storage dynamically, we’ll need to call one of the threememory allocation functions declared in the <stdlib.h> header:

malloc - allocates a block of memory but doesn’t initialize it,calloc - allocates a block of memory and clears it,realloc - resizes a previously allocated block of memory.

(Lecture 8) Pointers and arrays November 26, 2019 9 / 19

Page 36: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Dynamic storage allocation

C’s data structures are fixed in size.

In C99, the length of a variable-length array is determined at runtime, but it remains fixed for the rest of the array’s lifetime.

C supports dynamic storage allocation - the ability to allocate storageduring program execution.

Using dynamic storage allocation, we can design data structures thatgrow (or shrink) as needed.

Dynamic storage allocation is used most often for strings, arrays andstructures.

Dynamically allocated structures are of particular interest, since wecan link them together to form lists, trees, and other data structures.To allocate storage dynamically, we’ll need to call one of the threememory allocation functions declared in the <stdlib.h> header:malloc - allocates a block of memory but doesn’t initialize it,

calloc - allocates a block of memory and clears it,realloc - resizes a previously allocated block of memory.

(Lecture 8) Pointers and arrays November 26, 2019 9 / 19

Page 37: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Dynamic storage allocation

C’s data structures are fixed in size.

In C99, the length of a variable-length array is determined at runtime, but it remains fixed for the rest of the array’s lifetime.

C supports dynamic storage allocation - the ability to allocate storageduring program execution.

Using dynamic storage allocation, we can design data structures thatgrow (or shrink) as needed.

Dynamic storage allocation is used most often for strings, arrays andstructures.

Dynamically allocated structures are of particular interest, since wecan link them together to form lists, trees, and other data structures.To allocate storage dynamically, we’ll need to call one of the threememory allocation functions declared in the <stdlib.h> header:malloc - allocates a block of memory but doesn’t initialize it,calloc - allocates a block of memory and clears it,

realloc - resizes a previously allocated block of memory.

(Lecture 8) Pointers and arrays November 26, 2019 9 / 19

Page 38: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Dynamic storage allocation

C’s data structures are fixed in size.

In C99, the length of a variable-length array is determined at runtime, but it remains fixed for the rest of the array’s lifetime.

C supports dynamic storage allocation - the ability to allocate storageduring program execution.

Using dynamic storage allocation, we can design data structures thatgrow (or shrink) as needed.

Dynamic storage allocation is used most often for strings, arrays andstructures.

Dynamically allocated structures are of particular interest, since wecan link them together to form lists, trees, and other data structures.To allocate storage dynamically, we’ll need to call one of the threememory allocation functions declared in the <stdlib.h> header:malloc - allocates a block of memory but doesn’t initialize it,calloc - allocates a block of memory and clears it,realloc - resizes a previously allocated block of memory.

(Lecture 8) Pointers and arrays November 26, 2019 9 / 19

Page 39: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Dynamic storage allocation

When we call a memory allocation function to request a block ofmemory, the function has no idea what type of data we’re planning tostore in the block.

The function returns the memory address in the generic pointer typevoid *.

If the allocation cannot be made, the function returns a null pointer.

It’s the programmer’s responsibility to test the return value of anymemory allocation function and take appropriate action if it’s a nullpointer.

The effect of attempting to access memory through a null pointer isundefined, the program may crash.

Always use sizeof when calculating how much space is needed for anarray.

(Lecture 8) Pointers and arrays November 26, 2019 10 / 19

Page 40: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Dynamic storage allocation

When we call a memory allocation function to request a block ofmemory, the function has no idea what type of data we’re planning tostore in the block.

The function returns the memory address in the generic pointer typevoid *.

If the allocation cannot be made, the function returns a null pointer.

It’s the programmer’s responsibility to test the return value of anymemory allocation function and take appropriate action if it’s a nullpointer.

The effect of attempting to access memory through a null pointer isundefined, the program may crash.

Always use sizeof when calculating how much space is needed for anarray.

(Lecture 8) Pointers and arrays November 26, 2019 10 / 19

Page 41: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Dynamic storage allocation

When we call a memory allocation function to request a block ofmemory, the function has no idea what type of data we’re planning tostore in the block.

The function returns the memory address in the generic pointer typevoid *.

If the allocation cannot be made, the function returns a null pointer.

It’s the programmer’s responsibility to test the return value of anymemory allocation function and take appropriate action if it’s a nullpointer.

The effect of attempting to access memory through a null pointer isundefined, the program may crash.

Always use sizeof when calculating how much space is needed for anarray.

(Lecture 8) Pointers and arrays November 26, 2019 10 / 19

Page 42: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Dynamic storage allocation

When we call a memory allocation function to request a block ofmemory, the function has no idea what type of data we’re planning tostore in the block.

The function returns the memory address in the generic pointer typevoid *.

If the allocation cannot be made, the function returns a null pointer.

It’s the programmer’s responsibility to test the return value of anymemory allocation function and take appropriate action if it’s a nullpointer.

The effect of attempting to access memory through a null pointer isundefined, the program may crash.

Always use sizeof when calculating how much space is needed for anarray.

(Lecture 8) Pointers and arrays November 26, 2019 10 / 19

Page 43: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Dynamic storage allocation

When we call a memory allocation function to request a block ofmemory, the function has no idea what type of data we’re planning tostore in the block.

The function returns the memory address in the generic pointer typevoid *.

If the allocation cannot be made, the function returns a null pointer.

It’s the programmer’s responsibility to test the return value of anymemory allocation function and take appropriate action if it’s a nullpointer.

The effect of attempting to access memory through a null pointer isundefined, the program may crash.

Always use sizeof when calculating how much space is needed for anarray.

(Lecture 8) Pointers and arrays November 26, 2019 10 / 19

Page 44: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Dynamic storage allocation

When we call a memory allocation function to request a block ofmemory, the function has no idea what type of data we’re planning tostore in the block.

The function returns the memory address in the generic pointer typevoid *.

If the allocation cannot be made, the function returns a null pointer.

It’s the programmer’s responsibility to test the return value of anymemory allocation function and take appropriate action if it’s a nullpointer.

The effect of attempting to access memory through a null pointer isundefined, the program may crash.

Always use sizeof when calculating how much space is needed for anarray.

(Lecture 8) Pointers and arrays November 26, 2019 10 / 19

Page 45: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Dynamic storage allocation

#include <stdlib.h>float *p = malloc (100* sizeof(float));

if (!p){ //if (p == NULL) {// allocation failed; take appropriate action}else {// allocation successful}

if (p){ //if (p != NULL) {// allocation successful}else{// allocation failed; take appropriate action}

(Lecture 8) Pointers and arrays November 26, 2019 11 / 19

Page 46: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Dynamic storage allocation

#include <stdlib.h>float *p = malloc (100* sizeof(float));

if (!p){ //if (p == NULL) {// allocation failed; take appropriate action}else {// allocation successful}

if (p){ //if (p != NULL) {// allocation successful}else{// allocation failed; take appropriate action}

(Lecture 8) Pointers and arrays November 26, 2019 11 / 19

Page 47: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Dynamic storage allocation

After allocating the memory, calloc initializes it by setting all bits to0.

#include <stdlib.h>

double *p = calloc (100, sizeof(double));

if (p){...}

The realloc function can resize the array - void *realloc( void *ptr,size t size);ptr - must point to a memory block obtained by a previous call ofmalloc, calloc or realloc.The size parameter represents the new size of the block, which maybe larger or smaller than the original size.

(Lecture 8) Pointers and arrays November 26, 2019 12 / 19

Page 48: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Dynamic storage allocation

After allocating the memory, calloc initializes it by setting all bits to0.

#include <stdlib.h>

double *p = calloc (100, sizeof(double));

if (p){...}

The realloc function can resize the array - void *realloc( void *ptr,size t size);ptr - must point to a memory block obtained by a previous call ofmalloc, calloc or realloc.The size parameter represents the new size of the block, which maybe larger or smaller than the original size.

(Lecture 8) Pointers and arrays November 26, 2019 12 / 19

Page 49: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Dynamic storage allocation

After allocating the memory, calloc initializes it by setting all bits to0.

#include <stdlib.h>

double *p = calloc (100, sizeof(double));

if (p){...}

The realloc function can resize the array - void *realloc( void *ptr,size t size);

ptr - must point to a memory block obtained by a previous call ofmalloc, calloc or realloc.The size parameter represents the new size of the block, which maybe larger or smaller than the original size.

(Lecture 8) Pointers and arrays November 26, 2019 12 / 19

Page 50: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Dynamic storage allocation

After allocating the memory, calloc initializes it by setting all bits to0.

#include <stdlib.h>

double *p = calloc (100, sizeof(double));

if (p){...}

The realloc function can resize the array - void *realloc( void *ptr,size t size);ptr - must point to a memory block obtained by a previous call ofmalloc, calloc or realloc.

The size parameter represents the new size of the block, which maybe larger or smaller than the original size.

(Lecture 8) Pointers and arrays November 26, 2019 12 / 19

Page 51: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Dynamic storage allocation

After allocating the memory, calloc initializes it by setting all bits to0.

#include <stdlib.h>

double *p = calloc (100, sizeof(double));

if (p){...}

The realloc function can resize the array - void *realloc( void *ptr,size t size);ptr - must point to a memory block obtained by a previous call ofmalloc, calloc or realloc.The size parameter represents the new size of the block, which maybe larger or smaller than the original size.

(Lecture 8) Pointers and arrays November 26, 2019 12 / 19

Page 52: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Dynamic storage allocation

Rules concerning behavior of realloc:

When it expands a memory block, realloc doesn’t initialize the bytesthat are added to the block.If realloc can’t enlarge the memory block as requested, it returns anull pointer, the data in the old memory is unchanged.If realloc is called with a null pointer as its first argument, it behaveslike malloc.If realloc is called with 0 as its second argument, it frees the memoryblock.

Example.

int n=150, b=18;

int *tab = realloc(NULL , n * sizeof(int));tab = realloc(tab , (n - b) * sizeof(int));tab = realloc(tab , (2*n + 7*b) * sizeof(int));tab = realloc(tab , 0);

(Lecture 8) Pointers and arrays November 26, 2019 13 / 19

Page 53: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Dynamic storage allocation

Rules concerning behavior of realloc:When it expands a memory block, realloc doesn’t initialize the bytesthat are added to the block.

If realloc can’t enlarge the memory block as requested, it returns anull pointer, the data in the old memory is unchanged.If realloc is called with a null pointer as its first argument, it behaveslike malloc.If realloc is called with 0 as its second argument, it frees the memoryblock.

Example.

int n=150, b=18;

int *tab = realloc(NULL , n * sizeof(int));tab = realloc(tab , (n - b) * sizeof(int));tab = realloc(tab , (2*n + 7*b) * sizeof(int));tab = realloc(tab , 0);

(Lecture 8) Pointers and arrays November 26, 2019 13 / 19

Page 54: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Dynamic storage allocation

Rules concerning behavior of realloc:When it expands a memory block, realloc doesn’t initialize the bytesthat are added to the block.If realloc can’t enlarge the memory block as requested, it returns anull pointer, the data in the old memory is unchanged.

If realloc is called with a null pointer as its first argument, it behaveslike malloc.If realloc is called with 0 as its second argument, it frees the memoryblock.

Example.

int n=150, b=18;

int *tab = realloc(NULL , n * sizeof(int));tab = realloc(tab , (n - b) * sizeof(int));tab = realloc(tab , (2*n + 7*b) * sizeof(int));tab = realloc(tab , 0);

(Lecture 8) Pointers and arrays November 26, 2019 13 / 19

Page 55: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Dynamic storage allocation

Rules concerning behavior of realloc:When it expands a memory block, realloc doesn’t initialize the bytesthat are added to the block.If realloc can’t enlarge the memory block as requested, it returns anull pointer, the data in the old memory is unchanged.If realloc is called with a null pointer as its first argument, it behaveslike malloc.

If realloc is called with 0 as its second argument, it frees the memoryblock.

Example.

int n=150, b=18;

int *tab = realloc(NULL , n * sizeof(int));tab = realloc(tab , (n - b) * sizeof(int));tab = realloc(tab , (2*n + 7*b) * sizeof(int));tab = realloc(tab , 0);

(Lecture 8) Pointers and arrays November 26, 2019 13 / 19

Page 56: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Dynamic storage allocation

Rules concerning behavior of realloc:When it expands a memory block, realloc doesn’t initialize the bytesthat are added to the block.If realloc can’t enlarge the memory block as requested, it returns anull pointer, the data in the old memory is unchanged.If realloc is called with a null pointer as its first argument, it behaveslike malloc.If realloc is called with 0 as its second argument, it frees the memoryblock.

Example.

int n=150, b=18;

int *tab = realloc(NULL , n * sizeof(int));tab = realloc(tab , (n - b) * sizeof(int));tab = realloc(tab , (2*n + 7*b) * sizeof(int));tab = realloc(tab , 0);

(Lecture 8) Pointers and arrays November 26, 2019 13 / 19

Page 57: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Dynamic storage allocation

Rules concerning behavior of realloc:When it expands a memory block, realloc doesn’t initialize the bytesthat are added to the block.If realloc can’t enlarge the memory block as requested, it returns anull pointer, the data in the old memory is unchanged.If realloc is called with a null pointer as its first argument, it behaveslike malloc.If realloc is called with 0 as its second argument, it frees the memoryblock.

Example.

int n=150, b=18;

int *tab = realloc(NULL , n * sizeof(int));tab = realloc(tab , (n - b) * sizeof(int));tab = realloc(tab , (2*n + 7*b) * sizeof(int));tab = realloc(tab , 0);

(Lecture 8) Pointers and arrays November 26, 2019 13 / 19

Page 58: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Dynamic storage allocation

malloc and the other memory allocation functions obtain memoryblocks from a storage pool known as the heap.

Each C program is responsible for recycling its own garbage by callingthe free function to release unneeded memory.

Using free is easy:

#include <stdlib.h>

int *t = malloc( n * sizeof(int) );...free (t);

Calling free releases the block of memory that t points to.

This block is now available for reuse in subsequent calls of malloc,calloc or realloc.

(Lecture 8) Pointers and arrays November 26, 2019 14 / 19

Page 59: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Dynamic storage allocation

malloc and the other memory allocation functions obtain memoryblocks from a storage pool known as the heap.

Each C program is responsible for recycling its own garbage by callingthe free function to release unneeded memory.

Using free is easy:

#include <stdlib.h>

int *t = malloc( n * sizeof(int) );...free (t);

Calling free releases the block of memory that t points to.

This block is now available for reuse in subsequent calls of malloc,calloc or realloc.

(Lecture 8) Pointers and arrays November 26, 2019 14 / 19

Page 60: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Dynamic storage allocation

malloc and the other memory allocation functions obtain memoryblocks from a storage pool known as the heap.

Each C program is responsible for recycling its own garbage by callingthe free function to release unneeded memory.

Using free is easy:

#include <stdlib.h>

int *t = malloc( n * sizeof(int) );...free (t);

Calling free releases the block of memory that t points to.

This block is now available for reuse in subsequent calls of malloc,calloc or realloc.

(Lecture 8) Pointers and arrays November 26, 2019 14 / 19

Page 61: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Dynamic storage allocation

malloc and the other memory allocation functions obtain memoryblocks from a storage pool known as the heap.

Each C program is responsible for recycling its own garbage by callingthe free function to release unneeded memory.

Using free is easy:

#include <stdlib.h>

int *t = malloc( n * sizeof(int) );...free (t);

Calling free releases the block of memory that t points to.

This block is now available for reuse in subsequent calls of malloc,calloc or realloc.

(Lecture 8) Pointers and arrays November 26, 2019 14 / 19

Page 62: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Dynamic storage allocation

malloc and the other memory allocation functions obtain memoryblocks from a storage pool known as the heap.

Each C program is responsible for recycling its own garbage by callingthe free function to release unneeded memory.

Using free is easy:

#include <stdlib.h>

int *t = malloc( n * sizeof(int) );...free (t);

Calling free releases the block of memory that t points to.

This block is now available for reuse in subsequent calls of malloc,calloc or realloc.

(Lecture 8) Pointers and arrays November 26, 2019 14 / 19

Page 63: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Storage allocation: stack vs. heap

Never return a pointer to an automatic local variable from the stack:

int *BAD_get_tab(int var){int local_tab[SIZE];for(int i=0; i<SIZE; i++)

local_tab[i] = fun(var);return local_tab;

}int main(void){

int *ltab = BAD_get_tab (3);return 0;

}

gcc lcaltab.c -Walllcaltab.c:9:3: warning: function returns address of local variable[-Wreturn-local-addr]return local tab;

(Lecture 8) Pointers and arrays November 26, 2019 15 / 19

Page 64: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Storage allocation: stack vs. heap

Allocation and deallocation of the array on the heap:

int *array_malloc(int var , int n){int *pt;if (pt = malloc(n * sizeof(int)) ){

for(int i=0; i<n; i++)pt[i] = fun(var);

}return pt;

}int main(void){

int *mtab = array_malloc (6, 1000);...free(mtab);return 0;

}

(Lecture 8) Pointers and arrays November 26, 2019 16 / 19

Page 65: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Memory leak

int *p = malloc (100 * sizeof(int));int *q = malloc (100 * sizeof(int));

A program may allocate blocks of memory and then lose track ofthem.

(Lecture 8) Pointers and arrays November 26, 2019 17 / 19

Page 66: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Memory leak

int *p = malloc (100 * sizeof(int));int *q = malloc (100 * sizeof(int));

A program may allocate blocks of memory and then lose track ofthem.

(Lecture 8) Pointers and arrays November 26, 2019 17 / 19

Page 67: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Memory leak

int *p = malloc (100 * sizeof(int));int *q = malloc (100 * sizeof(int));p=q;

After q is assigned to p, both variables now point to the secondmemory block.

There are no pointers to the first memory block, so we’ll never beable to use it.

Garbage - a block of memory that’s no longer accesible to a program.

(Lecture 8) Pointers and arrays November 26, 2019 18 / 19

Page 68: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Memory leak

int *p = malloc (100 * sizeof(int));int *q = malloc (100 * sizeof(int));p=q;

After q is assigned to p, both variables now point to the secondmemory block.

There are no pointers to the first memory block, so we’ll never beable to use it.

Garbage - a block of memory that’s no longer accesible to a program.

(Lecture 8) Pointers and arrays November 26, 2019 18 / 19

Page 69: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Memory leak

int *p = malloc (100 * sizeof(int));int *q = malloc (100 * sizeof(int));p=q;

After q is assigned to p, both variables now point to the secondmemory block.

There are no pointers to the first memory block, so we’ll never beable to use it.

Garbage - a block of memory that’s no longer accesible to a program.

(Lecture 8) Pointers and arrays November 26, 2019 18 / 19

Page 70: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Memory leak

int *p = malloc (100 * sizeof(int));int *q = malloc (100 * sizeof(int));p=q;

After q is assigned to p, both variables now point to the secondmemory block.

There are no pointers to the first memory block, so we’ll never beable to use it.

Garbage - a block of memory that’s no longer accesible to a program.

(Lecture 8) Pointers and arrays November 26, 2019 18 / 19

Page 71: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Memory leak

int *p = malloc (100 * sizeof(int));int *q = malloc (100 * sizeof(int));p=q;

After q is assigned to p, both variables now point to the secondmemory block.

There are no pointers to the first memory block, so we’ll never beable to use it.

Garbage - a block of memory that’s no longer accesible to a program.

(Lecture 8) Pointers and arrays November 26, 2019 18 / 19

Page 72: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Memory leak

A program that leaves garbage behind has a memory leak.

C doesn’t provide a garbage collector that automatically locates andrecycles garbage.

Each C program is responsible for recycling its own garbage by callingthe free function to release unneeded memory.

http://valgrind.org/ - Valgrind is an instrumentation framework forbuilding dynamic analysis tools. There are Valgrind tools that canautomatically detect many memory management bugs.

(Lecture 8) Pointers and arrays November 26, 2019 19 / 19

Page 73: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Memory leak

A program that leaves garbage behind has a memory leak.

C doesn’t provide a garbage collector that automatically locates andrecycles garbage.

Each C program is responsible for recycling its own garbage by callingthe free function to release unneeded memory.

http://valgrind.org/ - Valgrind is an instrumentation framework forbuilding dynamic analysis tools. There are Valgrind tools that canautomatically detect many memory management bugs.

(Lecture 8) Pointers and arrays November 26, 2019 19 / 19

Page 74: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Memory leak

A program that leaves garbage behind has a memory leak.

C doesn’t provide a garbage collector that automatically locates andrecycles garbage.

Each C program is responsible for recycling its own garbage by callingthe free function to release unneeded memory.

http://valgrind.org/ - Valgrind is an instrumentation framework forbuilding dynamic analysis tools. There are Valgrind tools that canautomatically detect many memory management bugs.

(Lecture 8) Pointers and arrays November 26, 2019 19 / 19

Page 75: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Memory leak

A program that leaves garbage behind has a memory leak.

C doesn’t provide a garbage collector that automatically locates andrecycles garbage.

Each C program is responsible for recycling its own garbage by callingthe free function to release unneeded memory.

http://valgrind.org/ - Valgrind is an instrumentation framework forbuilding dynamic analysis tools. There are Valgrind tools that canautomatically detect many memory management bugs.

(Lecture 8) Pointers and arrays November 26, 2019 19 / 19

Page 76: Pointers and arrays - AGH University of Science and Technologygawronski/2019_pp/lecture_08.pdf · (Lecture 8) Pointers and arrays November 26, 20196/19. Pointers - using array name

Memory leak

A program that leaves garbage behind has a memory leak.

C doesn’t provide a garbage collector that automatically locates andrecycles garbage.

Each C program is responsible for recycling its own garbage by callingthe free function to release unneeded memory.

http://valgrind.org/ - Valgrind is an instrumentation framework forbuilding dynamic analysis tools. There are Valgrind tools that canautomatically detect many memory management bugs.

(Lecture 8) Pointers and arrays November 26, 2019 19 / 19