目录
一、练习
2、局部变量
char* GetMemory(void)
{
char p[] = "hello world";
return p;
}
void Test(void)
{
char* str = NULL;
str = GetMemory();
printf(str);
}
int main()
{
Test();
return 0;
}
p是个局部变量,出了这个函数后地址就会消失,即使在这之前已经把地址传了出去,str接收到后就指向h的地址,但是此时这块空间已经不是p的了,也不是hello world,所以不一定会打印出什么内容。这是一种返回栈空间的地址问题,因为局部变量存储在栈区,并且栈区的空间地址尽量不要随意返回。
3、忘记free
void GetMemory(char** p, int num)
{
*p = (char*)malloc(num);
}
void Test(void)
{
char* str = NULL;
GetMemory(&str, 100);
strcpy(str, "hello");
printf(str);
}
int main()
{
Test();
return 0;
}
存在内存泄露问题,因为没有free。
4、提前free
void Test(void)
{
char* str = (char*)malloc(100);
strcpy(str, "hello");
free(str);
if (str != NULL)
{
strcpy(str, "world");
printf(str);
}
}
int main()
{
Test();
return 0;
}
进入Test函数,动态开辟空间,把hello移动到str里,之后释放空间,虽然str还存在,但是所指向的空间已经还给系统了,str自然还不是NULL,进入if语句后,再把world拷贝,这时候就形成了非法访问,因为str指向的空间已经不属于str了,已经还给系统了。
二、C程序的内存开辟
内核空间(用户代码不能读写)只有操作系统使用,用户看不到
栈(向下增长)
内存映射段(文件映射、动态库、匿名映射)
堆(向上增长)
数据段(全局数据、静态数据)也就是静态区
代码段(可执行代码/只读常量)
以代码为例
int globalVar = 1;
static int staticGlobalVar = 1;
void Test()
{
static int staticVar = 1;
int localVar = 1;
int num1[10] = { 1, 2, 3, 4 };
char char2[] = "abcd";
const char* pChar3 = "abcd";
int* ptr1 = (int*)malloc(sizeof(int) * 4);
int* ptr2 = (int*)calloc(4, sizeof(int));
int* ptr3 = (int*)realloc(ptr2, sizeof(int) * 4);
free(ptr1);
free(ptr3);
}
从localVar到ptr3都是局部变量,都是在栈区开辟的。
static修饰的数据是静态数据,所以放在静态区。而最上方两个是全局变量,staticGlobalVar 是全局的静态变量,也都放在静态区。
动态开辟的空间在堆区开辟。
pChar3的"abcd"这样的常量字符串放在代码段里,是个只读常量,放在只读区的就不能被修改。
结束。