1 / 10

Computer Programming for Engineering Applications

Computer Programming for Engineering 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

pmildred
Télécharger la présentation

Computer Programming for Engineering Applications

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. Computer Programming forEngineering Applications ECE 175 Intro to Programming

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

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

  4. After Memory has been Aallocated Memory is reserved in memory area called heap ECE 175

  5. Referencing to the newly allocated memory Same way that we would access contents using pointers *nump = 307; *letp = ‘Q’; ECE 175

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

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

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

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

  10. Memory Allocation - calloc ECE 175

More Related