2 Size Array Pointer

I follow an example for implementing threads in C: http://ramcdougal.com/threads.html . This example uses a 1-dimensional array. I need a dynamic 2 dimensional array.

What would it look like if in main() it was an int **array instead of an int array[ARRAYSIZE] ?

My problem is how to pass a pointer to a two-dimensional array in a structure. The idea is that I have a large array, and each thread should only fill a specific area of ​​this array.

Thanks a lot!

Here is the code from the example:

 struct ThreadData { int start, stop; int* array; }; void* squarer(struct ThreadData* td) { struct ThreadData* data=(struct ThreadData*) td; int start=data->start; int stop=data->stop; int* array=data->array; int i; for (i=start; i<stop; i++) { array[i]=i*i; } return NULL; } int main(void) { int array[ARRAYSIZE]; pthread_t thread[NUMTHREADS]; struct ThreadData data[NUMTHREADS]; int i; int tasksPerThread=(ARRAYSIZE+NUMTHREADS-1)/NUMTHREADS; for (i=0; i<NUMTHREADS; i++) { data[i].start=i*tasksPerThread; data[i].stop=(i+1)*tasksPerThread; data[i].array=array; } /* the last thread must not go past the end of the array */ data[NUMTHREADS-1].stop=ARRAYSIZE; /* Launch Threads */ for (i=0; i<NUMTHREADS; i++) { pthread_create(&thread[i], NULL, squarer, &data[i]); } /* Wait for Threads to Finish */ for (i=0; i<NUMTHREADS; i++) { pthread_join(thread[i], NULL); } /* Display Result */ for (i=0; i<ARRAYSIZE; i++) { printf("%d ", array[i]); } printf("\n"); return 0; } 
+4
source share
2 answers

Think of it this way:

When working with a one-dimensional array, start and stop are one-dimensional vectors representing coordinates in 1-D space (And a 1-D vector can be represented as an integer, which is used in the source code.)

So, in a two-dimensional array, start and stop must be two-dimensional vectors:

 struct ThreadData { int start[2], stop[2]; int **array; } 

Then you split the rectangular blocks among the threads. And each thread gets the position of the upper left corner of its block at start , and the position of the lower right corner of its block at stop .

enter image description here

Remember that blocks that are rectangular can be high stripes (1 column per stream) or long (one row per stream) or a square or somewhere in between. You have to decide which form works faster by benchmarking.

In a sense, tasksPerThread also has two dimensions. With the actual number of tasks becoming tasksPerThread[0] * tasksPerThread[1] .

+1
source

to dynamically allocate a 2-dimensional array, use something like this:

 int** array = malloc(sizeof(int*)*ARRAYSIZE); 

Here you allocate an array of pointers to int, now you must allocate memory for each pointer:

 for(int i = 0;i<ARRAYSIZE;i++) array[i] = malloc(sizeof(int)*INNER_ARRAYSIZE); 

And now fill each record with your actual data:

 for(int i = 0;i<ARRAYSIZE;i++) for(int j = 0;j<INNER_ARRAYSIZE;j++) array[i][j]=(i+j);//just for example 

And update the ThreadData structure to use two-dimensional arrays:

 struct ThreadData { int start, stop; int** twoDimArray;//note one more * here 

};

And just pass the pointer here:

 struct ThreadData data; data.twoDimArray = array; data.twoDimArray[0][0] = data.twoDimArray[0][0]*data.twoDimArray[0][0]; //access element at 0,0 and square it 
+4
source

Source: https://habr.com/ru/post/1447436/


All Articles