2
Good afternoon, I’m having difficulty creating a program that increases the size of a structure called list , similar to an array, whenever the occupancy rate of this list is more than 80% filled so I would like to increase the size 2 times , using realloc , my problem is that I can’t see more than 30 elements of the list , if no while is i < 30 or another higher number starts to go wrong(Segmentation fault) I would like to know why, thank you.
#include "buffer.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
Buff init_buff() {
int i = 0;
Buff x = malloc(sizeof(struct buff));
x - > size = 1;
x - > used = 0;
x - > lis = malloc(sizeof(struct lista));
while (i < 10) {
if (i % 2 == 0) x = load_buffer(x, "par");
else x = load_buffer(x, "impar");
i++;
}
return x;
}
Buff load_buffer(Buff x, char * l) {
float taxa_ocupacao = x - > used / x - > size;
//primeiro caso
if (taxa_ocupacao > 0.8 || taxa_ocupacao == 0) {
x - > lis = realloc(x - > lis, x - > size * 2 * (sizeof(struct lista)));
x - > size *= 2;
}
x - > lis[x - > used].phrase = l;
x - > used++;
return x;
}
Good afternoon! Post the code itself instead of an image. So we can take the code and test to better help :)
– Cleber Griff