c语言动态数组的实现

动态数组是在程序运行时动态分配内存空间的数组,可以根据需要随时改变大小。在C语言中,动态数组通常通过指针和malloc函数来实现。

  1. 使用malloc函数动态分配内存空间:
cpp 复制代码
int *arr;
int size = 10;
arr = (int*)malloc(size * sizeof(int));
  1. 使用realloc函数调整动态数组的大小
cpp 复制代码
int new_size = 20;
arr = (int*)realloc(arr, new_size * sizeof(int));
  1. 使用free函数释放动态数组占用的内存
cpp 复制代码
free(arr);
  1. 使用指针操作动态数组元素:

    arr[0] = 10;
    arr[1] = 20;

需要注意的是,动态数组在使用完毕后应该及时释放内存,避免内存泄漏。另外,使用动态数组时需要注意数组下标的范围,避免访问越界导致程序崩溃。

动态数组的实现

模块化的编程方式:将头文件,函数,和main文件分离出来

函数头文件:对函数和结构体进行声明

函数头文件的代码

cpp 复制代码
#ifndef __MALLOC_H_
#define __MALLOC_H_
#include <stdbool.h>
// 定义符号常量,确定数组拥有的元素个数
#define VECTOR_INIT_CAPACITY 1
// 定义学生结构体:包含学生 id 名字,性别...
struct student {
	int id;
	char name[20];
	int gender;
	int mark;

};

// 动态数组vector包含动态数组的4个方法
struct vector {
	bool (*append) (struct vector* pVec, struct student data);
	struct student(*get)(struct vector* pVec, int index);
	void (*clear)(struct vector* pVec);
	void (*remove)(struct vector* pVec, int index);

	struct student* pData;// 数组首元素指针
	int size; // 数组已经存放的大小
	int capacity; // 数组容器的容量


};
void vectorInit(struct vector* pVec); // 初始化函数
bool vectorAppend(struct vector* pVec, struct student data);// 函数的添加
struct student vectorGet(struct vector* pVec, int index); // 获取数据
void vectorRemove(struct vector* pVec, int index); // 移除数组中的元素
void vectorClear(struct vector* pVec); // 清空数组
void vectorDestroy(struct vector* pVec); // 销毁数组

#endif

数组实现函数UTIL类别

函数具体实现代码

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "MallocLineData.h"
// 初始化函数
void vectorInit(struct vector* pVec) {
    // 初始化对象的方法
    pVec->get = vectorGet;
    pVec->append = vectorAppend;
    pVec->remove = vectorRemove;
    pVec->clear = vectorClear;


    pVec->pData = (struct student*)malloc(sizeof(struct student) * VECTOR_INIT_CAPACITY);
    pVec->size = 0;
    pVec->capacity = VECTOR_INIT_CAPACITY;
}
// 添加函数的代码
bool vectorAppend(struct vector* pVec, struct student data) {
    if (pVec->size >= pVec->capacity) {
        struct student* newData = (struct student*)
            realloc(pVec->pData, pVec->capacity * sizeof(struct student) * 2);
        if (newData == NULL) {
            return false;
        }
        pVec->pData = newData;
        pVec->capacity = 2 * pVec->capacity;
    }
    pVec->pData[pVec->size] = data;
    pVec->size++;
    return true;
}
// 获取数组元素的首地址
struct student vectorGet(struct vector* pVec, int index) {
    return pVec->pData[index];
}
// 移除数组中的元素
void vectorRemove(struct vector* pVec, int index) {
    for (int i = index; i < pVec->size - 1; i++) {
        pVec->pData[i] = pVec->pData[i + 1];
    }
    pVec->size -= 1;
}
//清除数组中元素的方法
void vectorClear(struct vector* pVec) {
    if (pVec->pData != NULL) {
        free(pVec->pData);
    }
    pVec->pData = (struct student*)malloc
    (sizeof(struct student) * VECTOR_INIT_CAPACITY);
    pVec->size = 0;
    pVec->capacity = VECTOR_INIT_CAPACITY;
}
// 销毁不需要使用的数组
void vectorDestroy(struct vector* pVec) {
    if (pVec->pData == NULL) {
        return;

    }
    free(pVec->pData);
    pVec->pData = NULL;
    pVec->size = 0;
    pVec->capacity = 0;
}

main函数代码

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "MallocLineData.h"

int main()
{
   // 对象的声明
	struct vector vec;
	vectorInit(&vec);
	struct student s1 = { 1,"小黄",1,90 };
	vec.append(&vec, s1);
	for (int i = 0; i < vec.size; i++) {
		struct student s = vec.get(&vec, i);
		printf("%d %s %d %d\n", s.id, s.name, s.gender, s.mark);
	}
	printf("size:%d\n", vec.size);
	printf("capacity:%d\n", vec.capacity);

	// remove方法的测试
	vec.remove(&vec, 0);
	for (int i = 0; i < vec.size; i++) {
		struct student s = vec.get(&vec, i);
		printf("%d %s %d %d\n", s.id, s.name, s.gender, s.mark);
	}
	printf("size:%d\n", vec.size);
	printf("capacity:%d\n", vec.capacity);

	// 调用清除数据的方法
	vec.clear(&vec);
	for (int i = 0; i < vec.size; i++) {
		struct student s = vec.get(&vec, i);
		printf("%d %s %d %d\n", s.id, s.name, s.gender, s.mark);
	}
	printf("size:%d\n", vec.size);
	printf("capacity:%d\n", vec.capacity);

	// 调用销毁数据的方法
	vectorDestroy(&vec);
	for (int i = 0; i < vec.size; i++) {
		struct student s = vec.get(&vec, i);
		printf("%d %s %d %d\n", s.id, s.name, s.gender, s.mark);
	}
	printf("size:%d\n", vec.size);
	printf("capacity:%d\n", vec.capacity);
}

动态数组运行结果展示

相关推荐
lhbweilai34 分钟前
深挖FreeRTOS内核实现(三)
c语言·stm32·单片机
一只小小的芙厨41 分钟前
【线性DP】
学习·算法·动态规划
xxwl5851 小时前
数据结构知识点和代码实现总结(C语言实现)
c语言·开发语言·数据结构
奋发向前wcx1 小时前
CSP-J复赛模拟赛5 王晨旭补题 2026.10.5
算法
小灰灰搞电子2 小时前
C语言 <time.h> 头文件详解
c语言·time.h
罗湖老棍子3 小时前
C Looooops(信息学奥赛一本通- P1640)
算法·数论·同余·裴蜀定理·扩展欧几里得
C++ 老炮儿的技术栈3 小时前
sizeof操作符
c语言·c++·人工智能·mfc·c
wuminyu4 小时前
ForkJoinPool内部WorkQueue的Lock-Free数组操作以及并发任务窃取原理剖析
java·linux·c语言·jvm·c++
她说彩礼65万4 小时前
C语言 堆区和栈区
java·linux·c语言
飘尘5 小时前
从"算子"到"AI Infra":大模型背后看不见的那群人在忙什么
人工智能·算法·面试