The code, indented for sanity.
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
short *ptr, n1 = 10, f = 1, f2 = 1;
long i = 0, add, j;
while (f2) {
printf("enter an address:\n");
scanf("%lu", &add);
while (f && f2) {
ptr = (short *) malloc(i * sizeof(short));
if (ptr) {
if (ptr == (short *) add) {
printf("found:%x\t%x\n", ptr, *ptr + i);
f2 = 0;
}
} else
f = 0;
i++;
}
if (!f)
puts("Sorry, the address that you intered is not readable");
for (; i != 0; i = i - 1)
free(ptr + i);
puts("enter 1 if u want to continue, to exit press 0");
scanf("%d", &f2);
}
return 0;
}
The only obvious problem here is your use of malloc() and free().
Inside the loop, you have only one variable (ptr), and MULTIPLE calls to malloc.
This my friend is a massive memory leak.
Outside the loop, you have multiple calls to free, but only ONE valid pointer.
The best thing you could do is something like
Code:
while (f && f2) {
ptr = malloc(i * sizeof(short)); // snipped the cast, it does nothing useful in a C program
// some other code
free(ptr);
}
which would (if nothing else) at least balance up your malloc and free calls.