Q:

bugs in matrix multiplication when I optimize my L1 Cache using

I write a program to optimize matrix multiplication using L1 Cache optimization in ubuntu.
and I got a Segmentation fault (core dumped) error.
I checked out all my malloc function but didn't find the bug.
I don't know how to fix it.
here is the code:
#include<sys/types.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sched.h>
#include <time.h>
#include <sys/time.h>
#include <pthread.h>
#include <sys/wait.h>
#define N 100
#define THREAD_NUM 1

void thread_func(void *arg){
 printf("thread start \n"); 
int i,j,k,i2,j2,k2;

double **a;   
double **b;
double **c;
double *aa,*bb,*cc;  

    a = (double**)malloc(sizeof(double*)*N);   
    for (i=0; i<N; i++)   
        a[i] = (double*)malloc(sizeof(double)*N);

    b = (double**)malloc(sizeof(double*)*N);   
    for (i=0; i<N; i++)   
        b[i] = (double*)malloc(sizeof(double)*N);

    c = (double**)malloc(sizeof(double*)*N);   
    for (i=0; i<N; i++)   
        c[i] = (double*)malloc(sizeof(double)*N);

    srand((unsigned)time(NULL));
    for(i = 0; i < N; i++)
        for(j = 0; j <N; j++)
            *(*(a+i)+j) = 555.123;
    for(i = 0; i < N; i++)
            for(j = 0; j <N; j++){
                *(*(b+i)+j) = 666.456;
            }
    for(i = 0; i < N; i++)
        for(j = 0; j <N; j++){
            *(*(c+i)+j) = 0.0;
        }
/*
    for(i = 0; i < N; i+=8)
    for(j = 0; j < N; j+=8) 
    for(k = 0; k < N; k+=8)
        for(i2=0,cc=&(*(*(c+i)+j)),aa=&(*(*(a+i)+k));i2<8;++i2,cc+=N,aa+=N)
        for(k2=0,bb=&(*(*(b+k)+j));k2<8;++k2,bb+=N)
        for(j2=0;j2<8;++j2){
            *(cc+j2)+=*(aa+k2)*(*(bb+j2));  
            //printf("cc ,*(cc+j2)%d %d\n",cc,*(cc+j2));

        }
*/  

    for(i = 0; i < N; i+=8)
    for(j = 0; j < N; j+=8) 
    for(k = 0; k < N; k+=8)
        for(i2=0, cc=(*(c+i)+j), aa=(*(a+i)+k);  i2<8;  ++i2,cc+=N,aa+=N)
        for(k2=0, bb=(*(b+k)+j); k2<8; ++k2, bb+=N)
        for(j2=0; j2<8; ++j2){
            *(cc+j2)+=(*(aa+k2))*(*(bb+j2));    
        }

 printf("ready to free \n"); 
//free(aa); free(bb);free(cc);
for (i=0; i<N; i++)   
        free(a[i]); free(b[i]);free(c[i]);  
   free(a);free(b);free(c);
 printf("thread done \n");  

}

int main(void) 
{ 
 printf("program start \n"); 
int i;

pthread_t handle[THREAD_NUM];

for( i=0;i<THREAD_NUM;i++)
pthread_create(&handle[i],NULL,(void*)thread_func,NULL);

for(i=0;i<THREAD_NUM;i++)
    pthread_join(handle[i],NULL);

perror("a*b leaveing");
    return 0; 
} 

A:

You wrote:
for (i=0; i<N; i++)   
    free(a[i]); free(b[i]);free(c[i]);

The compiler sees that as:
for (i=0; i<N; i++)   
    free(a[i]);
free(b[i]);
free(c[i]);

This must be trying to access/free data out of bounds. It also leaks a lot of memory.  Use braces and spaces — they're cheap.  Use multiple lines; they're cheap too.
for (i = 0; i < N; i++)
{
    free(a[i]);
    free(b[i]);
    free(c[i]);
}

You should also check that your memory allocation succeeded each time, though you'd be crashing if you tried to use an unsuccessfully allocated array.
Why would you go around writing:
*(*(a+i)+j) = 555.123;

The compiler will treat that the same as if you wrote a[i][j], which is less painful to type and clearer to read.

