动态Array和动态KeyValue(优化版my_table,segment段分配)

my_table.h

#ifndef MY_GLOB_HEAD
#define MY_GLOB_HEAD

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

#define LEN_SMALL 100
#define LEN_NORMAL 1000
#define LEN_BIG 10000

typedef struct my_row
{
    long id;
    char name[LEN_SMALL];
    int score;
    void *pointer;
} t_my_row;

typedef struct my_table
{
    int size;
    int curr;
    t_my_row *array;
} t_my_table;

t_my_table table_create(int size);
void table_extend(t_my_table *table, int addsize);
void table_insert(t_my_table *table, t_my_row *new_row);
void table_print(t_my_table *table);

#endif

my_table.c

#include "my_table.h"

t_my_table table_create(int size)
{
    t_my_row *rows = (t_my_row *)calloc(size, sizeof(t_my_row));
    t_my_table table;
    table.size = size;
    table.curr = -1;
    table.array = rows;
    return table;
}

void table_extend(t_my_table *table, int addsize)
{
    int newsize = (addsize + table->size) * sizeof(t_my_row);
    t_my_row *rows = (t_my_row *)realloc(table->array, newsize);
    if (rows == NULL)
    {
        fprintf(stderr, "Error - unable to allocate required memory\n");
        exit(-1);
    }
    table->size += addsize;
    table->array = rows;
}

void table_insert(t_my_table *table, t_my_row *new_row)
{
    if (table->curr == table->size - 1)
    {
        table_extend(table, table->size);
    }
    table->curr++;
    table->array[table->curr].id = new_row->id;
    strcpy(table->array[table->curr].name, new_row->name);
    table->array[table->curr].score = new_row->score;
    table->array[table->curr].pointer = new_row->pointer;
}

void table_print(t_my_table *table)
{
    int i;
    for (i = 0; i <= table->curr; i++)
    {
        printf("%ld  |  ", table->array[i].id);
        printf("%s  |  ", table->array[i].name);
        printf("%d  |  ", table->array[i].score);
        printf("%p  |  \n", table->array[i].pointer);
    }
}

main.c

#include "my_table.h"

int main()
{
    t_my_row newrow;
    newrow.id = 123;
    strcpy(newrow.name, "aaa");
    newrow.score = 666;
    newrow.pointer = NULL;

    t_my_table table = table_create(2);
    int i;
    for (i = 1; i <= 1000000; i++)
    {
        table_insert(&table, &newrow);
    }
    table_print(&table);

    return 0;
}

相关推荐

  1. 动态Array动态KeyValue(优化my_table,segment分配)

    2024-03-23 01:04:02       45 阅读
  2. C语言动态内存分配之malloc(初阶

    2024-03-23 01:04:02       48 阅读
  3. 最大子最大子矩阵|动态规划

    2024-03-23 01:04:02       37 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-23 01:04:02       99 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-23 01:04:02       107 阅读
  3. 在Django里面运行非项目文件

    2024-03-23 01:04:02       90 阅读
  4. Python语言-面向对象

    2024-03-23 01:04:02       98 阅读

热门阅读

  1. Android中的进程间通讯

    2024-03-23 01:04:02       44 阅读
  2. 统计单词数

    2024-03-23 01:04:02       34 阅读
  3. docker-compose 启动服务还需要pm2 守护进程?

    2024-03-23 01:04:02       51 阅读
  4. Go语言学习03-字符串

    2024-03-23 01:04:02       40 阅读
  5. Opencv面试题

    2024-03-23 01:04:02       39 阅读
  6. 千帆AppBuilder开发参考-应用API调用说明

    2024-03-23 01:04:02       43 阅读