Growing Arrays in C Language

Growing Arrays in C Language

Growing Arrays in C Language When to Use Do not use Growing Sorted Array O(n2) Operation Avoid when n is large Use Keep track of

a variable Few items Theory of Growing Arrays Java and C++ Library Class C struct Code Allocate Memory malloc

realloc Minimize Memory Space Resize in chunks Gather Array Together Memory Allocation Library function - malloc() malloc() allocates certain size of bytes in memory

malloc() returns a pointer to the allocated space or NULL if there is an error or size is zero Memory allocated by function must be released or freed when finished void *malloc()(size_t size) void free(void *pointer) Example int *ptr = malloc(10*sizeof(int)); if (ptr == NULL) { /*Error Occurred. Do Something*/

} else { free(ptr); ptr = NULL; } Reallocation of Memory Library Function - realloc() realloc() changes the size of a block of memory already allocated by malloc()

realloc() returns a pointer to block of memory or NULL May expand or move block of memory void *realloc(void *ptr, size_t size) Example int *ptr = malloc(10*sizeof(int)); /*Somewhere Later in the Program*/ int *temp = realloc(ptr, 15*sizeof(int)); if (temp == NULL) { /*Error Occurred. Do Something*/

} else { ptr = temp; } Growing Array Code (1) typedef struct Nameval Nameval; struct Nameval{ char *name; int value; };

struct NVtab{ int nval; /*current # of values*/ int max; /*allocated # of values*/ Nameval *nameval; /*array of pairs*/ } nvtab; enum{NVINIT = 1, NVGROW = 2}; Growing Array Code (2) int addname(Nameval newname) { Nameval *nvp;

if (nvtab.nameval == NULL) nvtab.nameval = (Nameval *) malloc(NVIT*sizeof(Nameval)); if (nvtab.nameval == NULL) return -1; nvtab.max = NVINIT; nvtab.nval = 0; } Growing Array Code (3) else if (nvtab.nval >= nvtab.max){ nvp = (nameval *) realloc(nvtab.nameval, (NVGROW*nvtab.max)*sizeof(Nameval));

if (nvp == NULL) return -1; nvtab.max *= NVGROW; nvtab.nameval = nvp; } nvtab.nameval[nvtab.nval] = newname; return nvtab.nval++; } memmove() Function To shrink a growing array we need to use memmove()

memmove() copies n bytes from one location and moves it to another *memmove(*s1, const *s2, size_t n) Shrinking Array Code int delname(char *name) { int i; for (i = 0; i < nvtab.nval; i++) if (strcmp(nvtab.nameval[i].name, name) == 0){ memmove(nvtab.nameval+i,nvtab.nameval+i+1, (nvtab.nval-(i+1))*sizeof(Nameval));

nvtab.nval--; return 1; } return 0; }

Recently Viewed Presentations

  • Linac PIP Modulator Update - Fermilab

    Linac PIP Modulator Update - Fermilab

    Short Circuit Problems. Gate Driver Circuit Board . Controls the firing of the main IGBT to create the modulator waveform. 25 Gate Drive Boards have already been built. Automatically reduces the drive level in slow and controlled manor during short...
  • Reconciling Nationalist and Non-Nationalist Loyalties

    Reconciling Nationalist and Non-Nationalist Loyalties

    Living With Contending Loyalties. How does an individual choose 1 loyalty over another? To deal with contending nationalist loyalties, people often have to sacrifice an important part of their identity. This often leads to feelings of . alienation - feelings...
  • Diapositiva 1 - OSARVA

    Diapositiva 1 - OSARVA

    Team Leader Training Search Competencies Ontario Search and Rescue Volunteer Association
  • Dr. Bill Vicars Lifeprint.com

    Dr. Bill Vicars Lifeprint.com

    Arial Arial Black Times New Roman Wingdings lesson28-tv Dr. Bill Vicars Lifeprint.com Linguistics "regain": RE = morpheme "R" = phoneme If a small part of a sign has meaning, what do we call it? A "morpheme." A small, meaningless by...
  • Context Clues - Williams Reading Wizards

    Context Clues - Williams Reading Wizards

    What are context clues? When you use clues in a text to figure out the meaning of a new, ... Antonym: The author uses a word with an opposite meaning. More Types of Context Clues. ... Mr. Fry is an...
  • SS5H6 The student will explain the reasons for America&#x27;s ...

    SS5H6 The student will explain the reasons for America's ...

    SS5H6 The student will explain the reasons for America's involvement in World War II. a. Describe Germany's aggression in Europe and Japan's aggression in Asia. b. Describe major events in the war in both Europe and the Pacific; include Pearl...
  • Ecdysozoa The Ecdysozoans are a grouping of protostome

    Ecdysozoa The Ecdysozoans are a grouping of protostome

    This hard exoskeleton gives the organism's segmented body protection and a place for muscle attachment. Arthropods also have jointed appendages that allow for specialized functions. They have an organ system level or organization and they are triploblastic. There are five...
  • Project B3 - University of York

    Project B3 - University of York

    Particle transport and organic carbon fluxes off NW Africa: impact of dust and carbonate G. Fischer (1), M. Iversen (3, 4), G. Karakas (3) , N. Nowald (1) ,