Dynamic Memory Allocation + C review

0.0(0)
studied byStudied by 0 people
0.0(0)
call with kaiCall with Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/28

flashcard set

Earn XP

Description and Tags

For COP 3502

Last updated 11:06 PM on 2/2/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

29 Terms

1
New cards

How to declare a single variable using malloc/calloc?

var *pa = (var *)malloc(sizeof(var));

2
New cards

How to write/allocate for a 1D array then?

var *parr = (var *)malloc(sizeof(var) * n);

3
New cards

How about a 2D array then? How to allocate enough memory?

int **arr2d = malloc(n * sizeof(int *));

4
New cards

Which term initializes the allocation to 0?

Calloc

5
New cards

Which one changes the propertie4s of the allocated memory?

Realloc

6
New cards

How to properly free something?

Freeing, in order from smallest(innerness) location, then working yourself up to the final one.

7
New cards

How to create a dynamically allocated struct?

temp = (struct point*)malloc(sizeof(struct point));

8
New cards

How to create a 1D array of struct pointers?

data *pd = (data *)malloc(10 * sizeof(data));

9
New cards

Function that shows freeing memory.

<p></p>
10
New cards

Function that determines the maximum

int findMax(int* arr, int size) {
    int max = arr[0];
    for (int i = 1; i < size; i++) {
        if (arr[i] > max) max = arr[i];
    }
    return max;
}

11
New cards

Function that determines the minimum

int findMin(int* arr, int size) {
    int min = arr[0];
    for (int i = 1; i < size; i++) {
        if (arr[i] < min) min = arr[i];
    }
    return min;
}

12
New cards

How to resize an dynamically allocated array with realloc

int* resizeArray(int* arr, int newSize) {
    int* temp = (int*)realloc(arr, newSize * sizeof(int));
    if (temp == NULL) {
        printf("Reallocation failed!\n");
        return arr; 
    }
    return temp;
}

13
New cards

Random: How to reverse an array?

void reverseArray(int* arr, int size) {
    int start = 0;
    int end = size - 1;
    while (start < end) {
        int temp = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
        start++;
        end--;
    }
}

14
New cards

How to properly free

void freeMatrix(int** matrix, int rows) {
    for (int i = 0; i < rows; i++) {
        free(matrix[i]); // Free each row
    }
    free(matrix); // Free the array of pointers
}

15
New cards

How to read input and place into a allocated memory location

char ** readBreeds(int *count){

    // updating the breedCount variable in main by scanning file
    scanf("%d", count);

    // allocating the char array of string with the size of breedcount
    // times the sizeof a char.
    char **arr = malloc(*count * sizeof(char *));

    // for loop that has a temp variable, placing each breed type in that variable
    // then assigning the arr[index] with the space of temp
    // then copying the temp variable into arr[index
    for(int i = 0; i < *count; i++){
        char temp[50];
        scanf("%s", temp);
        arr[i] = malloc(strlen(temp) + 1);
        strcpy(arr[i], temp);
    }

    // returning the array when finished, which will be assigned to dictionary in main
    return arr;

}

16
New cards

Get a character in something, something you read in already(PA1 example)

char* getCharPtrByBreed(char **dictionary, char *breedName, int breedCount){

    for(int i = 0; i<breedCount; i++){ // for loop iterating through the dictionary
        if(strcmp(dictionary[i], breedName) == 0) // if the match is found
            return dictionary[i]; // return that array position
    }

    // if it wasn't found, return NULL;
    return NULL;
}

17
New cards

how to free memory(PA1 code)

void freeStore(int count, CatStore *store){


    // going through the kennels, creating a pointer variable of the
    // kennels array to help
    for(int i = 0; i< count; i++){
        Kennel *k = &store->kennels[i];

        // in this nested loop, freeing each name and the cats themselves
        // for each k[i]
        for(int j=0; j < k->occupancy; j++){
            free(k->cats[j]->name);
            free(k->cats[j]);
        }
        // after each round of j
        // freeing the k[i] cats array and the location of the kennel
        free(k->cats);
        free(k->location);

    }

    // freeing the kennels after everything above is complete
    free(store->kennels);

    // going through the capacities array, freeing
    // the rows
    for(int i=0; i < count; i++){
        free(store->capacities[i]);
    }

    // then freeing the entire thing
    free(store->capacities);

    // At the end, now the store can be freed.
    free(store);
}

18
New cards
19
New cards
20
New cards
21
New cards
22
New cards
23
New cards
24
New cards
25
New cards
26
New cards
27
New cards
28
New cards
29
New cards

Explore top flashcards