我的程序在一定数量的递归下一直崩溃,而调试时我偶然发现了一些valgrind错误。
我一直在尝试解决这些问题?但是我一定会误解该错误,如果有人能够对这些错误进行扩展的解释,将不胜感激。
多个:
==5367== Invalid read of size 1
==5367== at 0x100000F60: ft_validator (validator.c:29)
==5367== by 0x100001139: ft_sort_list (sort_list.c:35)
==5367== by 0x100000DD5: main (main.c:41)
我的ft_validator:
/*
** Checks if the tetrimino's are valid and
** if each Tetrimino Block touches atleast another block.
** Check if the read '#' tetrimino block has any adjacent blocks,
** if not, we can discard the tetrimino.
** @return 0 = Valid tetrimino set, -1 is invalid tetrimino set
** @param buf represents the string containing the tetrimino sample set.
*/
int ft_validator(char *buf, int blocks, int adjacent, int dots)
{
int index;
index = 0;
while (buf[index] != '\0')
{
if ((buf[index] != '#' && buf[index] != '.' && buf[index] != '\n'))
return (-1);
if (buf[index] == '#')
{
blocks++;
if (buf[index - 1] == '#')
adjacent++;
if (buf[index + 1] == '#')
adjacent++;
if (buf[index + 5] == '#')
adjacent++;
if (buf[index - 5] == '#')
adjacent++;
}
buf[index] != '.' || dots++;
index++;
}
if (adjacent < 6 || blocks != 4 || dots != 12)
return (-1);
return (0);
}
这:
==5367== Address 0x100b58ca5 is 0 bytes after a block of size 21 alloc'd
==5367== at 0x1000991E6: malloc (in /Users/dvan-boc/.brew/Cellar/valgrind/3.14.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==5367== by 0x100000E54: ft_read (reader.c:38)
==5367== by 0x100000DC3: main (main.c:40)
==5367==
/*
**
** @param file represents the file name to be read.
** @return returns an integer
** Reads a file containing multiple tetrmino's ranging from 1 to 26 tetrimino's
** Every # is recognized as a Tetrimino_Block and every . is ignored
** @PURPOSE:
** The purpose of the tetrimino's is to find the smallest
** possible solution to the smallest possible square
**
** EXAMPLE:
** Coords start at 0, 0 and end at 4, 4
*/
int ft_read(const char *file, char **buf)
{
int fd;
int t_index;
int ret;
t_index = 0;
fd = open(file, O_RDONLY);
buf[t_index] = malloc(BUFF_SIZE * sizeof(**buf));
ret = read(fd, buf[t_index], BUFF_SIZE);
t_index++;
while (ret == 21 && t_index < 27)
{
buf[t_index] = malloc(BUFF_SIZE * sizeof(**buf));
ret = read(fd, buf[t_index], BUFF_SIZE);
t_index++;
}
if (ret != 20 || t_index == 27)
return (-t_index);
buf[t_index] = NULL;
return (t_index);
}