-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
  • 10,379
  • 42
  • 54
  • 85
aranyak
  • 1
  • 2
  • 3
    This isn't clear. What are the symptoms of your problem? – Oliver Charlesworth Jul 25 '15 at 08:16
  • I can't free the memory after using free() statement as I checked the first row for 11*11 matrix by using the portion of the code which is inactive now in my code by making some adjustment on boundary of the circle. – aranyak Jul 25 '15 at 08:21
  • 3
    @Beman "I can't free the memory" isn't specific enough. Anyway, `free` is commented out, so there is no way it could free anything. – juanchopanza Jul 25 '15 at 08:24
  • You ar will contain `0` till `p` is not greater than `2.3 *10^5` that's a huge number . – ameyCU Jul 25 '15 at 08:31
  • First indent your code properly. – Jayesh Bhoi Jul 25 '15 at 08:40
  • 1
    possible duplicate of [What happens to the data in memory deallocated by free()?](http://stackoverflow.com/questions/29586074/what-happens-to-the-data-in-memory-deallocated-by-free) – Blastfurnace Jul 25 '15 at 08:42
  • I see the answers in "What happens to.......by free" but can't find any solution. – aranyak Jul 25 '15 at 08:57
  • 1
    @Beman: There is no "solution". It is an error to read or write memory after you've called `free` on it. Don't do that, your code is broken. – Blastfurnace Jul 25 '15 at 08:59
  • Now I replace free(ar) by free(ar-(n*n)) and got different output. – aranyak Jul 25 '15 at 09:05
  • @Beman: Now you're just guessing. Put this code away and [read a good `C` book](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). – Blastfurnace Jul 25 '15 at 09:08
  • 2
    You will write out 1000 times 'I must not access memory after I have freed it because to do so is undefined behaviour'. – Martin James Jul 25 '15 at 09:32
  • That edit changed the question quite significantly, troll... – autistic Jul 25 '15 at 10:00

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,146
  • 2
  • 24
  • 40
  • 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. – aranyak 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