commit aa2fbef447e6fd48d1677bdf835d4e51dfe607d0 Author: yj Date: Wed Jun 25 11:30:28 2025 +0800 wall diff --git a/malloc.c b/malloc.c new file mode 100644 index 0000000..651968f --- /dev/null +++ b/malloc.c @@ -0,0 +1,37 @@ +#include +#include +#include + +int main() +{ + SetConsoleOutputCP(65001); // 设置控制台为 UTF-8 编码,配合 #include ,终端输出的就不是乱码 + int* ptr; + // 分配可以存储 5 个 int 值的内存块, 内存中的值是随机的 + ptr = (int*)malloc(5 * sizeof(int)); + if (ptr == NULL) + { + printf("内存分配失败\n"); + return 1; + } + + // 使用分配的内存块 + for (int i = 0; i < 5; i++) + { + ptr[i] = i + 1; + } + + // 打印内存中的值 + for (int i = 0; i < 5; i++) + { + printf("%d ", ptr[i]); + } + printf("\n"); + // 释放内存 + free(ptr); + + system("pause"); //保持终端存在,配合 #include + + + return 0; +} + diff --git a/malloc.exe b/malloc.exe new file mode 100644 index 0000000..e0bff4f Binary files /dev/null and b/malloc.exe differ diff --git a/memory.c b/memory.c new file mode 100644 index 0000000..7f42145 --- /dev/null +++ b/memory.c @@ -0,0 +1,40 @@ +#include +#include +#include + +int e; +static int f; +int g = 10; +static int h = 10; +int main() +{ + SetConsoleOutputCP(65001); // 设置控制台为 UTF-8 编码,配合 #include ,终端输出的就不是乱码 + int a; + int b = 10; + static int c; + static int d = 10; + char* i = "test"; + char* k = NULL; + + printf("&a\t %p\t // 局部未初始化变量\n", &a); + printf("&b\t %p\t // 局部初始化变量\n", &b); + + printf("&c\t %p\t // 静态局部未初始化变量\n", &c); + printf("&d\t %p\t // 静态局部初始化变量\n", &d); + + printf("&e\t %p\t // 全局未初始化变量\n", &e); + printf("&f\t %p\t // 全局静态未初始化变量\n", &f); + + printf("&g\t %p\t // 全局初始化变量\n", &g); + printf("&h\t %p\t // 全局静态初始化变量\n", &h); + + printf("i\t %p\t // 只读数据(文字常量区)\n", i); + + k = (char*)malloc(10); + printf("k\t %p\t // 动态分配的内存\n", k); + + system("pause"); //保持终端存在,配合 #include + + return 0; +} + diff --git a/memory.exe b/memory.exe new file mode 100644 index 0000000..35f1f4f Binary files /dev/null and b/memory.exe differ