Question:
Segmentation fault?!? Help me find this crap?
2010-10-25 01:00:20 UTC
I just wrote a fairly basic program: it fills a table with all of the same temp, and then allows a user to change the value of one of the rows and columns.

However, when I run the program, I get a "segmentation fault." Not being so keen on C, I don't know what that is, per say.

I suspect, however, it has something to do with the initialize_plate function... just a hunch, given that it started occurring after I wrote that. But with my luck, it might be something else....

Could someone here explain what a segmentation fault is in relation to my program?

Here is the code:

http://pastebin.com/SE69SMny


Many, many thank yous for your help, oh kind contributors to Programming and Design
Four answers:
TheOracle
2010-10-25 01:20:31 UTC
you are indexing beyond the limits of your array. it is a memory access violation. you declare the size of the array with the number 7, meaning the indices go from 0 to 6. then your while loop in initialize_plate() goes from 0 to 7.





just out of curiosity, whats with this line:

double plate[MAX_ROWS][MAX_COLS] = {{1.0, 2.0}, {3.0}};



i wasn't aware you could partially initialize arrays in this manner!
Stephan W
2010-10-25 04:18:40 UTC
A 'segmentation fault' happens when a program tries to access data that are not inside its data segment (= outside of its address space). The reason almost always is either an invalid pointer (= a pointer containing an address outside of the programs space) or index of an array (which actually is exactly the same in C/C++).



In line 71 you try to access plate[7], but plate is only defined to have MAX_ROWS = 7 rows, which means plate[0]...plate[6].
2010-10-25 01:54:53 UTC
Change like this:



void initialize_plate(double plate[][MAX_COLS], double temp)

{

int i;

int j;



for (i=0; i < MAX_ROWS; i++)

{



for(j= 0; j < MAX_COLS; j++)

{

plate [j][i] = temp;



}

}

}



I am not commenting everything, because of shortage of time.
alim
2016-11-06 13:49:55 UTC
Your argv argument announcement could be char **argv or char *argv[] somewhat than char *argv. In different words: int significant (int argc, char **argv){ //Parameter examine if (argc < 2){ printf("incorrect form of enter parameters.nUsage %s record", argv[0]); return EXIT_SUCCESS; } return EXIT_SUCCESS; }


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...