学c++方向,指针、结构体、动态内存开辟这三个直接关系到数据结构的学习,非常重要(学java就还好)
栈区:局部变量,形式参数
堆区:malloc,calloc,realloc,free申请的空间
malloc
int main()
{int arr[10] = { 0 }; //栈区申请空间//动态内存开辟int* p = (int*)malloc(40); //堆区申请空间if (p == NULL) //如果开辟失败{ printf("%s\n", strerror(errno));return 1; //1表示异常返回}//使用内存int i = 0;for (i = 0; i < 10; i++){*(p + i) = i;}for (i = 0; i < 10; i++){printf("%d ", *(p + i));}//没有free,内存泄露 但并不是说内存空间就不回收了//当程序退出的时候,系统会自动回收内存空间free(p);p = NULL; //防止p是个野指针//malloc跟free最好成对出现,防止内存泄露//过多内存泄露会导致电脑卡死,因为内存干爆了return 0;
}
calloc
calloc = malloc + memset
想初始化用calloc,不想用malloc
num : Number of elements
size :Length in bytes of each element
int main()
{//开辟10个整型的空间int* p = calloc(10, sizeof(int));if (p == NULL){printf("%s\n", strerror(errno));}//打印int i = 0;for (i = 0; i < 10; i++){printf("%d ", *(p + i)); //0 0 0 0 0 0 0 0 0 0}free(p);p = NULL;return 0;
}
realloc
int main()
{int* p= (int*)malloc(40);if (NULL == p){printf("%s\n", strerror(errno));return 1;}int i = 0;for (i = 0; i < 10; i++){*(p + i) = i + 1;}//扩容int*ptr = realloc(p, 80); //追加上40个字节,总共80个字节 //realloc会在堆区找到80字节的空间,再把数据拷贝过去,释放掉旧的空间,最后返回新的地址if (ptr != NULL) //如果扩容失败,返回空指针{p = ptr; //因此直接赋值给p很危险}for (i = 0; i < 10; i++){printf("%d ", *(p + i));}free(p);p = NULL;return 0;
}
realloc(NULL, 40); 等价于 malloc(40);
常见动态内存错误
int*p=(int*)malloc(INT_MAX/4);*p=20; //p是NULL是就有错误
动态开辟空间的越界访问
//动态开辟空间的越界访问int* p = (int*)malloc(40);if (p == NULL){printf("%s\n", strerror(errno));return 1;}int i = 0;for (i = 0; i <= 10; i++){p[i] = i;}free(p);p = NULL;
free栈区空间
int a = 10;int* p = &a;free(p);
使用free释放动态开辟的一部分
int* p = (int*)malloc(40);if (p == NULL){printf("%s\n", strerror(errno));return 1;}int i = 0;for (i = 0; i < 10; i++){*p=i;p++;}free(p); //p不再指向空间的起始位置p = NULL;
对同一块空间的多次释放
下面这个不行
free(p);//....free(p);
这个可以
free(p);p = NULL;//....free(p);
动态内存开辟忘记释放(内存泄露)
int* p = (int*)malloc(40);if (p == NULL){printf("%s\n", strerror(errno));return 1;}int i = 0;for (i = 0; i < 10; i++){if (i == 3) return; //free没有机会实现}free(p);p = NULL;
int* test()
{int* p = (int*)malloc(100);if (p == NULL){printf("%s\n", strerror(errno));return p;}return p;
}
int main()
{int* ret = test();//忘记释放了return 0;
}
几个经典的笔试题
void GetMemory(char* p)
{p = (char*)malloc(100); //p是形参,出函数后被销毁
}void Test(void)
{char* str = NULL;GetMemory(str); //str仍是空指针strcpy(str, "hello world"); //str解引用时,程序会崩溃printf(str); //打印不出来
}int main()
{Test();return 0;
}
修改后
void GetMemory(char** p)
{*p = (char*)malloc(100);
}void Test(void)
{char* str = NULL;GetMemory(&str); strcpy(str, "hello world"); printf(str); free(str);str = NULL;
}
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;
}
3
void Test(void)
{char* str = (char*)malloc(100);strcpy(str, "hello");free(str);if (str != NULL){strcpy(str, "world"); //str还记得地址,但是空间已经不属于str了//因此free完后一定要把指针置为空printf(str);}
}
int main()
{Test();return 0;
}