1 / 27

Dynamic Allocation

Dynamic Allocation. Array variables have fixed size, used to store a fixed and known amount of variables 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

fifi
Download Presentation

Dynamic Allocation

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. Dynamic Allocation • Array variables have fixed size, used to store a fixed and known amount of variables • 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

  2. The malloc function void *malloc(unsigned int nBytes); • The function malloc is used to dynamically allocate nBytes worth of space • How to determine nBytes? • 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>

  3. Example dynamic_reverse_array.c

  4. 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.

  5. 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 */

  6. 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!

  7. Another example another_strcpy.c

  8. 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

  9. Solution my_strcat.c (my_strcat2.c)

  10. 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; }

  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

  12. Exercise • Write the following program: • Input: a sequence of positive integers, until a negative one is input • Output: the largest 20 numbers (not necessarily sorted) • Do this without dynamic allocation

  13. Solution top20.c

  14. Exercise • Extend the previous program so that the number of numbers presented at the end is chosen by the user: • Input: a number N, and a sequence of positive integers, terminated by a negative one • Output: the top N integers

  15. Solution topN.c

  16. Implementing a student package • We want to create (part of) a course-management program. • We need to – • Maintain a list of the participating students • Keep track of their final grade • Be able to add and remove students from the course • And so on…

  17. How will we store a student? • One possible way is using a structure, like - typedefstruct Student_t { char ID[ID_LENGTH+1]; char Name[NAME_LENGTH+1]; int grade; } Student;

  18. Creating a student 1 Student NewStudent(char name[], char ID[], int grade) { Student new; new.Name = name; new.ID = ID; new.grade = grade; return new; }

  19. Creating a student 2 (a better idea) Student NewStudent(char name[], char ID[], int grade) { Student new; strcpy(new.Name, name); strcpy(new.ID, ID); new.grade = grade; return new; }

  20. Storing a list of students • One way to go would be by using an array of student structures (or pointers to structures). • The are problems with this – • we must allocate a big-enough array before accepting students (how do we know what’s big enough?) • How shall we remove students from the list without creating “holes”? • How can we maintain the list sorted by grade? • Insertion and deletion may be problematic

  21. Linking students • A better alternative might be using a linked list, by “self reference”. typedefstruct Student_t { char ID[ID_LENGTH]; char Name[NAME_LENGTH]; int grade; struct Student_t *next; /* A pointer to the next item on the list */ } Student;

  22. Linked lists - searching ! ? ? Head … * A list is always maintained by its head (why is this enough?)

  23. Linked lists - insertion Previous Next Head … Insert new item:

  24. Linked lists - deletion Previous Current Head …

  25. Creating a new kind of student • Usually when using linked lists we don’t know how many elements will be in the list • Therefore we would like to be able to dynamically allocate new elements when the need arises • A possible implementation follows…

  26. Creating a new kind of student Student *create_student(char name[], char ID[], int grade) { Student *std; std = (Student *)malloc(sizeof(Student)); if (std == NULL) { printf(“Memory allocation error!\n”); exit(1); } strcpy(std->Name, name); strcpy(std->ID, ID); std->grade = grade; std->next = NULL; return std; }

  27. Why not this, again? Student *create_student(char name[], char ID[], int grade) { Student std; strcpy(std.Name, name); strcpy(std.ID, ID); std.grade = grade; std.next = NULL; return &std; }

More Related