1/28
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
How to declare a single variable using malloc/calloc?
var *pa = (var *)malloc(sizeof(var));How to write/allocate for a 1D array then?
var *parr = (var *)malloc(sizeof(var) * n);How about a 2D array then? How to allocate enough memory?
int **arr2d = malloc(n * sizeof(int *));Which term initializes the allocation to 0?
Calloc
Which one changes the propertie4s of the allocated memory?
Realloc
How to properly free something?
Freeing, in order from smallest(innerness) location, then working yourself up to the final one.
How to create a dynamically allocated struct?
temp = (struct point*)malloc(sizeof(struct point));How to create a 1D array of struct pointers?
data *pd = (data *)malloc(10 * sizeof(data));Function that shows freeing memory.

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;
}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;
}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;
}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--;
}
}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
}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;
}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;
}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);
}