thread:pth00 stack overflow 内存溢出

遇到的错误:
在这里插入图片描述
使用malloc动态分配内存:

1.错误示范修改

extern int xxx_base_get_wifi_state(struct rev_info *ota_rec);
static int xxx_demo_test_1(vd_int_t argc, vd_char_t **argv)
{
   
   vd_char_t ota_rec[512]= {0};
   
   printf("sizeof(1)=%d\n",sizeof(1));
    printf("sizeof(4)=%d\n",sizeof(4));
   int flag=xxx_base_get_wifi_state(ota_rec);
   free(ota_rec);
}
extern int xxx_base_get_wifi_state(struct rev_info *ota_rec);
static int xxx_demo_test_1(vd_int_t argc, vd_char_t **argv)
{
   printf("sizeof(1)=%d\n",sizeof(1));
    printf("sizeof(4)=%d\n",sizeof(4));
    vd_char_t *ota_rec=(vd_char_t *)malloc(sizeof(1)*512);
   esp32_mini_sdio_AT_init();  
   int flag=xxx_base_get_wifi_state(ota_rec);
   free(ota_rec);
}

**vd_char_t ota_rec[512]= {0};**写成 **vd_char_t *ota_rec=(vd_char_t )malloc(sizeof(1)512);

malloc 这个函数,向内存申请size个字节的空间,然后将申请的空间的起始地址返回。

2.头文件

#include<stdio.h>
#include<stdlib.h>

3.示例

1、 指针

intp1 =(int)malloc(sizeof(int));

(int ) 为强制类型转换,将 (void) 转换为整形指针

(sizeof(int)) 为分配内存大小,一个整形大小(4个字节)

2、数组

int* arr = (int*)malloc(n * sizeof(int));

(n * sizeof(int)) 同上,为分配 n * 4 的字节内存大小

arr指向数组首地址

和arr[10]中 arr 为 arr[0] 的地址 用法相同

    //       1、创建整形指针
int*p1 =(int*)malloc(sizeof(int));
    //        字符型指针
char *p2=(char*)malloc(sizeof(char));
char *p2=(char*)malloc(1);
    //		2、创建数组arr[]
int* arr = (int*)malloc(n * sizeof(int));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值