大家好!我是编码小哥,欢迎关注,持续分享更多实用的编程经验和开发技巧,共同进步!
本例是一个动态数组的例子,实现数据的增加、删除、根据索引修改数值、获取数值。
dynamic_array.c
#include "dynamic_array.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
dynamic_array_t *init_dynamic_array()
{
dynamic_array_t *da = malloc(sizeof(dynamic_array_t));
da->items = calloc(DEFAULT_CAPACITY, sizeof(void *));
da->capacity = DEFAULT_CAPACITY;
return da;
}
void *add(dynamic_array_t *da, const void *value)
{
if (da->size >= da->capacity)
{
void **newItems = realloc(da->items, (da->capacity <<= 1) * sizeof(void **));
//free(da->items);
da->items = newItems;
}
void *copy_value = retrive_copy_of_value(value);
da->items[da->size++] = copy_value;
return copy_value;
}
void *put(dynamic_array_t *da, const void *value, const unsigned index)
{
if (!contains(da->size, index))
return INDEX_OUT_OF_BOUNDS;
free(da->items[index]);
void *copy_value = retrive_copy_of_value(value);
da->items[index] = copy_value;
return copy_value;
}
void *get(dynamic_array_t *da, const unsigned index)
{
if (!contains(da->size, index))
return INDEX_OUT_OF_BOUNDS;
return da->items[index];
}
void delete (dynamic_array_t *da, const unsigned index)
{
if (!contains(da->size, index))
return;
for (unsigned i = index; i < da->size; i++)
{
da->items[i] = da->items[i + 1];
}
da->size--;
free(da->items[da->size]);
}
unsigned contains(const unsigned size, const unsigned index)
{
//printf("size:%d,index:%d\n", size, index);
if (size >= 0 && index < size)
return 1;
printf("index [%d] out of bounds!\n", index);
return 0;
}
void *retrive_copy_of_value(const void *value)
{
void *value_copy = malloc(sizeof(void *));
memcpy(value_copy, value, sizeof(void *));
return value_copy;
}
dynamic_array.h
#ifndef __DYNAMIC_ARRAY__
#define __DYNAMIC_ARRAY__
#define DEFAULT_CAPACITY 1 << 4
#define INDEX_OUT_OF_BOUNDS NULL
typedef struct dynamic_array
{
void **items;
unsigned size;
unsigned capacity;
} dynamic_array_t;
extern dynamic_array_t *init_dynamic_array();
extern void *add(dynamic_array_t *da, const void *value);
extern void *put(dynamic_array_t *da, const void *value, unsigned index);
extern void *get(dynamic_array_t *da, const unsigned index);
extern void delete (dynamic_array_t *da, const unsigned index);
unsigned contains(const unsigned size, const unsigned index);
extern void *retrive_copy_of_value(const void *value);
main.c
#include "dynamic_array.h"
int main()
{
dynamic_array_t *da = init_dynamic_array();
for (int i = 1; i <= 50; i++)
{
add(da, &i);
}
delete (da, 10);
int value = 999;
put(da, &value, 0);
#if 1
//value = 666;
int another_value = 666;
add(da, &another_value);
#endif
#if 0
for (int i = 0; i < da->size; i++)
{
printf("value[%d]=%d\n",i, *(int *)get(da, i));
}
#endif
int value_for_invalid_index = 10000;
//put(da, &value_for_invalid_index, 150);
//put(da, &value_for_invalid_index, 65);
put(da, &value_for_invalid_index, 49);
for (int i = 0; i < da->size; i++)
{
printf("value[%d]=%d\n",i, *(int *)get(da, i));
}
free(da->items);
return 0;
}
Makefile:
CC = gcc
CFLAGS = -g -Wall
all: main
main: main.o dynamic_array.o
$(CC) $(CFLAGS) $^ -o $@
dynamic_array.o: dynamic_array.c
$(CC) $(CFLAGS) -c $^
clean:
rm *.o main
刚开始运行结果是这样的:
于是乎这样调试:
利用core文件调试:
先设置一下
ulimit -c unlimited
运行一下在当前目录下生成core文件
root@ubuntu-virtual-machine:/home/ubuntu/test# gdb main
gdb结合core文件调试
代码修正后运行结果:
value[0]=999
value[1]=2
value[2]=3
value[3]=4
value[4]=5
value[5]=6
value[6]=7
value[7]=8
value[8]=9
value[9]=10
value[10]=12
value[11]=13
value[12]=14
value[13]=15
value[14]=16
value[15]=17
value[16]=18
value[17]=19
value[18]=20
value[19]=21
value[20]=22
value[21]=23
value[22]=24
value[23]=25
value[24]=26
value[25]=27
value[26]=28
value[27]=29
value[28]=30
value[29]=31
value[30]=32
value[31]=33
value[32]=34
value[33]=35
value[34]=36
value[35]=37
value[36]=38
value[37]=39
value[38]=40
value[39]=41
value[40]=42
value[41]=43
value[42]=44
value[43]=45
value[44]=46
value[45]=47
value[46]=48
value[47]=49
value[48]=50
value[49]=10000
其实,一开始运行的时候,报错日志就给出了答案,并不需要这么大费周章地调试,debug就像生活一样,有时问题看似很复杂,其实回过头来看一看,答案就在眼前。