动态内存分配的常见错误

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//-------------------------------动态内存分配-----------------------
#if 0
int main()
{
int* p = (int*)malloc(10 * sizeof(int));
//面试考点
if (p == NULL)
{
//错误处理代码
return 1;
}
for (int i = 0; i < 10; i++)
{
p[i] = i;
}
return 0;
}

int main()
{
int *p = calloc(10, sizeof(int));
if (NULL != p)
{
//使用空间
}
//free传入的地址,必须是malloc返回的起始地址
free§;
p = NULL;
return 0;
}

//对NULL指针的解引用操作
void test()
{
int *p = (int *)malloc(INT_MAX / 4);
*p = 20;//如果p的值是NULL,就会有问题
free§;
}
//对动态开辟空间的越界访问
void test()
{
int i = 0;
int *p = (int *)malloc(10 * sizeof(int));
if (NULL == p)
{
exit(EXIT_FAILURE);
}
for (i = 0; i <= 10; i++)
{
(p + i) = i;//当i是10的时候越界访问
}
free§;
}
//对非动态开辟内存使用free释放
void tets()
{
int a = 10;
int
p = &a;
free§;
}
//使用free释放一块动态开辟内存的一部分
void test()
{
int *p = (int *)malloc(100);
p++;
free§; //p不再指向动态内存的起始位置
}
//对同一块动态内存多次释放
void test()
{
int *p = (int *)malloc(100);
free§;
free§;//重复释放
}
//动态开辟内存忘记释放(内存泄漏)
void test()
{
int *p = (int *)malloc(100);
if (NULL != p)
{
*p = 20;
}
}
int main()
{
test();
while (1);
}

//---------------------动态内存分配试题--------------------------------------
/*题目:1
错误:1.malloc没有free
2.malloc没有判断空
*/
//void GetMemory(char *p)
//{
// p = (char *)malloc(100);
//}
//void Test(void)
//{
// char *str = NULL;
// GetMemory(str);
// strcpy(str, “hello world”);
// printf(str);
//}

//修改程序:
void GetMemory(char** p)
{
p = (char)malloc(100);
if (*pNULL)
{
return;
}
}
void Test(void)
{
char *str = NULL;
GetMemory(&str);
if (str
NULL)
{
return;
}
strcpy(str, “hello world\n”);
printf(str);
free(str);
}
int main()
{
Test();
return 0;
}

/*题目:2

  1. char p[]是把hello world往数组中复制了一份,p是一个局部变量,
    随着函数执行结束就释放了,在函数外部 就不能访问P中的内容。
    2.char* p也是一个局部变量,但是这里面只包含一个字符串常量的地址,函数执行结束,就把这个地址交给了str,
    字符串常量在内存中的生命周期是跟随整个程序的,此时拿着刚才的地址仍能找到字符串内容。
    */
    char GetMemory(void)
    {
    char
    p= “hello world”; //error: char p[] = “hello world”;
    return p;
    }
    void Test(void)
    {
    char *str = NULL;
    str = GetMemory();
    printf(str);
    }
    int main()
    {
    Test();
    return 0;
    }
    /*题目3:
    错误:1.malloc没有free
    2.malloc没有判断空
    */
    void GetMemory(char **p, int num)
    {
    *p = (char *)malloc(num);
    }
    void Test(void)
    {
    char str = NULL;
    GetMemory(&str, 100);
    if (str==null) //判断
    {
    return;
    }
    strcpy(str, “hello”);
    printf(str);
    free(str); //释放
    }
    /

    题目4:
    错误:内存访问越界
    */
    void Test(void)
    {
    char str = (char )malloc(100);
    strcpy(str, “hello”);
    free(str);
    if (str != NULL)
    {
    strcpy(str, “world”);
    printf(str);
    }
    }
    #endif
    //----------------------C/C++内存开辟-----------------------------------
    void a()
    {
    }
    int b = 0;
    int main()
    {
    int
    c =(int
    ) malloc(sizeof(int));
    int d = 0;
    printf(“代码段:%p\n”,a);
    printf(“数据段:%p\n”,&b);
    printf(“堆:%p\n”, c);
    printf(“栈:%p\n”, &d);
    return 0;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值