Reputation: 839
I have created a 2D array in my main function and want to update it in another function . As per my first thought I need to declare the Array dynamically , but the problem is that the dimension should also be dynamic -
following is what I am trying -
int l;
int m;
int n;
void *Hello(void* rank); // Prototype of a thread function
int main(int argc,char* argv[])
{
int l = atoi(argv[2]);
int m = atoi(argv[3]);
int n = atoi(argv[4]);
int A[l][m];
pthread_create(&myThreads[thread_id], NULL,Hello, (void*)thread_id);
}
void *Hello(void* rank)
{ }
I want to update Array A from *Hello function , please advice me for right implementation.
Upvotes: 0
Views: 125
Reputation: 14809
First note that a 2-D array is not serializable, so as other answers recommend, a 1-D array may be a better approach in your case. Here is the naive implementation:
static int l, m, n; // static: local to compilation unit
int **A;
void *Hello(void* rank); // Prototype of a thread function
int main(int argc,char* argv[])
{
l = atoi(argv[2]);
m = atoi(argv[3]);
n = atoi(argv[4]);
A = new int*[l]; // remember to free this!
for (int i = 0; i < l; ++i) {
A[i] = new int[m];
}
pthread_create(&myThreads[thread_id], NULL,Hello, (void*)thread_id);
}
void *Hello(void* rank)
{ }
Here is another, with a serializable 1-D array to hold the data:
static int l, m, n; // static: local to compilation unit
int **A, *A_data;
void *Hello(void* rank); // Prototype of a thread function
int main(int argc,char* argv[])
{
l = atoi(argv[2]);
m = atoi(argv[3]);
n = atoi(argv[4]);
A_data = new int[l*m]; // remember to free this!
for (int i = 0; i < m; ++i) {
A[i] = &A_data[i*l];
}
pthread_create(&myThreads[thread_id], NULL,Hello, (void*)thread_id);
}
void *Hello(void* rank)
{ }
Let us know if you have any questions. :)
Upvotes: 3
Reputation: 7990
First, you should allocate the memory for the 2D array in the heap:
int **A = (int **)malloc(sizeof(int *) * l);
/* Error checking */
int i;
for(i = 0; i < l; ++i)
A[i] = (int*)malloc(sizeof(int) * m);
note that the array isn't in contiguous region of memory.
then you need pass the A
to the Hello()
using the fourth parameter like this:
pthread_create(&myThreads[thread_id], NULL, Hello, (void*)A);
then in the function Hello()
, cast the void*
back to int**
int **array = (int**)rank;
after that, you can update Array A
from Hello()
function.
Here is an example without any error checking:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int l,m;
void *Hello(void* rank); // Prototype of a thread function
int main(int argc,char* argv[])
{
l = atoi(argv[2]);
m = atoi(argv[3]);
int **A = (int **)malloc(sizeof(int *) * l);
int i;
for(i = 0; i <m; ++i)
A[i] = (int*)malloc(sizeof(int) * m);
A[0][0] = 3;
pthread_t tid;
pthread_create(&tid, NULL,Hello, (void*)A);
pthread_join(tid, NULL);
return 0;
}
void *Hello(void* rank)
{
int **array = (int**)rank;
printf("%d\t", array[0][0]);
array[0][0] = 4;
printf("%d\t", array[0][0]);
return;
}
Upvotes: 1
Reputation: 605
If you are not trying to make askew array;
how about creating a one d array and accessing it in two dimensional fashion.
int l;
int m;
int n;
#define idx(i,j) i*m+j
int main(int argc,char* argv[])
{
int l = atoi(argv[2]);
int m = atoi(argv[3]);
int n = atoi(argv[4]);
int *A=(int*)malloc(sizeof(int)*l*m);
memset(A,0,sizeof(int)*l*m);
// Use the array like this
// A[idx(i,j)]=10;
}
Upvotes: 0
Reputation: 26060
Bi-dimensional arrays aren't that common in C++: usually it simpler (and often better) to just have a mono-dimensional array and use it to store all your data:
int *A = new int[ l*m ];
// A[i][j] == A[ i*l + j%l ]
Also, it's usually a good practice to avoid raw memory allocations in C++: rather use std::vector
or something other with the semantics of an array:
std::vector<int> A( l*m );
// A[i][j] == A[ i*l + j%l ]
or, if for some reasons you really need a bi-dimensional array:
std::vector< std::vector<int> > A( l, std::vector<int>(m) );
// A is a vector of l vectors of m ints
Upvotes: 2