0
#include <stdlib.h>
#include <stdio.h>
#define SIZE 15 
int main()
  {

	  int *a, i;
	  
      a = (int*) malloc(SIZE*sizeof(int));

      for (i=0; i<SIZE; i++)
          *(a + i) = i * i;
      for (i=0; i<SIZE; i++)
          printf("%d\n", *a++);
	  
      
	  free(a);
      return 0;
  }

Why Is My Program Crashing??

3
Contributors
3
Replies
5
Views
11 Years
Discussion Span
Last Post by Drowzee
0

the parameter to free() must be the same pointer that was returned by malloc(), calloc() or realloc(). Your program destroyed that pointer in the printf() statement.

Why don't you use array a just like any other array instead of using pointer arithemic

for (i=0; i<SIZE; i++)
          a[i] = i * i;

for (i=0; i<SIZE; i++)
          printf("%d\n", a[i]);
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.