Dynamic allocation is very intimidating to a person the first time he comes across it, but that need not be. Simply relax and read this chapter carefully and you will have a good grounding in a very valuable programming resource. All of the variables in every program up to this point have been static variables as far as we are concerned. (Actually, some of them have been "automatic" and were dynamically allocated for you by the system, but it was transparent to you.) In this chapter, we will study some dynamically allocated variables. They are simply variables that do not exist when the program is loaded, but are created dynamically as they are needed. It is possible, using these techniques, to create as many variables as needed, use them,
and deallocate their space for use by other variables. As usual, the best teacher is an example, so load and display the program named dynlist.c.
We begin by defining a named structure "animal" with a few fields pertaining to dogs. We do not define any variables of this type, only three pointers. If you search through the remainder of the program, you will find no variables defined so we have nothing to store data in. All we have to work with are three pointers, each of which point to the defined structure. In order to do anything, we need some variables, so we will create some dynamically.
and deallocate their space for use by other variables. As usual, the best teacher is an example, so load and display the program named dynlist.c.
main( )
{
struct animal {
char name[25];
char breed[25];
int age;
} *pet1, *pet2, *pet3;
pet1 = (struct animal *)malloc(sizeof(struct animal));
strcpy(pet1->name,"General");
strcpy(pet1->breed,"Mixed Breed");
pet1->age = 1;
pet2 = pet1; /* pet 2 now points to the above data structure */
pet1 = (struct animal *)malloc(sizeof(struct animal));
strcpy(pet1->name,"Frank");
strcpy(pet1->breed,"Labrador Retriever");
pet1->age = 3;
pet3 = (struct animal *)malloc(sizeof(struct animal));
strcpy(pet3->name,Krystal");
strcpy(pet3->breed,German Shepard");
pet3->age = 4;
/* now print out the data described above /*
printf("%s is a %s, and is %d years old.\n",pet1->name,pet1->breed,
pet1->age);
printf("%s is a %s, and is %d years old.\n",pet2->name,pet2->breed,
pet2->age);
printf("%s is a %s and is %d years old.\n",pet3->name,pet3->breed,
pet3->age);
pet1 = pet3; /* pet1 now points to the same structure that
pet3 points to */
free(pet3); /* this frees up one structure */
free(pet2); /* this frees up one more structure */
/* free(pet1); this cannot be done,see explanation in text */
}
{
struct animal {
char name[25];
char breed[25];
int age;
} *pet1, *pet2, *pet3;
pet1 = (struct animal *)malloc(sizeof(struct animal));
strcpy(pet1->name,"General");
strcpy(pet1->breed,"Mixed Breed");
pet1->age = 1;
pet2 = pet1; /* pet 2 now points to the above data structure */
pet1 = (struct animal *)malloc(sizeof(struct animal));
strcpy(pet1->name,"Frank");
strcpy(pet1->breed,"Labrador Retriever");
pet1->age = 3;
pet3 = (struct animal *)malloc(sizeof(struct animal));
strcpy(pet3->name,Krystal");
strcpy(pet3->breed,German Shepard");
pet3->age = 4;
/* now print out the data described above /*
printf("%s is a %s, and is %d years old.\n",pet1->name,pet1->breed,
pet1->age);
printf("%s is a %s, and is %d years old.\n",pet2->name,pet2->breed,
pet2->age);
printf("%s is a %s and is %d years old.\n",pet3->name,pet3->breed,
pet3->age);
pet1 = pet3; /* pet1 now points to the same structure that
pet3 points to */
free(pet3); /* this frees up one structure */
free(pet2); /* this frees up one more structure */
/* free(pet1); this cannot be done,see explanation in text */
}

