Thread: Segmentation fault (core dumped)

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    8

    Segmentation fault (core dumped)

    I get the error "Segmentation fault (core dumped)" every time I try to run the code. I have no idea what's wrong, but I'm new to 2D arrays and I think that might have something to do with that.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(){
    int enterstep;
    puts("Enter the total radius");
    scanf("%d", &enterstep);
    int aaa,bbb,a,b,c,d,e,f,g,h,i,j,dt,tmax,max=enterstep,ice=0,water=0,dry=0, Tmelt=273, count1, count2, midtemp;
    float T[max+10][max*2+10], PastT[max+10][max*2+10], InitialT[max+10][max*2+10], deltat=3.15569*pow(10,7),K=pow(10,-6), dx=1000, diff=136.4, Tmeltul=Tmelt+diff, icef, waterf, dryf, radius, aa=0.26183*150.3226, kappa=K*deltat/pow(dx,2), track[max+1][max+1], number=max;
    puts("Enter time:");
    scanf("%d", &tmax);
    for(aaa=0; aaa<=max; aaa++){ 
    	for (bbb=0; bbb<=max*2; bbb++){
    		T[aaa][bbb]=150;
    		PastT[aaa][bbb]=150;
    		InitialT[aaa][bbb]=150;
    	}
    }
    printf("dt\tlayer\tT(K)\n");
    track[0][0]=0;
    for(i=1; i<=max; i++){
    	for(b=1; b<=max; b++){
    		radius=sqrt((pow(max-i,2)+pow(b,2)));
    		if(radius<=(max)){
    			count1++;
    		}
    	}
    }
    
    midtemp=count1+1;
    T[0][0]=500;
    PastT[0][0]=T[0][0];
    for (dt=0; dt<=tmax; dt++){
    	ice=0;
    	water=0;
    	dry=0;
    	if(dt==0){
    		for(a=0; a<=max; a++){
    			for(d=1; d<=max; d++){
    				if(dt==tmax){
    					printf("%d\t%d\t%.2f\n",dt,a,InitialT[a][d]);
    				}
    			}			
    		}
    	ice=100;
    	}
    	if(dt>0){
    		for(j=0; j<=2*max; j++){
    			for(e=1; e<=max; e++){
    				radius=sqrt(pow((max-j),2)+pow(e,2));
    				if(radius<=max){
    					count2++;
    					track[j][e]=j;
    					if(track[j][e]>track[j][e-1]){
    						if(count2==midtemp){
    							T[j][e]=T[0][0]+(kappa*((e+1/2)*PastT[j][e+1]-2*e*PastT[j][e])/e)+kappa*(PastT[(j+1)][e]-2*PastT[j][e]+PastT[(j-1)][e]);
    						}
    						if(count2!=midtemp){
    							T[j][e]=PastT[j][e]+(kappa*((e+1/2)*PastT[j][e+1]-2*e*PastT[j][e])/e)+kappa*(PastT[(j+1)][e]-2*PastT[j][e]+PastT[(j-1)][e]);
    						}
    					}
    					if(track[j][e]==track[j][e-1]){
    						if(sqrt(pow((max-j),2)+pow((e+1),2))>max){
    						T[j][e]=150;
    						}
    						if(sqrt(pow((max-j),2)+pow((e+1),2))<=max){
    						T[j][e]=PastT[j][e]+(kappa*((e+1/2)*PastT[j][e+1]-2*e*PastT[j][e]+(e-1/2)*PastT[j][e-1])/e)+kappa*(PastT[(j+1)][e]-2*PastT[j][e]+PastT[(j-1)][e]);
    						}
    					PastT[j][e]=T[j][e];
    					}
    				}			
    
    			}
    		}
    	}	
    }
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Some notes: (1) There is such a thing as scientific notation:
    Code:
    deltat=3.15569e7
    (2) Your for-loop at line 37 only runs if dt==0, but the print statement inside it only prints if dt==tmax, ie, never.
    (3) I have no idea why you decided to make the array sizes what they are.
    (4) I was unable to trigger a segfault, presumably because I was not picking appropriate input values. What input values are you using?

    EDIT: Nearly forgot:
    Code:
    gcc -Wall -g -o ice ice.c -lm
    ice.c: In function ‘main’:
    ice.c:9:261: warning: unused variable ‘number’ [-Wunused-variable]
    ice.c:9:193: warning: unused variable ‘aa’ [-Wunused-variable]
    ice.c:9:179: warning: unused variable ‘dryf’ [-Wunused-variable]
    ice.c:9:171: warning: unused variable ‘waterf’ [-Wunused-variable]
    ice.c:9:165: warning: unused variable ‘icef’ [-Wunused-variable]
    ice.c:9:145: warning: unused variable ‘Tmeltul’ [-Wunused-variable]
    ice.c:8:73: warning: variable ‘dry’ set but not used [-Wunused-but-set-variable]
    ice.c:8:65: warning: variable ‘water’ set but not used [-Wunused-but-set-variable]
    ice.c:8:59: warning: variable ‘ice’ set but not used [-Wunused-but-set-variable]
    ice.c:8:31: warning: unused variable ‘h’ [-Wunused-variable]
    ice.c:8:29: warning: unused variable ‘g’ [-Wunused-variable]
    ice.c:8:27: warning: unused variable ‘f’ [-Wunused-variable]
    ice.c:8:21: warning: unused variable ‘c’ [-Wunused-variable]
    ice.c:77:1: warning: control reaches end of non-void function [-Wreturn-type]
    That's a lot of variables you're not using. Also if you do have a core dump, you can use a debugger to examine it and find out what was happening at the time of the crash; or you can run the program in the debugger in the first place and get info about all the variables at the time of crash.
    Last edited by tabstop; 01-28-2014 at 05:43 PM.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    +1 to everything tabstop said.

    A few more notes:
    1. Compile at maximum warning level
    Code:
    $ make foo
    gcc -Wall -ggdb3 -pedantic -std=c99 -O0 -o foo foo.c -lm -lpthread -lrt
    foo.c: In function ‘main’:
    foo.c:9:261: warning: unused variable ‘number’ [-Wunused-variable]
    foo.c:9:193: warning: unused variable ‘aa’ [-Wunused-variable]
    foo.c:9:179: warning: unused variable ‘dryf’ [-Wunused-variable]
    foo.c:9:171: warning: unused variable ‘waterf’ [-Wunused-variable]
    foo.c:9:165: warning: unused variable ‘icef’ [-Wunused-variable]
    foo.c:9:145: warning: unused variable ‘Tmeltul’ [-Wunused-variable]
    foo.c:8:73: warning: variable ‘dry’ set but not used [-Wunused-but-set-variable]
    foo.c:8:65: warning: variable ‘water’ set but not used [-Wunused-but-set-variable]
    foo.c:8:59: warning: variable ‘ice’ set but not used [-Wunused-but-set-variable]
    foo.c:8:31: warning: unused variable ‘h’ [-Wunused-variable]
    foo.c:8:29: warning: unused variable ‘g’ [-Wunused-variable]
    foo.c:8:27: warning: unused variable ‘f’ [-Wunused-variable]
    foo.c:8:21: warning: unused variable ‘c’ [-Wunused-variable]
    2. Clean up unused variables, they clutter your already-messy code.
    3. Try putting some whitespace around operators, it makes it easier to read, thus helping you spot small calculation errors that might result in, e.g., array out of bounds errors
    4. Add some comments explaining what this cryptic stuff does.
    5. Try some better variable names. i and j are fine for loop counters/array indices. But what do count1 and count2 count? How about count_inside_radius for count1.
    6. You can re-use a loop index, so long as you don't actually need it's value for something else. For example, aaa and bbb are only used in the first pair of nested for loops (lines 12-18). Instead, use i and j, and remove the declaration of aaa and bbb. In fact, I think you only need 3 loop variables. dt, i and j should suffice, you never nest more than 3 levels deep.
    7. Use temporary variables to store intermediate values, and do calculations in parts, so you avoid 120-char long expressions with no white space. Again, this improves readability and lowers the likelihood of errors. Besides, I think some of the intermediate values can be reused, saving coding effort, reducing the amount of places to look for and fix errors, and saves some computation time.

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Code:
    ==1696== Conditional jump or move depends on uninitialised value(s)
    ==1696==    at 0x401142: main (blah.c:54)
    ==1696== 
    ==1696== Conditional jump or move depends on uninitialised value(s)
    ==1696==    at 0x401151: main (blah.c:55)
    ==1696== 
    ==1696== Conditional jump or move depends on uninitialised value(s)
    ==1696==    at 0x4012AB: main (blah.c:58)
    ==1696== 
    ==1696== Conditional jump or move depends on uninitialised value(s)
    ==1696==    at 0x401469: main (blah.c:62)
    ==1696== 
    ==1696== Conditional jump or move depends on uninitialised value(s)
    ==1696==    at 0x401472: main (blah.c:62)
    ==1696==
    Do not take offence at what I called the program (blah.c)... I do the same for my own stuff as well

    Edit: In spite of the above I cannot cause a segfault either. Your variable names are confusing me otherwise I'd debug it more but if you follow the fine advice others have giving I'm sure you'll work it out.
    Last edited by Hodor; 01-28-2014 at 06:01 PM.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    8
    thanks

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Your program is difficult to read. Some spacing would help.

    You seem to believe that you need a different variable for every loop. You only need different variables if the loops are nested. The loop-nesting in your program is 3 levels, so you need 3 index variables (traditionally called i, j, k). You are using 9: aaa bbb i b dt a d j e

    It's not useful to use pow just to square a number. Instead of pow(e,2) just use e * e.

    The expression (e + 1 / 2) is simply (e + 0), i.e., e. This is due to integer division, which essentially returns a truncated result. I'm not sure what you want here, but since e is an int, maybe you meant ((e + 1) / 2).

    If you've already tested for (e.g.) a == b, there's no need to then test if a != b, since that must be the case if the first condition is false. So instead of this
    Code:
    if (a == b) 
        /* ... */ ;
    if (a != b)
        /* ... */ ;
    do this
    Code:
    if (a == b) 
        /* ... */ ;
    else // a != b
        /* ... */ ;
    Also, I notice that your gigantic calculation is essentially repeated 3 times. Try to calculate it just once and assign it to the different variables in the conditional statements.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault (core dumped) in #c?
    By hs101 in forum C Programming
    Replies: 9
    Last Post: 12-06-2012, 02:38 PM
  2. Segmentation fault (core dumped)
    By KoRn KloWn in forum C Programming
    Replies: 3
    Last Post: 09-22-2012, 02:34 AM
  3. Segmentation Fault (core dumped)
    By sameer2904 in forum C Programming
    Replies: 3
    Last Post: 01-09-2012, 07:37 AM
  4. Segmentation fault (core dumped)????
    By yosipoa in forum C++ Programming
    Replies: 2
    Last Post: 07-20-2011, 01:18 PM
  5. Segmentation fault (core dumped)
    By JYSN in forum C Programming
    Replies: 1
    Last Post: 02-21-2002, 03:24 AM