Problem
Find as many issues as possible and correct them:
malloc(n) allocates n bytes in the heap.
int main(void)
{
int *ptr = (int *) malloc(10);
for(int i =0; i <10; i++)
{
*ptr++ = i;
}
free(ptr);
}
- line 3 -- 10 integers should have the size of 10 * sizeof(int);
- line 8 -- ptr is not original pointer allocated -- free(ptr - 10)
- ptr is used without checking its validity
- missing header files <stdlib.h> or <malloc.h>
- misssing return statement, but it is ok for main() because it is only one function that can implicitly return 0