Skip to main content
GameDev.net gamedev.net
🔒 Locked

segmentation error in GCC

Started by saranga2005 Oct 6, 2006 at 2:50 AM 3 replies 800+ views
Original Post
saranga2005
saranga2005
#include #include #include int main() { int numRows; int numCols; FILE *fp; int *resptr; int ct=0; int i,x,y,j=0; double k=32.00; printf("\tTHE K VALUE IS %d",k); char temc; unsigned long int hex_addr,temptag,tempindex; int counter=0; double ans=log (2.0); int rows=0,columns=0; double b=0; double c=0; double p=0; double c_size=0; double blk_t=0; double as=1.00000; double blk_size=0; double set_t=0; double set_bit=0; //double i=0; double blk_off=0; char ch; int pol=0; int rep=0; int hit=0; int miss=0; double tagbit=0; printf("\nEnter size of cache:\t"); scanf("%lf",&c_size); printf("\nEnter associativity of cache:\t"); scanf("%lf",&as);//1=direct mapped printf ("\nAssociativity is %lf",as); printf("\nEnter Block size of cache:\t"); scanf("%lf",&blk_size); blk_t=c_size/blk_size; printf("\nTotal no of cache blocks: %lf",blk_t); b=blk_size; p=(log(b)/ans); blk_off=p; c=(blk_t/as);//Total no of sets in cache printf("\nTotal no of sets: %lf",c); printf("\nNo of Block offset bits: %lf",blk_off); /*Set BIT Calculation*/ ans=log (2.0); p=(log(c)/ans); set_bit=p; /*TRACE READ*/ printf("\n"); fp=fopen("go_trace.txt","r"); printf("\nTotal bits for set = %lf",set_bit); //HERE IT GIVES SEGMENTATION ERROR while (!feof(fp)) { fscanf(fp,"%c %x\n",&ch,&hex_addr); //printf("mode - %c\taddr - %x\n",&ch,hex_addr); ct++; //i++; } fclose(fp); rows=ct; columns=as; printf("\nNumber of lines is %d Enter the policies for write and read\n",ct); printf("1=WTNA ,2=WTBA"); scanf("%d",&pol); printf("1=LRU 2=FIFO 3=OPTIMAL 4=Random"); scanf("%d",&rep); //ROWS ARE THE NUMBER OF ADDRESSES IN TRACE; // COLUMNS IS THE ASSOCIATIVITY /*Allocating memory for tag and count array*/ printf("%lf bits to subtract\n",blk_off); numRows=(int)c; numCols=(int)as; printf("ROWS= %d \nCOLUMNS= %d\n",numRows,numCols); tagbit=32.00000-blk_off; tagbit=tagbit-set_bit; int **tag = (int **)malloc( numRows * sizeof( int * ) ); for ( i = 0; i < numRows; ++i ) { tag[ i ] = (int *)malloc( numCols * sizeof( int ) ); } int **count = (int **)malloc( numRows * sizeof( int * ) ); for ( i = 0; i < numRows; ++i ) {//printf("%d",i); count[ i ] = (int *)malloc( numCols * sizeof( int ) ); } printf("Tagbits= %lf \n block offset bits= %lf\n",tagbit,blk_off); printf("helloo\n"); fp=fopen("go_trace.txt","r"); printf("setbits= %lf \n block offset bits= %lf\n",set_bit,blk_off); while (!feof(fp)) { fscanf(fp,"%c %x\n",ch,&hex_addr); printf("%x is address\n",hex_addr); tempindex=hex_addr>>(int)blk_off; temptag=tempindex>>(int)set_bit; //printf("\n%x is tag[%d]",temptag,i); counter++; //if (counter==10) break;// FOR FIRST 10 addresses for (i=0;i[j] = count[j]+1; if (tag[j]==temptag) { break; hit++; printf ("\n%d",hit); } else if (tag[j]==0) { tag[j]=temptag; count[j]=1; } else if (tag[j]!=temptag) { miss++; switch(pol) case 1: for (x=0;x[j]>count[j+1]) x=i;y=j+1; } } } } } fclose(fp); return 0; } }
Aardvajk
Aardvajk
Thank you for posting your code. You may find people are more inclined to answer if you:

a) put your code in [ source ] [ /source ] tags (without the spaces)
b) actually ask a question

The FAQ is not just there to eat webspace.
Emmanuel Deloget
Emmanuel Deloget
You code is bugless when everything is ok. It can crash badly when something goes wrong. For example, when a file pointer is set to NULL by the corresponding call to fopen()... (AKA "Oh! I can't open the file!").

There are multiple possible improvement to your code. It is not secure at all. For example, try to give it a cache block size of 0. Also, remember that return values may also be used to spot some errors during the execution of your program.

For your next post, I'd also like you to actually ask a question, and to use appropriate tags to format your code in a meaningfull way. We are not a helpdesk, we are not paid to help anybody, so the less you can do is to minimize our pain if you have to ask us something. I, for instance, can't read minds. I know, this is very infortunate.

Regards,
Zahlman
Zahlman
Quote:
Original post by Emmanuel Deloget
You code is bugless when everything is ok.


Are you sure? The OP seems to think s/he is writing in C, but only a C++ compiler would accept that, seeing as how you need to declare your variables at the top of their scope. (You're also not supposed to cast the result of malloc(), but I doubt any C compilers out there will warn about that without significant reconfiguration)
Emmanuel Deloget
Emmanuel Deloget
Quote:
Original post by Zahlman
Quote:
Original post by Emmanuel Deloget
You code is bugless when everything is ok.


Are you sure? The OP seems to think s/he is writing in C, but only a C++ compiler would accept that, seeing as how you need to declare your variables at the top of their scope. (You're also not supposed to cast the result of malloc(), but I doubt any C compilers out there will warn about that without significant reconfiguration)


He is obviously using a C++ compiler but he also obviously wanted to write C-like code (I cannot see any other explaination for this horrible malloc()). Of course, the code would not pass strict C compilation (unless C99 allow such kind of definitions, but I'm doubtfull) [smile]

And to be fair, there is at least one "bug": a bunch of memory leak.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.