#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??

Recommended Answers

All 3 Replies

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]);

can u correct the previous code

sunny? Um... It's a copy/paste fix.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.