当前位置: 首页 > news >正文

24/06/26(1.1129)动态内存

strtok 字符串分割函数

#include<stdio.h>


int main(){
    char str[] = "this,a sample string.";
    char* sep = ",";
    char* pch = strtok(str, sep);
    printf("%s\n", pch);
    while (pch != NULL){
        printf("%s\n", pch);
        pch = strtok(NULL, sep);
    }


    system("pause");
    return 0;
}

动态内存分配

为什么存在动态内存分配

动态内存函数介绍

        malloc

        free

        calloc

        realloc

常见的动态内存错误

柔性数组

为什么动态内存分配?

int val = 20;//在栈空间上开辟四个字节

char arr[10] = {0};//在栈空间上开辟十个字节的连续空间

但是上述开辟空间的方式有两个特点:

1.空间开辟的大小是固定的

2.数组在申明的时候,必须指定数组的长度,它所需要的内存在编译时分配

但是对于空间的需求,不仅是上述情况,有时候需要空间大小在程序运行的时候只能试试动态内存开辟了.

#include<stdio.h>

int g = 0;

void fun(int x, int y){
    int m; //局部变量
    int n;
}

int main(){
    //静态开辟 栈区
    int a = 10;
    char* str = "hello";
    int arr[10] = { 0 };

    system("pause");
    return 0;
}

stack栈区:(局部变量,函数的参数)

heap堆区:自由存储区 malloc free

static静态常量区:(静态定义的对象,字符串常量)

malloc 和 free

#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>


void main(){
    int n;
    printf("input n:");
    scanf("%d",&n);
    //动态内存开辟
    int* arr = (int *)malloc(sizeof(int) * n);//n int
    if (NULL == arr){
        printf("neicunyichu");
        return ;
    }
    for (int i = 0; i < n; i++){
        arr[i] = i + 1;
    }
    for (int i = 0; i < n; i++){
        printf("%d", arr[i]);
    }
    printf("\n");

    free(arr);//释放哦
    system("pause");
    
}

void main(){
    int *p = (int*)malloc(100);
    free(p);//释放空间

    system("pause");
    
}

释放p之后这片内存已经释放了,但指针*p依旧指向这块内存,它已经变成了野指针.

所以要对它赋值 NULL

p = NULL;//释放指针

calloc和malloc很像,一般用malloc代劳

void main(){
    int *p = (int*)malloc(sizeof(int) * 10);
    if (NULL == p)
        return 0;

    int *p1 = (int*)calloc(10,sizeof(int));//会把申请的空间初始化
    if (NULL == p1)
        return 0;

realloc 对申请空间的大小进行调整

#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>


void main(){
    int n = 5;
    int* p = (int*)malloc(sizeof(int)* n);
    if (NULL == p){
        printf("yichule");
        return ;
    }
    for (int i = 0; i < n; i++){
        p[i] = i + 1; //1 2 3 4 5
    }
    for (int i = 0; i < n; i++){
        printf("%d",p[i]);
    }
    printf("\n");

    //1 2 3 4 5 6 7 8 9 10
    n = 10;
    p = realloc(p, sizeof(int)* n);

if(NULL = p){

printf("yichule");

return;

}
    for (int i = 5; i < n; i++){
        p[i] = i + 1; //1 2 3 4 5 x x x x x
    }
    for (int i = 0; i < n; i++){
        printf("%d",p[i]); //1 2 3 4 5
    }
    printf("\n");
    system("pause");
    
}

.bss段 一般来存放未初始化的全局变量;

被初始化的在.data段即数据段(在静态常量区).

柔性数组:

#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>

typedef struct test
{
    char a;
    double b;
    int c;
    char arr[0];//柔性数组成员 不占空间
}test;

void main(){
    printf("size = %d\n",sizeof(test));
    system("pause");
    
}

http://www.lryc.cn/news/384538.html

相关文章:

  • 基于 elementUI / elementUI plus,实现 主要色(主题色)的一件换色(换肤)
  • js 计算某个日期加月份最后月份不会增加或者跳变
  • Git简介与详细教程
  • 创建OpenWRT虚拟机
  • 智慧安防新篇章:如何科学设定可燃气体报警器校准检测周期
  • 如何优化Spring Boot应用的启动时间
  • (Effective C) 2.3 作用域
  • Python 基础 (标准库):堆 heap
  • 动手学深度学习(Pytorch版)代码实践 -卷积神经网络-30Kaggle竞赛:图片分类
  • 【LeetCode】每日一题:数组中的第K大的元素
  • Keil5.38ARM,旧编译器(V5)安装
  • 【perl】脚本编程的一些坑案例
  • MIX OTP——使用 GenServer 进行客户端-服务器通信
  • 2024年云安全发展趋势预测
  • java.io.eofexception:ssl peer shut down incorrectly
  • Unity之HTC VIVE Cosmos环境安装(适合新手小白)(一)
  • 入门JavaWeb之 Response 验证码和重定向
  • 2024-06-26 问AI: 在大数据模型中,deep speed 是什么?
  • mobaxterm x11 转发Ubuntu mac
  • python数据分析实训任务三(‘职业’)
  • vscode连接SSH
  • 金融科技行业创新人才培养与引进的重要性及挑战
  • 【C++题解】1714. 输出满足条件的整数4
  • 如何安装和配置 Django 与 Postgres、Nginx 和 Gunicorn
  • Graphwalker基于模型的自动化测试
  • Macbook M1 Fusion安装Debian/Linux
  • ERP收费模式是怎样的?SAP ERP是如何收费的?
  • 如何实现免交互
  • 浏览器userAgent大全及JS判断当前APP
  • 11.异常(java版)