-7

I am new to c programming.I need a (10^5)*(10^5) matrix within which I define a circular boundary.The matrix entries inside boundary will be 1 and outside 0.For checking I have started with (1000*1000) and it prints the whole matrix if I don't use the free(ar) statement.And when I am using (11*11) matrix and changing the circular boundary accordingly and checking the free(ar)'s action by printing first row it shows previous entries.I am using windows 64 bit machine,and code::blocks 13.12 IDE.This is my code for (11*11) matrix:

    #include <stdio.h>
    #include <stdlib.h>
    #include<math.h>
    #include<conio.h>
    #include<time.h>
    #define NULL ((void *)0)
    main()
    {
        int i,j,n,k,*ar;
        float p;

        printf("Enter n::::");
        scanf("%d",&n);

        ar=(int*)calloc((n*n),sizeof(int));

        if(ar==NULL)
        {
            printf("Memory allocation error");
            exit(1);
        }

        printf("\nThe matrix is::\n");

        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                p=((i-5)*(i-5)+(j-5)*(j-5));
                if(p<16)
                {
                    ar[i*n+j]=0;
                    printf("%d",ar[i*n+j]);
                }
                else
                {
                    ar[i*n+j]=1;
                    printf("%d",ar[i*n+j]);
                }
            }
            printf("\n");
        }

        printf("\nPRINTING COMPLETE\n");            

        free(ar-(n*n));

        printf("\nThe first row of the matrix is\n\n");

        for(k=0;k<11;k++)
        {
            printf("%d\t",*(ar+k));
        }
    }

The output is:

      Enter n::::11

      The matrix is::
      11111111111
      11111111111
      11100000111
      11000000011
      11000000011
      11000000011
      11000000011
      11000000011
      11100000111
      11111111111
      11111111111

      PRINTING COMPLETE

      The first row of the matrix is

        1         1       1       1       1       1       1       1                                          

        1         1       1
mpromonet
  • 11,326
  • 43
  • 62
  • 91

1 Answers1

1
  free(ar);  

Thus accessing ar after freeing it causes undefined behaviour . So if you don't want to access it after freeing it assign it to NULL(Just a good practice) after

  free(ar);
  ar=NULL;

so that if you try to access it your program will crash so you know that error has occurred.

ameyCU
  • 16,489
  • 2
  • 26
  • 41
  • I want to say that free(ar) is not freeing the memory.If you see the edited question there I printed first row after freeing and that gives me my previous matrix entries, and if I print other rows in the same way there also the same result.So memory is not cleaned by free(ar-(n*n)) statement. – Satyajit Ghosh Jul 25 '15 at 09:20
  • @Beman You see there first two values are not n `ar` .Those value could be anything .Please notice in your output. You are invoking undefined behaviour thus it can print anything . – ameyCU Jul 25 '15 at 09:31