0

My code crash coz free() doesn't want to clear array. First of all i create array and add to there a numbers. After that i enter K (k < m) and creates new matrix MxK. k - amount of number of first array in line. And if the number are over , should fill in the rest matrix with zeros. I use while for reuse, but my code crash when i use big array and big matrix. With what it can be connected?

`int main() {
    while(1){
    int m,k;
    printf("Enter size of array: ");
    int res;
    do
    {
        res=scanf("%d",&m);
        if(res!=1)printf("it should be a number!");

    }
    while(res!=1);

    int * a = (int*) malloc(m*sizeof(int)); 


    int i;
    for (i = 0; i < m; i++)
    {
        printf("a[%d] = ", i);
            do
            {
            res=scanf("%d", &a[i]);
            flush();
            if(res!=1) printf("it should be a number!\n");
            }
            while(res!=1);
    }

    for (i = 0; i < m; i++)
        printf("%d ", a[i]);

    printf("\nEnter K: ");
    do
    {
    res=scanf("%d",&k);
    flush();
    if(res!=1 || k>m) printf("k<m must be or enter number\n");
    }
    while(res!=1 || k>m);

    int all=k*m;
    for(i=m;i<all;i++)
    {
        a[i] = 0;
    }
    int j;
    int n=0;
    int s[i][j];
    for(i=0;i<m;i++)
    {
    for(j=0;j<k;j++)
    {
    s[i][j] = a[n];
    printf("%d ",s[i][j]);
    n++;
    }
    printf( "\n" );
    }
    for(i=0;i<m;i++)
    {
    for(j=0;j<k;j++)
    {
        free(s[i][j]);
    }
    }   
int povtor;   ----- exit using break
char temp[10];
printf("Continue?(1 - No, everything - Yes): ");
gets(temp);
povtor = atoi(temp);
if (povtor == 1 ) break; 

}
return 0;
}`
  • `int j;` followed by `int s[i][j];`... What is the value of `j` at that point? I suggest you learn how to use [rubber duck debugging](https://en.wikipedia.org/wiki/Rubber_duck_debugging). – Some programmer dude Apr 21 '19 at 10:55

2 Answers2

2

Your code has a lot of problems.

First, you can only free something that was allocated with malloc, calloc, or realloc. This doesn’t apply to s[i][j].

But that’s only one issue. You allocate a to hold m elements:

int * a = (int*) malloc(m*sizeof(int));

but then you try to write past the end of the array here:

int all=k*m;
for(i=m;i<all;i++)
{
    a[i] = 0;
}

You need to resize a before attempting that, otherwise you are writing over memory you don’t own:

int *tmp = realloc(a, sizeof *a * all );
if ( !tmp )
{
  // resize failed, exit with error
}
a = tmp;
for( i = m; i < all; i++ )
{
  a[i] = 0;
}

Then there’s this:

int j;
int n=0;
int s[i][j];

What is the value of j at this point? For that matter, what is the value of i? Are you sure you don’t mean s[m][k]?

John Bode
  • 119,563
  • 19
  • 122
  • 198
1

You are trying to free an int, and not a pointer : int s[i][j] is an array of array of int, hence s[][] is a int.

Even though, s is automatic memory allocated : basically, its a variable stored in the stack so you don't need to free it. Trying to free static variable results in crash. This question may help you.

By the way, you have a lot of uninitialized variable, resulting in conditionals jumps and possible crashs : for example, you declare int j without a value and after you use it to declare your array of array.

Finally, don't use gets. This function is dangerous, use fgets instead. I suggest you read the manual page of gets to understand why.

Antoine. F
  • 46
  • 5