Question:
Segmentation fault on C?
1970-01-01 00:00:00 UTC
Segmentation fault on C?
Four answers:
John H
2012-12-10 15:21:07 UTC
There is another error:



int *ID = malloc(1);

*ID = list+1;



You need to malloc(sizeof(int)). You're only malloc'ing a single byte of memory. You could be getting lucky, and malloc is always rounding the size up so it's not causing problems even though it is wrong, but it's an error, and should be fixed.
rickert
2016-11-23 21:20:16 UTC
A "Segmentation Fault" continually skill one ingredient: you're getting access to a memory region exterior the boundary of your technique area. usually, meaning your array index factors exterior the array. examine your 'r' and 'c' and 'i' and 'j' values to verify that they are no longer unfavourable or too vast on your array length. by utilising the way, did you allocate any area for "vec"? i did no longer spot any vec = new vector (yadda yadda); everywhere.
Laurence I
2012-12-10 11:41:27 UTC
your code or your data is too big for a SEGMENT

when you compile a C program, you have different MEMORY MODELS

small medium large and maybe Huge.



these model sizes decide how big the segments can be.



you can control this to some extent using Command Line switches

or #define statements.



the Linker switches are important too.



the link below is very old but it kind of covers some of the problems to do with segments
Jeroonk
2012-12-10 12:35:26 UTC
When confronted with a segfault, it almost always is a dereference of some bogus value. When interpreted as an address, it is very unlikely that such a value points into a valid segment.



There are two causes for these segfaults in your program:



- You initialize each thread with an 'ID = list + 1', which means the numbers 1 trough 5. You address the 'flag' array directly using this id. Array indices start at zero, not at one! This means you will never use flag[0], and dereference a value beyond the array bounds whenever monitor 5 is trying to eat.



- Check your #define for the RIGHT macro, shouldn't the condition be (ID == 4) instead of (ID == ID-1)? This might also trigger a dereference of some bogus value beyond the 'flag' array bounds.



Additionally:



- Joining the thread xyz[list] will cause problems. Because 'list' has just been iterated in a loop it will actually contain the value 5 (the first value that fails the loop conditional). You are now calling pthread_join(xyz[5], NULL), which is beyond the bounds of xyz. This will cause very odd behavior, most likely that main simply doesn't wait for any of the threads to finish and exits immediately. Instead, use:



pthread_join(xyz[0], NULL);



- You must join all threads, not just the first one. In this case, since neither of the threads will ever stop, it is not a problem. In general, unless you specifically want the program to exit with running threads, use:



for(list = 0; list<5; list++) pthread_join(xyz[list], NULL);



- See the answer given by John H.





The code runs fine after these fixes:

http://pastebin.com/hgk7C280 (EDIT: fixed the malloc(1) issue, see the answer given by John H)



Only problem now is that the appetite of these monitors is enormous, and they just keep on thinking and eating and the program never stops. You should probably make them count down the number of times they eat.


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