分段错误(核心转储)。指针问题

时间:2015-11-14 19:12:19

标签: c pointers segmentation-fault malloc

我正在尝试执行以下代码。它通过编译但在运行时因错误分段错误(核心转储)而失败。我试图模拟缓存,但我遇到了一些指针不一致(可能)。 `

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#define _GNU_SOURCE

int hit=0,miss=0,evic=0,verbose=0;

typedef struct{
    long int tag;
    int valid;
    int age;
}cLine;

typedef struct{
    cLine *Lines;
}cSet;

typedef struct{
    cSet *Sets;
}cache;

cache my_cache(int s, int E, int b){
    cache cach; 
    int i=0,j=0;
    cach.Sets=malloc(sizeof(cSet)*(1<<s));
    for (i = 0; i < (1<<s); i++) {
      cach.Sets[i].Lines = malloc(sizeof(cLine) * (1<<E));
      for (j=0;j<(1<<E);j++){
        cach.Sets[i].Lines[j].tag=0;
        cach.Sets[i].Lines[j].valid=0;
        cach.Sets[i].Lines[j].age=0;
      }
    }

    return cach;        
}

void printSummary(int hits, int misses, int evictions)
{
    printf("hits:%d ,misses:%d ,evictions:%d",hits,misses,evictions);
}

void increment_age(cSet *set, int E){
    printf("\n\ninside increment_age");
    int j=0;
    for (j=0;j<(1<<E);j++){
        set->Lines[j].age++;
    }

}

cLine* find_adr(cache *MyCache,long int mem_tag,int set,int E){
    printf("\n\ninside find_adr");
    increment_age(&MyCache->Sets[set], E);
    int j=0;
    for (j=0;j<(1<<E);j++){
        if(MyCache->Sets[set].Lines[j].tag==mem_tag && MyCache->Sets[set].Lines[j].valid==1){
            hit++;
            if(verbose==1){
                printf("hit ");
            }
            return &MyCache->Sets[set].Lines[j];
        }
    }
    if(verbose==1){
                printf("miss ");
            }
    miss++;
    return NULL;
}

cLine* find_empty(cache *MyCache,int set, int E){
    printf("\n\ninside find_empty");
    int j=0;
    for (j=0;j<(1<<E);j++){
        if(MyCache->Sets[set].Lines[j].valid==0){
            return &MyCache->Sets[set].Lines[j];
        }
    }
    return NULL;
}

cLine* find_evicable(cache *MyCache,int set,int E){
    cLine *ptrLine=&MyCache->Sets[set].Lines[0];
    evic++;
    if(verbose==1){
                printf("eviction ");
            }
    int j=0;
    for (j=0;j<(1<<E);j++){
        if(MyCache->Sets[set].Lines[j].age>ptrLine->age){
            ptrLine=&(MyCache->Sets[set].Lines[j]);
        }
    }
    return ptrLine;
}

void operate(cache *MyCache,long int mem_tag,int set,int E){
    cLine *opLine;
    opLine=find_adr(MyCache,mem_tag,set,E);
    if (opLine==NULL){
        opLine=find_empty(MyCache,set, E);
        if(opLine==NULL){
            opLine=find_evicable(MyCache,set,E);
        }
    }
    opLine->age=0;
    opLine->tag=mem_tag;

}
int main(int argc, char *argv[])
{
    int c;
    int hflg=0, vflg=0, sflg=0, Eflg=0, bflg=0;
    FILE *trc=NULL;
    char *tflg=NULL;
    char *line = NULL;
    size_t len = 0;
    ssize_t read;
    long int tag_size;//=(32-sflg-bflg);
    cache newcache=my_cache(sflg,Eflg,bflg);

    while ((c=getopt(argc, argv, "hvs:E:b:t:"))!=-1)
    {
        switch(c)
        {
            case 'h':
                hflg=1;
                printf("argument h was specified\n");
                break;
            case 'v':
                vflg=1;verbose=1;
                printf("argument v was specified\n");
                break;
            case 's':
                if(optarg == NULL )
                {
                    printf("mandatory argument %c is not specified\n",optopt);
                    break;
                }
                else
                {
                    sflg=atoi(optarg);
                    printf("the value of s is %d\n",sflg);
                    break;
                }
            case 'E':
                if(optarg == NULL )
                {
                    printf("mandatory argument %c is not specified\n",optopt);
                    break;
                }
                else
                {
                    Eflg=atoi(optarg);
                    printf("the value of E is %d\n",Eflg);
                    break;
                }
            case 'b':
                if(optarg == NULL )
                {
                    printf("mandatory argument %c is not specified\n",optopt);
                    break;
                }
                else
                {
                    bflg=atoi(optarg);
                    printf("the value of b is %d\n",bflg);
                    break;
                }
            case 't':
                if(optarg == NULL )
                {
                    printf("mandatory argument %c is not specified\n",optopt);
                    break;
                }
                else
                {
                    tflg=optarg;
                    printf("the value of t is %s\n",tflg);
                    char * str3 = (char *) malloc(1 + strlen("./")+ strlen(optarg) );
                    strcpy(str3, "./");
                    strcat(str3, optarg);
                    trc=fopen(str3,"r");
                    //printf("%s", str3);
                    //free(str3);
                    break;
                }           
        }
    }
    tag_size=(32-sflg-bflg);

if (trc == NULL){
    printf("invalid input file");
    }
else {
    char cmd;
    long int adr;
    int size;
    long int intag,inset;
    while (fscanf(trc, " %c %lx,%d", &cmd, &adr, &size) == 3) {

        switch(cmd) {
            case 'I':
                //printf("%c, %lx\n",cmd,adr);
                //long int intag=adr>>(sflg+bflg);
                //int inset=(adr<<tag_size)>>(tag_size+bflg);
                //printf("%lx, %lx\n",intag,inset);
                break;
            case 'L':
                printf("\n%c, %lx ",cmd,adr);
                intag=adr>>(sflg+bflg);
                inset=(adr<<tag_size)>>(tag_size+bflg);
                operate(&newcache,intag,inset,Eflg);
                //printf("tag:%lx, Set:%lx\n",intag,inset);
                //par = run_sim(sim_cache, par, address);
                break;
            case 'S':
                printf("\n%c, %lx ",cmd,adr);
                intag=adr>>(sflg+bflg);
                inset=(adr<<tag_size)>>(tag_size+bflg);
                operate(&newcache,intag,inset,Eflg);
                //printf("tag:%lx, Set:%lx\n",intag,inset);
                //par = run_sim(sim_cache, par, address);
                break;
            case 'M':
                printf("\n%c, %lx ",cmd,adr);
                intag=adr>>(sflg+bflg);
                inset=(adr<<tag_size)>>(tag_size+bflg);
                operate(&newcache,intag,inset,Eflg);
                operate(&newcache,intag,inset,Eflg);
                //printf("tag:%lx, Set:%lx\n",intag,inset);
                //par = run_sim(sim_cache, par, address);
                    //par = run_sim(sim_cache, par, address);   
                break;
            default:
                break;
        }
    }
}



       fclose(trc);
       printSummary(0, 0, 1);
    //printf("\n%d",1<<4);
    return 0;
}

` 有人可以帮忙......

0 个答案:

没有答案