1 / 54

Programming

Programming. Structures and Dynamic Allocation. Dynamic Memory Allocation. The memory requirement of our program is not always known in advance Arrays have fixed size determined at compile time We would like to allocate at runtime. The Memory. Two Parts: Stack

hova
Télécharger la présentation

Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Structures andDynamic Allocation

  2. Dynamic Memory Allocation • The memory requirement of our program is not always known in advance • Arrays have fixed size determined at compile time • We would like to allocate at runtime

  3. The Memory • Two Parts: • Stack • Memory is allocated when entering a function • Deallocation when the function terminates • Heap • Programmer control allocation and deallocation

  4. Memory Management • stdlib.h • Two operations for memory management • Allocation • void* malloc(…); • Deallocation • void free(…);

  5. The malloc Function void* malloc(unsignedint nBytes); • 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>

  6. The free Function void free(void *ptr); • Use free(p) to deallocate memory pointed by p • If p doesn’t point to an area allocated by malloc an error occurs • No partial deallocation • Always remember to free the allocated memory once you are done with it

  7. Example int main(void) { int i, n, *p; printf("How many numbers will you enter?\n"); scanf("%d", &n); /* Allocate an int array of the proper size */ p = (int*)malloc(n * sizeof(int)); if (p == NULL) { printf("Memory allocation failed!\n"); return 1; } ... /* Free the allocated space */ free(p); return 0; }

  8. Why Casting? The casting in p = (int *)malloc(n*sizeof(int)); is needed becausemallocreturnsvoid* : void* malloc(unsigned int nbytes); The type (void*) specifies a general pointer, which can be cast to any pointer type.

  9. What is ‘sizeof’ ? • The sizeof operator gets a variable or a type as an input and return its size in bytes • sizeofalways calculates the size of the type double x; s1 = sizeof(x); /* s1 is 8 */ s2 = sizeof(int) /* s2 is 4 */

  10. Example char name[25];char* address = (char*)malloc(25);int i; printf("sizeof(name) = %d\n", sizeof(name)); printf("sizeof(address) = %d\n", sizeof(address)); if (sizeof(&i) == sizeof(address)) printf("True\n"); else printf("False\n"); 254True

  11. Returning Allocated Memory • Since allocated memory is not deallocated upon function termination we can return its address. • Duplicate a string char* strdup(const char* str){char* dup = (char*)malloc((strlen(str)+1) * sizeof(char));if (dup != NULL) { strcpy(dup, str); }return dup;}

  12. Exercise • Implement the functionchar* my_strcat(const char *s1,const char *s2); • Output – a pointer to a dynamically allocated concatenation of s1 and s2 • For example: The concatenation of “hello_” and “world!” is the string “hello_world!” • Test your function

  13. Solution char* my_strcat(const char *s1, const char *s2) { int len;char *result = NULL; len = strlen(s1) + strlen(s2) + 1; result = (char*)malloc(len * sizeof(char)); if (result == NULL) { printf(“Memory allocation failed!\n"); return NULL; } strcpy(result, s1); strcat(result, s2); return result; }

  14. What’s Wrong Here? char* my_strcat(const char *s1, const char *s2) { int len; char result[500]; /* assume this is enough */ strcpy(result, s1); strcpy(result + strlen(s1), s2); return result; }

  15. Pitfalls • Accessing un-initialized pointerint* p; *p = 3; • Using de-allocated memoryint* p = (int*)malloc(SIZE * sizeof(int)); ... /* do something with p*/free(p); *p = 0;

  16. Structures • Not much in the real world comes to us as simple data types. (e.g. Car, Book, Bank account) • We want to be able to manipulate logical entities as a whole • The individual pieces of the data may exist in one of the basic forms • How do you keep track of what pieces of the data belong together?

  17. Examples • Book • Title, Authors, ISBN, Publisher • Bank Account • Owners, Balance, Credit • Car • Make, Model, Year

  18. Structures - User Defined Types • A convenient way of grouping several pieces of related information together. • A collection of variables under a single name.

  19. Structures - Example struct point{double x;double y;}; struct rectangle{ struct point low_left; struct point up_right;}; struct student{ char id[10]; char* name; int avg_grade; }; struct point{double x, y;};

  20. Defining a struct struct struct-name{ type field-name1; type field-name2; type field-name3; ...};

  21. Example - Complex Numbers • Structure declaration • Variable definition • Define variables of type complex struct complex {double real, img;}; struct complex c1, c2;

  22. Field Access • Use the . (dot) operator to access a field in a structure<variable name>.<field name> • If comp is of type complex then to access the real part we writecomp.realand to access the imaginary part we write comp.img

  23. Example • Printing a complex number printf("%g + %gi", comp.real, comp.img); • Input from the user scanf("%lf", &comp.real);

  24. Typdef • Introduce synonyms for typestypedefint Boolean;typedefchar* String;Boolean b;String str = "Text";

  25. Typdef and Structs • Instead of writing struct complexeverywhere we can create a shorter synonym using typedef. typdef struct complex Complex • Then use it Complex c1, c2;

  26. TypedefEven Shorter • We can combine the typdef with the structure declaration: typedef structcomplex { int real; int img; } Complex; • This is the common way to define new types

  27. Structures and Functions • Function can receive structures as their parameters and return structures as their return value • Parameter passing is done by value • copy • Similar to basic types

  28. Structures and Functions - Example Complex make_complex(double real, double img){ Complex temp; temp.real = real; temp.img = img; return temp; } Complex add_complex(Complex c1, Complex c2) { return make_comlex(c1.real + c2.real, c1.img + c2.img); }

  29. b a img img real real … … … … add_complex – step by step int main(void){ Complex a, b, sum; printf("…"); scanf("%lf%lf", &(a.real), &(a.img)); printf("…"); scanf("%lf%lf", &(b.real), &(b.img)); sum = add_complex(a, b); printf("result = %g+%gi\n", sum.real, sum.img); return 0;} sum img real … …

  30. b a img img real real … … … … add_complex – step by step int main(void){ Complex a, b, sum; printf("…"); scanf("%lf%lf", &(a.real), &(a.img)); printf("…"); scanf("%lf%lf", &(b.real), &(b.img)); sum = add_complex(a, b); printf("result = %g+%gi\n", sum.real, sum.img); return 0;} sum img real … …

  31. b a img img real real 1.0 … 2.0 … add_complex – step by step int main(void){ Complex a, b, sum; printf("…"); scanf("%lf%lf", &(a.real), &(a.img)); printf("…"); scanf("%lf%lf", &(b.real), &(b.img)); sum = add_complex(a, b); printf("result = %g+%gi\n", sum.real, sum.img); return 0;} sum img real … …

  32. b a img img real real 1.0 … 2.0 … add_complex – step by step int main(void){ Complex a, b, sum; printf("…"); scanf("%lf%lf", &(a.real), &(a.img)); printf("…"); scanf("%lf%lf", &(b.real), &(b.img)); sum = add_complex(a, b); printf("result = %g+%gi\n", sum.real, sum.img); return 0;} sum img real … …

  33. b a img img real real 1.0 3.2 2.0 4.5 add_complex – step by step int main(void){ Complex a, b, sum; printf("…"); scanf("%lf%lf", &(a.real), &(a.img)); printf("…"); scanf("%lf%lf", &(b.real), &(b.img)); sum = add_complex(a, b); printf("result = %g+%gi\n", sum.real, sum.img); return 0;} sum img real … …

  34. b a img img real real 1.0 3.2 2.0 4.5 add_complex – step by step int main(void){ Complex a, b, sum; printf("…"); scanf("%lf%lf", &(a.real), &(a.img)); printf("…"); scanf("%lf%lf", &(b.real), &(b.img)); sum = add_complex(a, b); printf("result = %g+%gi\n", sum.real, sum.img); return 0;} sum img real … …

  35. y x img img real real 1.0 3.2 2.0 4.5 add_complex – step by step Complex add_complex(Complex x, Complex y) { Complex sum; sum.real = x.real + y.real; sum.img = x.img + y.img; return sum; } sum img real … …

  36. y x img img real real 1.0 3.2 2.0 4.5 add_complex – step by step Complex add_complex(Complex x, Complex y) { Complex sum; sum.real = x.real + y.real; sum.img = x.img + y.img; return sum; } sum img real … 6.5

  37. y x img img real real 1.0 3.2 2.0 4.5 add_complex – step by step Complex add_complex(Complex x, Complex y) { Complex sum; sum.real = x.real + y.real; sum.img = x.img + y.img; return sum; } sum img real 4.2 6.5

  38. y x img img real real 1.0 3.2 2.0 4.5 add_complex – step by step Complex add_complex(Complex x, Complex y) { Complex sum; sum.real = x.real + y.real; sum.img = x.img + y.img; return sum; } sum img real 4.2 6.5

  39. b a img img real real 1.0 3.2 2.0 4.5 add_complex – step by step int main(void){ Complex a, b, sum; printf("…"); scanf("%lf%lf", &(a.real), &(a.img)); printf("…"); scanf("%lf%lf", &(b.real), &(b.img)); sum = add_complex(a, b); printf(“result = %g+%gi\n", sum.real, sum.img); return 0;} sum img real 4.2 6.5

  40. b a img img real real 1.0 3.2 2.0 4.5 add_complex – step by step int main(void){ Complex a, b, sum; printf("…"); scanf("%lf%lf", &(a.real), &(a.img)); printf("…"); scanf("%lf%lf", &(b.real), &(b.img)); sum = add_complex(a, b); printf(“result = %g+%gi\n", sum.real, sum.img); return 0;} sum img real 4.2 6.5

  41. b a img img real real 1.0 3.2 2.0 4.5 add_complex – step by step int main(void){ Complex a, b, sum; printf("…"); scanf("%lf%lf", &(a.real), &(a.img)); printf("…"); scanf("%lf%lf", &(b.real), &(b.img)); sum = add_complex(a, b); printf(“result = %g+%gi\n", sum.real, sum.img); return 0;} sum img real 4.2 6.5

  42. Assignment • Structures can be assigned (copied) using the assignment operator “=” • Bitwise copy – copying the content of one structure’s memory onto another • c1 = add_complex(…); 0000000000000000000000000000000000000000 1001001110101010010101010101110101110101 1001001110101010010101010101110101110101 c1 c2

  43. Arrays in Structs • The entire array is part of the structure • When passing the struct to a function (by value) • Changing the array field won’t change the original array

  44. Pointers in Structs • When copying a struct containing a pointer only the pointer is copied (shallow copy) • Not what the pointer points to • we should take extra carewhen manipulating structures that contain pointers

  45. Comparison • Structures cannot be compared using the equality operator “==” • They must be compared member by member • Usually this will be done in a separate function is_equal_complex(Complex c1, Complex c2){return (c1.real == c2.real) && (c1.img == c2.img);}

  46. Exercise • Implement a multiply_complex function:Complex multiply_complex(Complex x, Complex y); • Note: if x = a + bi and y = c + di then: z = xy = (ac - bd) + (ad + bc)i • Write a program that uses the above function to multiply two complex numbers given by the user

  47. Solution Complex make_complex(double real, double img){ Complex temp; temp.real = real; temp.img = img;return temp;}Complex multiply_complex(Complex a, Complex b){return make_complex(a.real * b.real - a.img * b.img, a.real * b.img + a.img * b.real);}

  48. Pointers to Structures • Same as with other types • Pointer definition • structure-name * variable-name; • Complex* comptr; defines a complex and a pointer to a complex Complex c, *cptr;cptr = &c; assign the address of the complex variable to the complex pointer

  49. Accessing Fields Using Pointers Complex comp;Complex *pcomp = &comp; • To access the fields we can write:(*pcomp).real(*pcomp).img

  50. Alternative Syntax • Pointer to structures are extremely common • Alternative notation as a shorthand • p→member-of-structure • → is minus sign followed by > • Example • pcomp→real • pcomp→img

More Related