This commit is contained in:
yj
2025-06-25 11:30:28 +08:00
commit aa2fbef447
4 changed files with 77 additions and 0 deletions

37
malloc.c Normal file
View File

@ -0,0 +1,37 @@
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main()
{
SetConsoleOutputCP(65001); // 设置控制台为 UTF-8 编码,配合 #include <windows.h> ,终端输出的就不是乱码
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<stdlib.h>
return 0;
}

BIN
malloc.exe Normal file

Binary file not shown.

40
memory.c Normal file
View File

@ -0,0 +1,40 @@
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int e;
static int f;
int g = 10;
static int h = 10;
int main()
{
SetConsoleOutputCP(65001); // 设置控制台为 UTF-8 编码,配合 #include <windows.h> ,终端输出的就不是乱码
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<stdlib.h>
return 0;
}

BIN
memory.exe Normal file

Binary file not shown.