Witam!
Oto mój program:
#include <stdio.h>
#include <stdlib.h>
struct Point
{
float x, y;
};
typedef struct
{
float l;
struct Point A;
struct Point B;
} line;
int main()
{
int i=0, n=0;
float buf=0;
line *ptr1, *ptr2;
printf("How many points do you need? ");
scanf("%d", &n);
ptr2 = (line*) malloc(sizeof(line)*n);
while(i!=n)
{
ptr1 = (ptr2+i*sizeof(line));
printf("Give me x coordinate for %i point A: ", i+1);
scanf("%f", &buf);
ptr1 -> A.x = buf;
printf("Give me y coordinate for %i point A: ", i+1);
scanf("%f", &buf);
ptr1 -> A.y = buf;
printf("\nGive me x coordinate for %i point B: ", i+1);
scanf("%f", &buf);
ptr1 -> B.x = buf;
printf("Give me y coordinate for %i point B: ", i+1);
scanf("%f", &buf);
ptr1 -> B.y = buf;
++i;
printf("\n");
}
i=0;
while(i!=n) {
ptr1 = (ptr2+i*sizeof(line));
printf ("Line no. %d: A=(%f, %f), B=(%f, %f)\n", i+1, ptr1->A.x, ptr1->A.y, ptr1->B.x, ptr1->B.y);
++i;
}
printf("\n");
free(ptr2);
system("PAUSE");
return 0;
}
Program się kompiluje, nie ma żadnych błędów. Problem pojawia się podczas wykonywania programu. Otóż "kochany" Visual Studio, w który "zaopatrzyła" nas uczelnia, przerywa mi program i informuje mnie, iż "HEAP: Free Heap block 501560 modified at 5016b4 after it was freed. Windows has triggered a breakpoint in blabla.exe". Nie jest to błąd, przez który utknąłem nad resztą projektu, ale czysto teoretycznie interesuje mnie, gdzie jest problem i jak można mu zapobiec.
Z góry dziękuję za pomoc ;)