2012年7月28日

摘要: #include <iostream>#include <stdio.h>#include <stdlib.h>using namespace std;char *GetMemory(char *&p, int num){p = (char *)malloc(sizeof(char)*num);//p = new char[num];return p;}int main(void){char *str ;GetMemory(str, 100);strcpy(str, "hello");cout << str <& 阅读全文
posted @ 2012-07-28 23:44 longlybits 阅读(11043) 评论(0) 推荐(1) 编辑
摘要: 常见的内存错误:1. 内存未分配成功,却使用它2. 内存分配成功,尚未初始化就使用它3. 内存分配成功,已初始化,但越界使用4. 忘记释放内存,造成内存泄露5. 释放了内存,但还在使用它a)返回的是“栈类型”的指针,因为该内存在函数结束是自动销毁b)用free或delete释放内存后,没有将指针设置为NULL而继续使用,导致野指针GetMemory函数的几种经典考法代码1: void GetMemory(char *p){ p = (char*)malloc(100);}int main(int argc, char *argv[]){ char *str = NULL; GetMemory( 阅读全文
posted @ 2012-07-28 23:17 longlybits 阅读(303) 评论(0) 推荐(0) 编辑

导航