Download presentation
Presentation is loading. Please wait.
1
1 Structures, Dynamic Memory Allocation
2
2 Agenda Structures Definition & usage Pointers to structures Arrays and pointers in structures Dynamic Memory Allocation
3
3 Structures ‘Logical entities’ Examples: complex numbers, dates, student records, geometric objects, etc’ Each is composed of a number of variables
4
4 Structures struct: collection of variables, gathered into one variable Defines new data types Memory Variables in a struct are called members or fields
5
5 Example – complex numbers Definition of a new ‘type’ that represents a complex number: struct complex { int real; int img; }; Once we define a structure, we can use it as any type: struct complex num1, num2, num3;
6
6 Access structure members If A is of some structure with a member named x, then A.x is that member of A struct complex C; C.real = 0; If A is a pointer to a structure with a member x, then A->x is that member of the variable pointed by A (same as (*A).x) struct complex *pc = &C; pc->real = 1;
7
7 Convenient usage with typedef typedef struct complex_t { int real; int img; } complex; A new variable type: “complex” Saves writing “struct complex” every time! Usage: complex num1, num2;
8
8 Examples (AddComplex.c) complex AddComp (complex x, complex y) { complex z; z.real = x.real + y.real; z.img = x.img + y.img; return z; } Structures are passed to functions “by value” A copy of the structure is passed
9
9 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; …… realimg a …… realimg b …… realimg c
10
10 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; …… realimg a …… realimg b …… realimg c
11
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.02.0 realimg a …… realimg b …… realimg c
12
12 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.02.0 realimg a …… realimg b …… realimg c
13
13 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.02.0 realimg a 3.04.0 realimg b …… realimg c
14
14 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.02.0 realimg a 3.04.0 realimg b …… realimg c
15
15 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.02.0 realimg x 3.04.0 realimg y …… realimg z
16
16 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.02.0 realimg x 3.04.0 realimg y …6.0 realimg z
17
17 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.02.0 realimg x 3.04.0 realimg y 4.06.0 realimg z
18
18 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.02.0 realimg x 3.04.0 realimg y 4.06.0 realimg z
19
19 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.02.0 realimg a 3.04.0 realimg b 4.06.0 realimg c
20
20 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.02.0 realimg a 3.04.0 realimg b 4.06.0 realimg c
21
21 Exercise Implement the MultComplex function – Input - two complex numbers Output – their multiplication Definition: 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
22
22 Solution (MultiplyComplex.c) complex MultiplyComp(complex a, complex b) { complex c; c.real = a.real*b.real - a.img*b.img; c.img = a.real*b.img + a.img*b.real; return c; }
23
23 More on Structures Structure members: ordinary variable types, structures, arrays Passing structures to functions by address A copy of the structure is not created – just a pointer to the existing structure
24
24 More on Structures 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
25
25 Example (Is_In_Circle.c) 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) radius * p_circle->radius)) return 1; return 0; }
26
26 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"); else printf("dot is out of circle\n"); …… yx d (dot)c (circle) …… yx center (dot) radius …
27
27 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"); else printf("dot is out of circle\n"); …… yx d (dot)c (circle) …… yx center (dot) radius …
28
28 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"); else printf("dot is out of circle\n"); 1.02.0 yx d (dot)c (circle) …… yx center (dot) radius …
29
29 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"); else printf("dot is out of circle\n"); 1.02.0 yx d (dot)c (circle) …… yx center (dot) radius …
30
30 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"); else printf("dot is out of circle\n"); 1.02.0 yx d (dot)c (circle) 0.0 yx center (dot) radius …
31
31 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"); else printf("dot is out of circle\n"); 1.02.0 yx d (dot)c (circle) 0.0 yx center (dot) radius …
32
32 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"); else printf("dot is out of circle\n"); 1.02.0 yx d (dot)c (circle) 0.0 yx center (dot) radius 5
33
33 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"); else printf("dot is out of circle\n"); 1.02.0 yx d (dot)c (circle) 0.0 yx center (dot) radius 5
34
34 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 radius*p_circle->radius) return 1; return 0; } 1.02.0 yx (dot)(circle) 0.0 yx center (dot) radius 5 x_disty_dist …… p_circlep_dot 1024756
35
35 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 radius*p_circle->radius) return 1; return 0; } 1.02.0 yx (dot)(circle) 0.0 yx center (dot) radius 5 x_disty_dist 1.0… p_circlep_dot 1024756
36
36 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 radius*p_circle->radius) return 1; return 0; } 1.02.0 yx (dot)(circle) 0.0 yx center (dot) radius 5 x_disty_dist 1.02.0 p_circlep_dot 1024756
37
37 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 radius*p_circle->radius) return 1; return 0; } 1.02.0 yx (dot)(circle) 0.0 yx center (dot) radius 5 x_disty_dist 1.02.0 p_circlep_dot 1024756
38
38 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 radius*p_circle->radius) return 1; return 0; } 1.02.0 yx (dot)(circle) 0.0 yx center (dot) radius 5 x_disty_dist 1.02.0 p_circlep_dot 1024756
39
39 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"); else printf("dot is out of circle\n"); 1.02.0 yx d (dot)c (circle) 0.0 yx center (dot) radius 5
40
40 Exercise Write a struct that represents a date (day, month, year) Write a function that increments the date void IncDate(Date *d); For example – 31.12.05 -> 1.1.06
41
41 Solution IncDate.c
42
42 Structures containing arrays A structure member that is an array does not ‘behave’ like an ordinary array ‘=‘: the array is copied element by element It is not the address that 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
43
43 Structures containing arrays Same behavior when passing the structure to a function Changing the array 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
44
44 Pointers are another matter If the member is a pointer all that gets copied is the pointer (the address) itself For example, pointer_member.c Make sure that you understand what you do!
45
45 Dynamic Memory Allocation
46
46 Dynamic Memory Allocation: Motivation Array variables have fixed size (e.g.: int – 4 bytes, char – 1 byte) This can’t be changed after compilation It is not always known how many elements we will need in runtime We would like to be able to dynamically allocate memory
47
47 The malloc function void *malloc(unsigned int n); The function malloc is used to dynamically allocate n bytes malloc returns a pointer to the allocated area on success, NULL on failure
48
48 The malloc function void *malloc(unsigned int n); We should always check whether memory was successfully allocated Remember to #include Allocated memory must be freed (later)
49
49 Usage Example int n, *p; printf("How many numbers do you want to enter?\n"); scanf("%d",&n); p = (int *)malloc(n*sizeof(int)); if(p == NULL) { printf("Memory allocation failed!\n"); return 1; } free(p);
50
50 Why casting? The casting in p=(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.
51
51 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 */
52
52 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
53
53 Free the allocated memory segment void free(void *ptr); Always remember to free the allocated memory once you don’t need it Otherwise, you may run out of memory – a common bug that is hard to detect
54
54 Example dynamic_reverse_array.c
55
55 Example (dynamic_reverse_array.c) int i, n, *p; printf("How many numbers do you want to enter?\n"); scanf("%d",&n); p = (int *)malloc(n*sizeof(int)); if(p == NULL) { printf("Memory allocation failed!\n"); return 1; } printf("Please enter numbers now:\n"); for(i=0; i<n; i++) scanf("%d", &p[i]); printf("The numbers in reverse order are - \n"); for(i=n-1; i>=0; i--) printf("%d ",p[i]); free(p);
56
56 Allocating memory within a function Dynamic allocation of memory is not deleted when we leave the scope / exit a function This is why we need to free it Now we are able to allocate memory within a function and use it outside
57
57 Example (another_strcpy.c) char *another_strcpy(char *src) { char *dst; int len, i; len=strlen(src); dst=(char*)malloc(sizeof(char)*(len+1)); if(dst == NULL) { printf("Memory allocation failed!\n"); return NULL; } for(i=0;i<=len;i++) dst[i] = src[i]; return dst; }
58
58 Exercise (@ home) 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
59
59 Solution my_strcat.c (my_strcat2.c)
60
60 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; }
61
61 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 See strcpy_with_exit.c
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.