Exercise 11

52
Exercise 11 Dynamic allocation and structures

description

Exercise 11. Dynamic allocation and structures. Dynamic Allocation. Array variables have fixed size, used to store a fixed and known amount of variables – known at the time of compilation This size can’t be changed after compilation - PowerPoint PPT Presentation

Transcript of Exercise 11

Page 1: Exercise 11

Exercise 11

Dynamic allocation and structures

Page 2: Exercise 11

Dynamic Allocation

Array variables have fixed size, used to store a fixed and known amount of variables – known at the time of compilation

This size can’t be changed after compilation However, we don’t always know in advance

how much space we would need for an array or a variable

We would like to be able to dynamically allocate memory

Page 3: Exercise 11

The malloc function

void * malloc(unsigned int nBytes);

The function malloc is used to dynamically allocate nBytes in memory

malloc returns a pointer to the allocated area on success, NULL on failure

You should always check whether memory was successfully allocated

Remember to #include <stdlib.h>

Page 4: Exercise 11

Example

dynamic_reverse_array.c

Page 5: Exercise 11

Why casting?

The casting in y = (int *)malloc(n*sizeof(int));

is needed because malloc returns void * :void * malloc(unsigned int nbytes);

The type (void *) specifies a general pointer, which can be cast to any pointer type.

Page 6: Exercise 11

What is this ‘sizeof’ ?

The sizeof operator gets a variable or a type as an input and outputs its size in bytes:

double x;s1=sizeof(x); /* s1 is 8 */s2=sizeof(int) /* s2 is 4 */

Page 7: Exercise 11

Free the allocated memory segment

void free(void *ptr);

We use free(p) to free the allocated memory pointed to by p

If p doesn’t point to an area allocated by malloc, a run-time error occurs

Always remember to free the allocated memory once you don’t need it anymore

Otherwise, you may run out of memory before you know it!

Page 8: Exercise 11

Another example

another_strcpy.c

Page 9: Exercise 11

Exercise

Implement the function my_strcat : Input – two strings, s1 and s2 Output – a pointer to a dynamically allocated

concatenation (‘shirshur’) For example: The concatenation of “hello_” and

“world!” is the string “hello_world!”

Write a program that accepts two strings from the user and prints their concatenation Assume input strings are no longer than a 100

chars

Page 10: Exercise 11

Solution

my_strcat.c (my_strcat2.c)

Page 11: Exercise 11

What’s wrong with this?

char *my_strcat(char *str1, char *str2){

int len;char result[500]; /* Let’s assume this is large

enough */

len = strlen(str1);

strcpy(result, str1);strcpy(result+len, str2);

return result;}

Page 12: Exercise 11

Structures

Often we want to be able to manipulate ‘logical entities’ as a whole For example, complex numbers, dates,

student records, etc’ Each of these must be composed of more

than one variable, but are logically units

Page 13: Exercise 11

Structures

A struct (short for structure) is a collection of variables of different types, gathered into one super-variable

It is used to define more complex data types

Variables in a struct are called members or fields

Page 14: Exercise 11

Example – complex numbers.

The following is the definition of a new ‘variable’ of type complex number:

struct complex { int real; int img; };

Once we define a structure, we can treat it as any type. In a program, we can then write:

struct complex num1, num2, num3;

Page 15: Exercise 11

Access structure members

If A is a variable of some structure type with a member named x, then A.x is that member of A struct complex C;

C.real = 0;

If pA is a pointer to a structure with a member x, then pA->x is that member of the variable pointed by pA. This is simply shorthand for (*pA).x

struct complex *pc = &C;

pc->real = 1;

Page 16: Exercise 11

A more convenient usage with typedef

An alternative definition:typedef struct complex_t {

int real; int img; } complex;

Now the program has a new variable type - “complex”. This way we don’t have to write “struct complex” every

time! For example, we can define two complex numbers in the

following line:complex num1, num2;

Page 17: Exercise 11

Examples

AddComplex.c

IncDate.c

Page 18: Exercise 11

AddComplex – step by step

complex a, b, c;

printf(“…");scanf("%lf%lf",&(a.real),&(a.img));printf(“…");scanf("%lf%lf",&(b.real),&(b.img));

c = AddComp(a,b);

printf(“result = %g+%gi\n",c.real,c.img);return 0;

… …

real

img

a

… …

real

img

b

… …

real

img

c

Page 19: Exercise 11

AddComplex – step by step

complex a, b, c;

printf(“…");scanf("%lf%lf",&(a.real),&(a.img));printf(“…");scanf("%lf%lf",&(b.real),&(b.img));

c = AddComp(a,b);

printf(“result = %g+%gi\n",c.real,c.img);return 0;

… …

real

img

a

… …

real

img

b

… …

real

img

c

Page 20: Exercise 11

AddComplex – step by step

complex a, b, c;

printf(“…");scanf("%lf%lf",&(a.real),&(a.img));printf(“…");scanf("%lf%lf",&(b.real),&(b.img));

c = AddComp(a,b);

printf(“result = %g+%gi\n",c.real,c.img);return 0;

1.0 2.0

real

img

a

… …

real

img

b

… …

real

img

c

Page 21: Exercise 11

AddComplex – step by step

complex a, b, c;

printf(“…");scanf("%lf%lf",&(a.real),&(a.img));printf(“…");scanf("%lf%lf",&(b.real),&(b.img));

c = AddComp(a,b);

printf(“result = %g+%gi\n",c.real,c.img);return 0;

1.0 2.0

real

img

a

… …

real

img

b

… …

real

img

c

Page 22: Exercise 11

AddComplex – step by step

complex a, b, c;

printf(“…");scanf("%lf%lf",&(a.real),&(a.img));printf(“…");scanf("%lf%lf",&(b.real),&(b.img));

c = AddComp(a,b);

printf(“result = %g+%gi\n",c.real,c.img);return 0;

1.0 2.0

real

img

a

3.0 4.0

real

img

b

… …

real

img

c

Page 23: Exercise 11

AddComplex – step by step

complex a, b, c;

printf(“…");scanf("%lf%lf",&(a.real),&(a.img));printf(“…");scanf("%lf%lf",&(b.real),&(b.img));

c = AddComp(a,b);

printf(“result = %g+%gi\n",c.real,c.img);return 0;

1.0 2.0

real

img

a

3.0 4.0

real

img

b

… …

real

img

c

Page 24: Exercise 11

AddComplex – step by step

complex AddComp(complex x, complex y){ complex z;

z.real = x.real + y.real; z.img = x.img + y.img;

return z;}

1.0 2.0

real

img

x

3.0 4.0

real

img

y

… …

real

img

z

Page 25: Exercise 11

AddComplex – step by step

complex AddComp(complex x, complex y){ complex z;

z.real = x.real + y.real; z.img = x.img + y.img;

return z;}

1.0 2.0

real

img

x

3.0 4.0

real

img

y

… 6.0

real

img

z

Page 26: Exercise 11

AddComplex – step by step

complex AddComp(complex x, complex y){ complex z;

z.real = x.real + y.real; z.img = x.img + y.img;

return z;}

1.0 2.0

real

img

x

3.0 4.0

real

img

y

4.0 6.0

real

img

z

Page 27: Exercise 11

AddComplex – step by step

complex AddComp(complex x, complex y){ complex z;

z.real = x.real + y.real; z.img = x.img + y.img;

return z;}

1.0 2.0

real

img

x

3.0 4.0

real

img

y

4.0 6.0

real

img

z

Page 28: Exercise 11

AddComplex – step by step

complex a, b, c;

printf(“…");scanf("%lf%lf",&(a.real),&(a.img));printf(“…");scanf("%lf%lf",&(b.real),&(b.img));

c = AddComp(a,b);

printf(“result = %g+%gi\n",c.real,c.img);return 0;

1.0 2.0

real

img

a

3.0 4.0

real

img

b

4.0 6.0

real

img

c

Page 29: Exercise 11

AddComplex – step by step

complex a, b, c;

printf(“…");scanf("%lf%lf",&(a.real),&(a.img));printf(“…");scanf("%lf%lf",&(b.real),&(b.img));

c = AddComp(a,b);

printf(“result = %g+%gi\n",c.real,c.img);return 0;

1.0 2.0

real

img

a

3.0 4.0

real

img

b

4.0 6.0

real

img

c

Page 30: Exercise 11

Miscellaneous structure trivia

Structure members may be ordinary variable types, but also other structures and even arrays!

Structures can therefore be rather large and take up a lot of space

Many times we prefer to pass structures to functions by address, and not by value Thus a new copy of the structure is not created –

just a pointer to the existing structure

Page 31: Exercise 11

More trivia

Structures cannot be compared using the == operator They must be compared member by

member Usually this will be done in a separate

function Structures can be copied using the =

operator Member-wise copy

Page 32: Exercise 11

Example

Is_In_Circle.c

Page 33: Exercise 11

Is_in_circle – step by step

printf(“Enter dot\n");scanf("%lf%lf",&d.x,&d.y);printf("Enter circle center\n");scanf("%lf%lf",&c.center.x,&c.center.y);printf("Enter circle radius\n");scanf("%lf",&c.radius);

if (IsInCircle(&d, &c))printf("dot is in circle\n");

elseprintf("dot is out of circle\n");

… …

yx

d (dot)

c (circle)

… …

yx

center (dot) radiu

s…

Page 34: Exercise 11

Is_in_circle – step by step

printf(“Enter dot\n");scanf("%lf%lf",&d.x,&d.y);printf("Enter circle center\n");scanf("%lf%lf",&c.center.x,&c.center.y);printf("Enter circle radius\n");scanf("%lf",&c.radius);

if (IsInCircle(&d, &c))printf("dot is in circle\n");

elseprintf("dot is out of circle\n");

… …

yx

d (dot)

c (circle)

… …

yx

center (dot) radiu

s…

Page 35: Exercise 11

Is_in_circle – step by step

printf(“Enter dot\n");scanf("%lf%lf",&d.x,&d.y);printf("Enter circle center\n");scanf("%lf%lf",&c.center.x,&c.center.y);printf("Enter circle radius\n");scanf("%lf",&c.radius);

if (IsInCircle(&d, &c))printf("dot is in circle\n");

elseprintf("dot is out of circle\n");

1.0 2.0

yx

d (dot)

c (circle)

… …

yx

center (dot) radiu

s…

Page 36: Exercise 11

Is_in_circle – step by step

printf(“Enter dot\n");scanf("%lf%lf",&d.x,&d.y);printf("Enter circle center\n");scanf("%lf%lf",&c.center.x,&c.center.y);printf("Enter circle radius\n");scanf("%lf",&c.radius);

if (IsInCircle(&d, &c))printf("dot is in circle\n");

elseprintf("dot is out of circle\n");

1.0 2.0

yx

d (dot)

c (circle)

… …

yx

center (dot) radiu

s…

Page 37: Exercise 11

Is_in_circle – step by step

printf(“Enter dot\n");scanf("%lf%lf",&d.x,&d.y);printf("Enter circle center\n");scanf("%lf%lf",&c.center.x,&c.center.y);printf("Enter circle radius\n");scanf("%lf",&c.radius);

if (IsInCircle(&d, &c))printf("dot is in circle\n");

elseprintf("dot is out of circle\n");

1.0 2.0

yx

d (dot)

c (circle)

0.0 0.0

yx

center (dot) radiu

s…

Page 38: Exercise 11

Is_in_circle – step by step

printf(“Enter dot\n");scanf("%lf%lf",&d.x,&d.y);printf("Enter circle center\n");scanf("%lf%lf",&c.center.x,&c.center.y);printf("Enter circle radius\n");scanf("%lf",&c.radius);

if (IsInCircle(&d, &c))printf("dot is in circle\n");

elseprintf("dot is out of circle\n");

1.0 2.0

yx

d (dot)

c (circle)

0.0 0.0

yx

center (dot) radiu

s…

Page 39: Exercise 11

Is_in_circle – step by step

printf(“Enter dot\n");scanf("%lf%lf",&d.x,&d.y);printf("Enter circle center\n");scanf("%lf%lf",&c.center.x,&c.center.y);printf("Enter circle radius\n");scanf("%lf",&c.radius);

if (IsInCircle(&d, &c))printf("dot is in circle\n");

elseprintf("dot is out of circle\n");

1.0 2.0

yx

d (dot)

c (circle)

0.0 0.0

yx

center (dot) radiu

s5

Page 40: Exercise 11

Is_in_circle – step by step

printf(“Enter dot\n");scanf("%lf%lf",&d.x,&d.y);printf("Enter circle center\n");scanf("%lf%lf",&c.center.x,&c.center.y);printf("Enter circle radius\n");scanf("%lf",&c.radius);

if (IsInCircle(&d, &c))printf("dot is in circle\n");

elseprintf("dot is out of circle\n");

1.0 2.0

yx

d (dot)

c (circle)

0.0 0.0

yx

center (dot) radiu

s5

Page 41: Exercise 11

Is_in_circle – step by step

int IsInCircle(dot *p_dot, circle *p_circle){ double x_dist,y_dist;

x_dist = p_dot->x - p_circle->center.x; y_dist = p_dot->y - p_circle->center.y; if (x_dist*x_dist + y_dist*y_dist <= p_circle->radius*p_circle->radius) return 1;

return 0;}

1.0 2.0

yx

(dot)

(circle)

0.0 0.0

yx

center (dot) radiu

s5

x_dist

y_dist… …

p_circle

p_dot1024756

Page 42: Exercise 11

Is_in_circle – step by step

int IsInCircle(dot *p_dot, circle *p_circle){ double x_dist,y_dist;

x_dist = p_dot->x - p_circle->center.x; y_dist = p_dot->y - p_circle->center.y; if (x_dist*x_dist + y_dist*y_dist <= p_circle->radius*p_circle->radius) return 1;

return 0;}

1.0 2.0

yx

(dot)

(circle)

0.0 0.0

yx

center (dot) radiu

s5

x_dist

y_dist1.0 …

p_circle

p_dot1024756

Page 43: Exercise 11

Is_in_circle – step by step

int IsInCircle(dot *p_dot, circle *p_circle){ double x_dist,y_dist;

x_dist = p_dot->x - p_circle->center.x; y_dist = p_dot->y - p_circle->center.y; if (x_dist*x_dist + y_dist*y_dist <= p_circle->radius*p_circle->radius) return 1;

return 0;}

1.0 2.0

yx

(dot)

(circle)

0.0 0.0

yx

center (dot) radiu

s5

x_dist

y_dist1.0 2.0

p_circle

p_dot1024756

Page 44: Exercise 11

Is_in_circle – step by step

int IsInCircle(dot *p_dot, circle *p_circle){ double x_dist,y_dist;

x_dist = p_dot->x - p_circle->center.x; y_dist = p_dot->y - p_circle->center.y; if (x_dist*x_dist + y_dist*y_dist <= p_circle->radius*p_circle->radius) return 1;

return 0;}

1.0 2.0

yx

(dot)

(circle)

0.0 0.0

yx

center (dot) radiu

s5

x_dist

y_dist1.0 2.0

p_circle

p_dot1024756

Page 45: Exercise 11

Is_in_circle – step by step

int IsInCircle(dot *p_dot, circle *p_circle){ double x_dist,y_dist;

x_dist = p_dot->x - p_circle->center.x; y_dist = p_dot->y - p_circle->center.y; if (x_dist*x_dist + y_dist*y_dist <= p_circle->radius*p_circle->radius) return 1;

return 0;}

1.0 2.0

yx

(dot)

(circle)

0.0 0.0

yx

center (dot) radiu

s5

x_dist

y_dist1.0 2.0

p_circle

p_dot1024756

Page 46: Exercise 11

Is_in_circle – step by step

printf(“Enter dot\n");scanf("%lf%lf",&d.x,&d.y);printf("Enter circle center\n");scanf("%lf%lf",&c.center.x,&c.center.y);printf("Enter circle radius\n");scanf("%lf",&c.radius);

if (IsInCircle(&d, &c))printf("dot is in circle\n");

elseprintf("dot is out of circle\n");

1.0 2.0

yx

d (dot)

c (circle)

0.0 0.0

yx

center (dot) radiu

s5

Page 47: Exercise 11

Exercise

Implement the MultComplex function: Input – two complex numbers Output – their multiplication Note: if x=a+ib and y=c+id then:

z = xy = (ac-bd)+i(ad+bc)

Write a program that uses the above function to multiply two complex numbers given by the user

Page 48: Exercise 11

Solution

MultiplyComplex.c

Page 49: Exercise 11

Exiting the program

void exit(int status); Sometimes an error occurs and we want the

program to immediately exit The exit function closes all open files, frees

all allocated memory, and exits the program Equivalent to calling ‘return’ within main Remember to #include <stdlib.h> See strcpy_with_exit.c

Page 50: Exercise 11

Structures containing arrays

A structure member that is an array does not ‘behave’ like an ordinary array

When copying a structure that contains a member which is an array, the array is copied element by element Not just the address gets copied For example - array_member.c

Reminder – ordinary arrays can’t be copied simply by using the ‘=‘ operator They must be copied using a loop

Page 51: Exercise 11

Structures containing arrays

When passing the structure to a function by value Changing the array field inside the function won’t

change it in the calling function

Reminder – when passing an ordinary array to a function, all that gets passed is the address of its first element Hence every change to the array within the function,

changes the array in the calling function

Page 52: Exercise 11

Pointers are another matter

If the member is a pointer, for example to a dynamically allocated array, all that gets copied is the pointer (the address) itself For example, pointer_member.c

Hence, we should take extra care when manipulating structures that contain pointers