-1

I have a signal 11; i.e. segfault. I know where it occurs (see code), but do not know how to correct it.

int *totalThreadNumProduced; //array
int *totalThreadNumConsumed; //array
int *tempStats; //array
for(int i=0; i < global_args.numProducers; i++)
{  
    tempStats = (int*) pthread_join( tidP[i], NULL );
    simulationStats.totalThreadNumProduced[i] = tempStats[0]; //where the segmentation fault 11 is
    simulationStats.numTimesBufferFull += tempStats[1];

}
Bilal Siddiqui
  • 349
  • 3
  • 17
  • Have you allocated the memory for those three array? – Chen OT Nov 22 '18 at 01:59
  • 1
    @ChenOT pointers. – Swordfish Nov 22 '18 at 02:00
  • `pthread_join( tidP[i], NULL );` will return `0` on success, or an error code on failure. None of those will most likely be a valid pointer value. Also as this is obviously not your real code, please provide a [mcve]. – Swordfish Nov 22 '18 at 02:01
  • ibm has a sample code of (what i think) you are trying to do: https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.3.0/com.ibm.zos.v2r3.bpxbd00/ptjoin.htm – split Nov 22 '18 at 02:24

1 Answers1

0
tempStats = (int*) pthread_join( tidP[i], NULL );

tempStats is an array of integers, judging from the following line:

simulationStats.totalThreadNumProduced[i] = tempStats[0];

the segfault is either due to memory not being allocated for totalThreadNumProduced, or tempStats.

Furthermore, your usage for pthread_join is likely incorrect; it simply returns whether the call was successful or not. If you wanting the return value of a completed thread function, consider using the 2nd status parameter of pthread_join as show here.

Bilal Siddiqui
  • 349
  • 3
  • 17
  • A pointer is a pointer. An array is an array. A pointer is no array. An array is no pointer. – Swordfish Nov 22 '18 at 12:02
  • The first element of a 1D array can be accessed using array_name or array_name[0]; i.e. an array is a pointer to a sequence of identical data-types. Usually, people use the square bracket notation to access 'elements' but that can also be done use pointer arithmetic. *(array_name+5) is that same as array_name[5]. – Bilal Siddiqui Nov 22 '18 at 16:11
  • That an array when used as a value and not as operand of the `sizeof` or address-of operator is \*converted\* to a pointer to its first element is not the same as it *being* a pointer. – *an array is a pointer to a sequence of identical data-types.* – No, an array is not a pointer. And `int *tempStats;` is certainly no array. – Swordfish Nov 22 '18 at 16:19
  • https://stackoverflow.com/questions/3959705/arrays-are-pointers Okay, thanks for the enlightenment; I think I understand what you are saying now. With that said, what do we call a pointer that has been malloc'd *m* items? That is what seems to be the problem - OP is attempting to use a pointer as an array without allocating memory to the pointer. – Bilal Siddiqui Nov 22 '18 at 16:54