Witam, mam problem ze zwalnianiem pamięci w funkcji createitem. Gdzie bym nie umieścił free() to program się sypie. Już nie wiem o co tu chodzi. Dziwne jest też to, że w Visualu po wpisaniu danych pojawia się błąd, a w Devie wszystko działa tylko zamiast właściwych danych wyświetlają się krzaki. Komunikat błędu wygląda tak:"CRT detected that applicaton wrote to memory after end of heap buffer.", a przecież to nie prawda(przynajmniej tak mi się wydaje). Dodam jeszcze, że bez zwalniania pamięci wszystko działa jak trzeba. Będę wdzięczny za pomoc.
Oto fragment mojego programu:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
typedef struct LIST List;
typedef struct DATA Data;
struct DATA
{
char* word;
char* replacer;
};
struct LIST
{
Data data;
List* next;
};
List* head;
int showlist()
{
List *temp;
system("cls");
temp = head;
if (temp == NULL)
{
printf("Brak elementow\n");
getch();
return 1;
}
else
printf("WYRAZY: ZAMIENNIKI:\n");
while (temp != NULL)
{
printf("%-12s%-12s\n", temp->data.word, temp->data.replacer);
temp = temp->next;
}
getch();
return 0;
}
List* createitem()
{
List *element;
char *word, *replacer;
char bufor[30];
system("cls");
printf("Podaj wyraz oraz zamiennik, ktore chcesz dodac do listy\n\n");
printf("Wyraz: ");
gets(bufor);
word = (char*)malloc((strlen(bufor)+1) * sizeof(char));
strcpy(word, bufor);
printf("Zamiennik: ");
gets(bufor);
replacer = (char*)malloc((strlen(bufor)+1)*sizeof(char));
strcpy(replacer, bufor);
element = (List*)malloc(sizeof(List));
element->next = NULL;
element->data.word = word;
element->data.replacer = replacer;
free(word);
free(replacer);
return element;
}
void additem()
{
List *tmp = createitem();
if (head != NULL)
tmp->next = head;
head = tmp;
}
int main(int argc, char **argv)
{
additem();
showlist();
return 0;
}