witam,
mam prostą listę jednokierunkową i wygląda na to, że mam wyciek pamięci.
Cały kod: http://wklej.org/id/2269402/
Kod potrzebny do zorientowania się co i jak:
#define MALLOC(type) (type*)malloc(sizeof(type))
typedef struct kosmita{
struct kosmita *next;
char name[8]; // nazwa kosmity
unsigned short int tent_amount; // ilosc czolkow
}kosmita_t;
void add_elements(kosmita_t *head, size_t n)
{
kosmita_t *current;
current = head;
for(int i = 0; i < n; ++i)
{
while(current->next != NULL)
current = current->next;
current->next = MALLOC(kosmita_t);
if(current->next == NULL)
{
errno = ENOMEM;
perror("Nie mozna bylo zaalokowac pamieci!");
exit(EXIT_FAILURE);
}
current = current->next;
current->tent_amount = rand() % MAX_TENT + 1;
rand_str(current->name, 8);
}
current->next = NULL;
}
int main()
{
int counter = 0;
time_t t;
srand((unsigned int)&t);
char choice = 'o';
size_t n; // niech bedzie n= 10e6
kosmita_t * head;
head = MALLOC(kosmita_t);
if(head == NULL)
{
errno = ENOMEM;
perror("Nie mozna bylo zaalokowac pamieci!");
exit(EXIT_FAILURE);
}
head->next = NULL;
head->tent_amount = 1;
strcpy(head->name, "first");
while(choice != 'q')
{
scanf("%c", &choice);
while(getchar() != '\n'); // clear buffer
add_elements(head, n);
while(head != NULL)
{
kosmita_t *temp;
temp = head;
head = head->next;
free(temp);
}
}
return 0;
}
Po dodaniu około miliona elementów i przejściu przez while'a na końcu main
zajmuje 2-3 MB pamięci, podczas gdy tuż po uruchomieniu programu było koło 500KB. Gdzie jest błąd?