100 likes | 131 Views
Learn the need for dynamic memory allocation and how to allocate, reserve, and manage memory during programming. Examples and syntax for dynamic memory functions in the stdlib.h library are provided, along with code snippets for usage.
E N D
Computer Programming forEngineering Applications ECE 175 Intro to Programming
Dynamic memory allocation Why do we need to allocate the memory dynamically? We do not know the memory requirements beforehand User may need to specify required space Memory requirements may change during execution ECE 175
Dynamic Memory Allocation Function of the stdlib.h library for dynamic allocation Examples Syntax: pointer_var = (data type *)malloc(byte number) pointer to memory number of bytes reserved cast to datatype int *nump; char *letp; planet_t *planetp; nump = (int *)malloc(4); letp = (char *)malloc(sizeof(char)); planetp = (planet_t *)malloc(sizeof(planet_t); STEP 1 STEP 2 ECE 175
After Memory has been Aallocated Memory is reserved in memory area called heap ECE 175
Referencing to the newly allocated memory Same way that we would access contents using pointers *nump = 307; *letp = ‘Q’; ECE 175
Example for the use of malloc #include<stdio.h> // library for allocating memory #include<stdlib.h> int main(void) { int *temp; //pointer to an int int x, y; // allocatingmemory for one integer temp=(int *)malloc(sizeof (int)); printf("Give me an integer:"); scanf("%d",temp); // no & sign, tempis a pointer x=(*temp)*10; y=(*temp)*2; free(temp); // freeingmemoryallocatedtotemp printf("%d, %d\n", x,y); return (0); } ECE 175
Dynamic Allocation of Memory for a Structure typedefstruct { char name[20]; // name of the planet double diameter; // diameter of the planet in km int moons; // number of moon doubleorbit_time; // orbit around the sun in years doublerotation_time; // orbit around itself in years } planet_t; int main(void) { planet_t *pl3; // pointer to a planet //allocation of memory to store one planet pl3=(planet_t *)malloc(sizeof(planet_t)); scan_planet(pl3); // initialization of planet print_planet(*pl3); // printing the planet return(0); } ECE 175
Dynamic Array Allocation using calloc int main(void) { char *s1; // pointer to a char int *array_of_ints; // pointer to an int planet_t *array_of_planets; // pointer to a planet_t ints_size, int_array_size, planet_array_size, i; printf("Enter the string length> "); scanf("%d", &s_size); // reading the size of the array //allocation for a character array of size s_size s1 = (char *)calloc(s_size+1, sizeof(char)); printf("Enter the string>"); fflush(stdin); fgets(s1, s_size+1, stdin); // reading a line from stdin if (s1[strlen(s1)-1]=='\n') // elimination of '\n’ s1[strlen(s1)-1]='\0'; printf("%s",s1); // printingthestring ECE 175
Dynamic Array Allocation using calloc printf("\nEnter the size of the array you want to create?> "); scanf("%d", &int_array_size); // reading the size of the array // memory allocation of intarray of size int_array_size array_of_ints = (int *)calloc(int_array_size, sizeof (int)); array_of_ints[0] = 5; //initialization of the array values for (i = 0; i < int_array_size; i++) { array_of_ints[i] = i*(i+1); printf("%d ", array_of_ints[i]); } printf("\nEnter the number of planets you want to create>"); scanf("%d", &planet_array_size); // reading array size //memory allocation for a planet_t array array_of_planets = (planet_t *)calloc(planet_array_size, sizeof (planet_t)); for (i = 0; i <planet_array_size; ++i) // initialization { scan_planet(&array_of_planets[i]); print_planet(array_of_planets[i]); } return(0); } ECE 175
Memory Allocation - calloc ECE 175