Run make
to compile.
Last active
February 5, 2024 10:43
-
-
Save wcho21/8b4a525bb6c720b76b39a67760e28f83 to your computer and use it in GitHub Desktop.
Shallow copy and deep copy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
main.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include "array_util.h" | |
#define STR_LEN 1024 | |
char* arrayToString(int* arr, int size, char* str) { | |
char buf[STR_LEN]; | |
str[0] = '\0'; | |
strcat(str, "["); | |
for (int i = 0; i < size; ++i) { | |
sprintf(buf, "%d%s", arr[i], (i < size-1) ? ", " : ""); | |
strcat(str, buf); | |
} | |
strcat(str, "]"); | |
return str; | |
} | |
char* nestedArrayToString(int** arr, int size, int nestedSize, char* str) { | |
char buf[STR_LEN]; | |
str[0] = '\0'; | |
strcat(str, "["); | |
for (int i = 0; i < size; ++i) { | |
char elemBuf[STR_LEN]; | |
sprintf(buf, "%s%s", arrayToString(arr[i], nestedSize, elemBuf), (i < size-1) ? ", " : ""); | |
strcat(str, buf); | |
} | |
strcat(str, "]"); | |
return str; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef ARRAY_UTIL_H | |
#define ARRAY_UTIL_H | |
char* arrayToString(int* arr, int size, char* str); | |
char* nestedArrayToString(int** arr, int size, int nestedSize, char* str); | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include "memory_util.h" | |
#include "array_util.h" | |
#define STR_LEN 1024 | |
void copyArray(int* source, int* dest, int size); | |
void copyNestedArray(int** source, int** dest, int size); | |
void deepCopyNestedArray(int** source, int** dest, int size, int nestedSize); | |
void demoCopyArray(); | |
void demoCopyNestedArray(); | |
void demoDeepCopyNestedArray(); | |
int main() { | |
demoCopyArray(); | |
demoCopyNestedArray(); | |
demoDeepCopyNestedArray(); | |
destroyArrays(); | |
} | |
void demoCopyArray() { | |
printf("[copy array demo]\n"); | |
int* original = createArray(3, 3, 1, 2, 3); | |
int* copy = createArray(3, 0); | |
char str[STR_LEN]; | |
copyArray(original, copy, 3); | |
printf("original: %s\n", arrayToString(original, 3, str)); | |
printf("copy: %s\n", arrayToString(copy, 3, str)); | |
original[0] = 4; | |
printf("(original changed)\n"); | |
printf("original: %s\n", arrayToString(original, 3, str)); | |
printf("copy: %s\n", arrayToString(copy, 3, str)); | |
printf("\n"); | |
} | |
void demoCopyNestedArray() { | |
printf("[copy nested array demo]\n"); | |
int* original[] = { createArray(2, 2, 1, 2), createArray(2, 2, 3, 4) }; | |
int* copy[] = { createArray(2, 0), createArray(2, 0) }; | |
char str[STR_LEN]; | |
copyNestedArray(original, copy, 2); | |
printf("original: %s\n", nestedArrayToString(original, 2, 2, str)); | |
printf("copy: %s\n", nestedArrayToString(copy, 2, 2, str)); | |
original[0][0] = 5; | |
printf("(original changed)\n"); | |
printf("original: %s\n", nestedArrayToString(original, 2, 2, str)); | |
printf("copy: %s\n", nestedArrayToString(copy, 2, 2, str)); | |
printf("\n"); | |
} | |
void demoDeepCopyNestedArray() { | |
printf("[deep copy nested array demo]\n"); | |
int* original[] = { createArray(2, 2, 1, 2), createArray(2, 2, 3, 4) }; | |
int* copy[] = { createArray(2, 0), createArray(2, 0) }; | |
char str[STR_LEN]; | |
deepCopyNestedArray(original, copy, 2, 2); | |
printf("original: %s\n", nestedArrayToString(original, 2, 2, str)); | |
printf("copy: %s\n", nestedArrayToString(copy, 2, 2, str)); | |
original[0][0] = 5; | |
printf("(original changed)\n"); | |
printf("original: %s\n", nestedArrayToString(original, 2, 2, str)); | |
printf("copy: %s\n", nestedArrayToString(copy, 2, 2, str)); | |
printf("\n"); | |
} | |
void copyArray(int* source, int* dest, int size) { | |
for (int i = 0; i < size; ++i) { | |
dest[i] = source[i]; | |
} | |
} | |
void copyNestedArray(int** source, int** dest, int size) { | |
for (int i = 0; i < size; ++i) { | |
dest[i] = source[i]; | |
} | |
} | |
void deepCopyNestedArray(int** source, int** dest, int size, int nestedSize) { | |
for (int i = 0; i < size; ++i) { | |
int* copy = createArray(nestedSize, 0); | |
for (int j = 0; j < nestedSize; ++j) { | |
copy[j] = source[i][j]; | |
} | |
dest[i] = copy; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
main.out: main.c memory_util.c array_util.c | |
$(CC) $^ -o $@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdarg.h> | |
#include <stdlib.h> | |
#include "memory_util.h" | |
#define STACK_SIZE 1024 | |
static int* arrayStack[STACK_SIZE]; | |
static int arrayStackPointer = 0; | |
// util functions to manage memory | |
int* createArray(int arrLen, int initLen, ...) { | |
va_list args; | |
va_start(args, initLen); | |
int* arr = malloc(arrLen * sizeof(int)); | |
arrayStack[arrayStackPointer++] = arr; | |
for (int i = 0; i < initLen; ++i) { | |
arr[i] = va_arg(args, int); | |
} | |
va_end(args); | |
return arr; | |
} | |
void destroyArrays() { | |
for (int i = arrayStackPointer-1; i >= 0; --i) { | |
int* arr = arrayStack[i]; | |
free(arr); | |
} | |
arrayStackPointer = 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef MEMORY_UTIL_H | |
#define MEMORY_UTIL_H | |
int* createArray(int arrLen, int initLen, ...); | |
void destroyArrays(); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment