• C

Problem in passing arguments between files in c

Hi all,
I'm having the problem in passing the num_thread from my main.c to mcompute.c, in line 39 it prints out 0(instead of 2 from the command line argument). hence, i got an Arithmetic Exception (core dumped) after it.
Can anyone help me in this, my code is a little messy since this is my first time splitting up the code into several files, so i'm kind of weak in passing the arguments.

These are the global var i have in my matmul.h:
int num_thread;      // number of threads
int      itt_max;      // number of itterations to preform
int      seed;            // seed for srand48() / drand48()
int      n;                  // problenm size
double      *a;            // pointer to transfromation array, space to be malloced
double      *b;            // pointer to transfromation vector, space to be malloced
double *err;

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <pthread.h>
#include "matmul.h"

void srand48(long int seedval);
double drand48(void);

void* compute(void* thrd)
{	
	int from, to;
	int s = (int)thrd;
	int i, j;
	double  *t;     // pointer to solution vector, space to be malloced 
	double  *t1;    // pointer to next itteraton of solution vector, 
	                // space to be malloced
	double	*ttemp;	// used to swap t1 and t at each itteration
	int	itt;	// current itteration 
	double	sum;	// computes the inner products for A * t
	double 	error;  // max | t1[i] - t[i] |
	double 	errori; // | t1[i] - t[i] |
	
	if( (t=(double *)malloc(sizeof(double)*n)) == NULL) 
	{
  		fprintf(stderr," ERROR : malloc for t failed\n");
	}
	
	if( (t1=(double *)malloc(sizeof(double)*n)) == NULL) 
	{
  		fprintf(stderr," ERROR : malloc for t1 failed\n");
	}
	
	if( (err = (double*) malloc (sizeof (double) * itt_max)) == NULL) 
	{
  		fprintf(stderr," ERROR : malloc for err failed\n");
	}	
	
	printf("Number of threads is: %d \n\n", num_thread);
	for(i=0; i< n; i++) 
	{
  		t[i] = b[i];
	}
	
	if( s < num_thread)
	{
		from = (s * (itt_max + 1))/num_thread;
		to = ((s + 1) * (itt_max + 1))/num_thread - 1;
	}
	else if ( s == num_thread)
	{
		from = (s * (itt_max + 1))/num_thread;
		to = ((s + 1) * (itt_max + 1))/num_thread;
	}

	for(itt=from; itt<=to; itt++) 
	{
  		error=0.0;
  		for(i=0; i< n; i++) 
		{
    		sum = 0.0;
			double *a_itt = a + n*i;
			for(j=0; j< n; j++) 
			{
			   sum += *(a_itt++) * t[j];
			}
    		t1[i] = sum + b[i];
    		errori = fabs(t1[i]-t[i]);
    		if(errori > error) 
			{
      			error=errori;
    		}
  		}
		err[itt] = error;
  		ttemp = t1;
  		t1 = t;
  		t = ttemp;
  		//printf("%5d %14.6e\n", itt, error); 
	}

	pthread_exit(0);
}

int mcompute(int n, int seed, int itt_max, int num_thread, double *a, double *b)
{
	srand48((long int)seed);
	float resultb[n];	
	int	i, j;   // indices into arrays
	pthread_t* thread;	// pointer to a group of threads

	float **resulta = (float**)malloc(sizeof(float*)*n);
	for(i=0; i< n; i++)
		*(resulta+i) = (float*)malloc(sizeof(float)*n);

	for(i=0; i< n; i++) 
	{
  		for(j=0; j< n; j++) 
		{
    			*(a+n*i+j) = 1.999 * (drand48() - 0.5) / n;
    			resulta[i][j] = *(a+n*i+j);
  		}
	}

	moutput_a(resulta, n);

	// Generate vector b 
	for(i=0; i< n; i++) 
	{
  		b[i] = 10.0 * drand48();
		resultb[i] = b[i];
	}

	moutput_b(resultb, n);
	
	thread = (pthread_t*)malloc(num_thread*sizeof(pthread_t));
	
	for (i = 0; i < num_thread; i++)
	{
		if(pthread_create(&thread[i], NULL, compute, (void*)i) != 0)
		{
			perror("Can't create thread!");
			free(thread);
			exit(-1);
		}
	}
	
	for (i = 0; i < num_thread; i++)
		pthread_join (thread[i], NULL);

	moutput_itt(err, itt_max);
	
	free(thread);
	return 0;
}

Open in new window

crazy4sAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

fridomCEO/ProgrammerCommented:
num_thread is never set. And you do not show the code where it is. So whatever the outpuut is, it is ok.
0
ZoppoCommented:
Hi crazy4s,

IMO you somehow mix local and global use of the variable num_threads, but it's hard to say how you expect it to work since you didn't post the code of the mentioned main.

What I can tell you is that compute uses a global declared num_threads, mcompute instead uses a local declare num_threads passed as argument.

Without seing the call of this function I can't tell you more, sorry.

ZOPPO
0
crazy4sAuthor Commented:
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

ZoppoCommented:
ok, first I would suggest to rename the global variable to make it easier to distinguish between the global and the local used variables.

Next I'm a bit suprised why this doesn't produce any linker errors, but maybe your compiler/linker acts different than those I use. Generally a problem is that you instantiate the global num_threads within the header, so in each c-file where you include mathmul.h a new instance is created. To get only one global instance you'll have to declare it as extern in the header and instantiate it in exactly one c-file.

So you can do it somehow like this:
// mathmul.h
...
extern int global_thread_num;
...

Open in new window

// mathmul-compute.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <pthread.h>
#include "matmul.h"

int global_thread_num = 0;
...
void* compute(void* thrd)
{
 // replace all occurances of thread_num with global_thread_num in this function
}

int mcompute(int n, int seed, int itt_max, int num_thread, double *a, double *b)
{
 global_num_thread = num_thread;
 // you can replace all further occurances of thread_num with global_thread_num in this function to avoid confusion, but it's not necessary
 ...
} 

Open in new window


BTW, you should change the instantiation in the header for the other global variables in mathmul.h the same way (declare as extern in the header, instantiate once in a c-file).

Hope that helps,

ZOPPO
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
crazy4sAuthor Commented:
and how bout those with pointer do i do the same too:
double      *a;

do i need to change it to extern double *global_a ???

and since i'm accessing the global variable, do i actually still need to pass those variables?
0
ZoppoCommented:
For every global thing you need/want to use in multiple C-files you have to declare it as extern in all of these files (usually by declaring it in a header which is include in each of these files), no matter if variables, pointers (which are variables too) or functions. You can even mix where you instantiate/implement them and you can even do it if wanted in single C-files only

For example:

// header.h
// (declaration in header allows use in every c-file which includes the header)
extern int global_int;
extern double* global_ptr;
extern int global_function( int value );

// cfile1.c
#include "header.h"

int global_int = 0; // instantiation
double local_double = 0.0; // instantiation of a local variable

int global_function( int value ); // implementation
{
 // global pointer accessed here allthough it's not instantiated in this file
 global_ptr = malloc( sizeof( double ) );
}

// cfile2.c
#include "header.h"

extern double local_double; // this tells the compilet/linker the variable exists but is instantiated elsewhere, so you can use it here
double* global_ptr = &local_double; // instantiation;

Open in new window


Hope that's helpful.

And, for the second question: If you simply use global_num_threads with the scanf in your main function you can use it everywhere else you used num_thread before without the need to pass it anywhere else.

ZOPPO
0
crazy4sAuthor Commented:
between is it possible to pass those variables without using global var?
this means locally pass from main to setup, compute, and output.
which is better?
0
ZoppoCommented:
It is usually possible to simply pass the variable as parameters to any function where you need, but there are some cases where it's not simply possible because a function has to be declared with a given signature - that's the case in your code because you cannot change the paramter list of compute to pass two variables since it's passed to pthread_create and therefore cannot be declared to get two parameters passed.

But you can implement a little struct to do it, i.e.:

struct compute_params
{
 int num_threads;
 int s;
}

void* compute(void* thrd)
{	
	compute_params* params = (compute_param*)thrd;
	int s = params.s;
	int num_threads = params.num_threads;
        ...
        free( params );
}

int mcompute(int n, int seed, int itt_max, int num_thread, double *a, double *b)
{
        compute_params* params;
        ...
	for (i = 0; i < num_thread; i++)
	{
		params = malloc( sizeof( compute_params ) );
		params.s = i;
		params.num_threads = num_threads;
		if(pthread_create(&thread[i], NULL, compute, (void*)params) != 0)
		{
			...
		}
	}
	...
}

Open in new window

This way you can pass an arbitrary number of any kind of data to the thread function, so you don't need to have num_threads as global variable anymore.

ZOPPO
0
crazy4sAuthor Commented:
not sure why i got the error of compute_params  and params undeclared:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <pthread.h>
#include "matmul.h"

void srand48(long int seedval);
double drand48(void);

double *err;

struct compute_params
{
	int s;
	int n;
	int itt_max;
	int num_thread;
	double *a;
	double *b;
}

void* compute(void* thrd)
{	
	int from, to;
	compute_params* params = (compute_params)* thrd;
	int s = params.s;
	int n = params.n;
	int itt_max = params.itt_max;
	int num_thread = params.num_thread;
	double *a = params.a;
	double *b = params.b;

	int i, j;
	double  *t;     // pointer to solution vector, space to be malloced 
	double  *t1;    // pointer to next itteraton of solution vector, 
	                // space to be malloced
	double	*ttemp;	// used to swap t1 and t at each itteration
	int	itt;	// current itteration 
	double	sum;	// computes the inner products for A * t
	double 	error;  // max | t1[i] - t[i] |
	double 	errori; // | t1[i] - t[i] |
	
	if( (t=(double *)malloc(sizeof(double)*n)) == NULL) 
	{
  		fprintf(stderr," ERROR : malloc for t failed\n");
	}
	
	if( (t1=(double *)malloc(sizeof(double)*n)) == NULL) 
	{
  		fprintf(stderr," ERROR : malloc for t1 failed\n");
	}
	
	if( (err = (double*) malloc (sizeof (double) * itt_max)) == NULL) 
	{
  		fprintf(stderr," ERROR : malloc for err failed\n");
	}	

	// Initialize t
	
	for(i=0; i< n; i++) 
	{
  		t[i] = b[i];
	}
	
	if( s < num_thread)
	{
		from = (s * (itt_max + 1))/num_thread;
		to = ((s + 1) * (itt_max + 1))/num_thread - 1;
	}
	else if ( s == num_thread)
	{
		from = (s * (itt_max + 1))/num_thread;
		to = ((s + 1) * (itt_max + 1))/num_thread;
	}

	for(itt=from; itt<=to; itt++) 
	{
  		error=0.0;
  		for(i=0; i< n; i++) 
		{
    			sum = 0.0;
			double *a_itt = a + n*i;
			for(j=0; j< n; j++) 
			{
			   sum += *(a_itt++) * t[j];
			}

    		t1[i] = sum + b[i];
    		errori = fabs(t1[i]-t[i]);
    		if(errori > error) 
			{
      			error=errori;
    		}
  		}
		err[itt] = error;
  		ttemp = t1;
  		t1 = t;
  		t = ttemp;
	}

	pthread_exit(0);
	free(params);
}

int mcompute(int n, int seed, int itt_max, int num_thread, double *a, double *b)
{
	srand48((long int)seed);
	float resultb[n];	
	int	i, j;   // indices into arrays
	pthread_t* thread;	// pointer to a group of threads
	compute_params* params;

	float **resulta = (float**)malloc(sizeof(float*)*n);
	for(i=0; i< n; i++)
		*(resulta+i) = (float*)malloc(sizeof(float)*n);

	for(i=0; i< n; i++) 
	{
  		for(j=0; j< n; j++) 
		{
    			*(a+n*i+j) = 1.999 * (drand48() - 0.5) / n;
    			resulta[i][j] = *(a+n*i+j);
  		}
	}

	moutput_a(resulta, n);

	// Generate vector b 
	for(i=0; i< n; i++) 
	{
  		b[i] = 10.0 * drand48();
		resultb[i] = b[i];
	}

	moutput_b(resultb, n);
	
	thread = (pthread_t*)malloc(num_thread*sizeof(pthread_t));
	
	for (i = 0; i < num_thread; i++)
	{
		params = malloc(sizeof(compute_params));
		params.s = i;
		params.n = n;
		params.itt_max = itt_max;
		params.num_thread = num_thread;
		params.a = a;
		params.b = b;
		if(pthread_create(&thread[i], NULL, compute, (void*)params) != 0)
		{
			perror("Can't create thread!");
			free(thread);
			exit(-1);
		}
	}
	
	for (i = 0; i < num_thread; i++)
		pthread_join (thread[i], NULL);

	moutput_itt(err, itt_max);
	
	free(thread);
	return 0;
}

Open in new window

0
ZoppoCommented:
If this is your exact code then the problem is a missing ;, it has to be
struct compute_params
{
	int s;
	int n;
	int itt_max;
	int num_thread;
	double *a;
	double *b;
}; // <-- here

Open in new window

ZOPPO
0
crazy4sAuthor Commented:
okay even i added the semicolon it's still giving me the same error:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <pthread.h>
#include "matmul.h"

void srand48(long int seedval);
double drand48(void);

double *err;

struct compute_params
{
	int s;
	int n;
	int itt_max;
	int num_thread;
	double *a;
	double *b;
};

void* compute(void* thrd)
{	
	int from, to;
	compute_params* params = (compute_params)* thrd; // struct to be passed
	int s = params.s;
	int n = params.n;
	int itt_max = params.itt_max;
	int num_thread = params.num_thread;
	double *a = params.a;
	double *b = params.b;

	int i, j;
	double  *t;     // pointer to solution vector, space to be malloced 
	double  *t1;    // pointer to next itteraton of solution vector, 
	                // space to be malloced
	double	*ttemp;	// used to swap t1 and t at each itteration
	int	itt;	// current itteration 
	double	sum;	// computes the inner products for A * t
	double 	error;  // max | t1[i] - t[i] |
	double 	errori; // | t1[i] - t[i] |
	
	if( (t=(double *)malloc(sizeof(double)*n)) == NULL) 
	{
  		fprintf(stderr," ERROR : malloc for t failed\n");
	}
	
	if( (t1=(double *)malloc(sizeof(double)*n)) == NULL) 
	{
  		fprintf(stderr," ERROR : malloc for t1 failed\n");
	}
	
	if( (err = (double*) malloc (sizeof (double) * itt_max)) == NULL) 
	{
  		fprintf(stderr," ERROR : malloc for err failed\n");
	}	

	// Initialize t
	
	for(i=0; i< n; i++) 
	{
  		t[i] = b[i];
	}
	
	if( s < num_thread)
	{
		from = (s * (itt_max + 1))/num_thread;
		to = ((s + 1) * (itt_max + 1))/num_thread - 1;
	}
	else if ( s == num_thread)
	{
		from = (s * (itt_max + 1))/num_thread;
		to = ((s + 1) * (itt_max + 1))/num_thread;
	}

	for(itt=from; itt<=to; itt++) 
	{
  		error=0.0;
  		for(i=0; i< n; i++) 
		{
    			sum = 0.0;
			double *a_itt = a + n*i;
			for(j=0; j< n; j++) 
			{
			   sum += *(a_itt++) * t[j];
			}

    		t1[i] = sum + b[i];
    		errori = fabs(t1[i]-t[i]);
    		if(errori > error) 
			{
      			error=errori;
    		}
  		}
		err[itt] = error;
  		ttemp = t1;
  		t1 = t;
  		t = ttemp;
	}

	pthread_exit(0);
	free(params);
}

int mcompute(int n, int seed, int itt_max, int num_thread, double *a, double *b)
{
	srand48((long int)seed);
	float resultb[n];	
	int	i, j;   // indices into arrays
	pthread_t* thread;	// pointer to a group of threads
	compute_params* params;

	float **resulta = (float**)malloc(sizeof(float*)*n);
	for(i=0; i< n; i++)
		*(resulta+i) = (float*)malloc(sizeof(float)*n);

	for(i=0; i< n; i++) 
	{
  		for(j=0; j< n; j++) 
		{
    			*(a+n*i+j) = 1.999 * (drand48() - 0.5) / n;
    			resulta[i][j] = *(a+n*i+j);
  		}
	}

	moutput_a(resulta, n);

	// Generate vector b 
	for(i=0; i< n; i++) 
	{
  		b[i] = 10.0 * drand48();
		resultb[i] = b[i];
	}

	moutput_b(resultb, n);
	
	thread = (pthread_t*)malloc(num_thread*sizeof(pthread_t));
	
	for (i = 0; i < num_thread; i++)
	{
		params = malloc(sizeof(compute_params));
		params.s = i;
		params.n = n;
		params.itt_max = itt_max;
		params.num_thread = num_thread;
		params.a = a;
		params.b = b;
		if(pthread_create(&thread[i], NULL, compute, (void*)params) != 0)
		{
			perror("Can't create thread!");
			free(thread);
			exit(-1);
		}
	}
	
	for (i = 0; i < num_thread; i++)
		pthread_join (thread[i], NULL);

	moutput_itt(err, itt_max);
	
	free(thread);
	return 0;
}

Open in new window

0
ZoppoCommented:
Sorry, I guess the problem is I'm not programming pure C since over 10 years, so I forgot a lot special things.

In C (that's different in C++) you cannot use a struct directly as a variable type. You have either to use the keyword struct for variables too or to use a typedef, i.e.:
struct compute_params
{
	int s;
	int n;
	int itt_max;
	int num_thread;
	double *a;
	double *b;
};

// need to use struct keyword here
struct compute_params params;

Open in new window

or
typedef struct s_compute_params
{
	int s;
	int n;
	int itt_max;
	int num_thread;
	double *a;
	double *b;
} compute_params;

// no need to use struct keyword since it's a typedef
compute_params params;

Open in new window

ZOPPO
0
crazy4sAuthor Commented:
got these errors:
matmul_compute.c: In function `compute':
matmul_compute.c:25: warning: dereferencing `void *' pointer
matmul_compute.c:25: error: void value not ignored as it ought to be
matmul_compute.c:26: error: request for member `s' in something not a structure or union
matmul_compute.c:27: error: request for member `n' in something not a structure or union
matmul_compute.c:28: error: request for member `itt_max' in something not a structure or union
matmul_compute.c:29: error: request for member `num_thread' in something not a structure or union
matmul_compute.c:30: error: request for member `a' in something not a structure or union
matmul_compute.c:31: error: request for member `b' in something not a structure or union
matmul_compute.c: In function `mcompute':
matmul_compute.c:142: error: request for member `s' in something not a structure or union
matmul_compute.c:143: error: request for member `n' in something not a structure or union
matmul_compute.c:144: error: request for member `itt_max' in something not a structure or union
matmul_compute.c:145: error: request for member `num_thread' in something not a structure or union
matmul_compute.c:146: error: request for member `a' in something not a structure or union
matmul_compute.c:147: error: request for member `b' in something not a structure or union

Open in new window


for the below code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <pthread.h>
#include "matmul.h"

void srand48(long int seedval);
double drand48(void);

double *err;

typedef struct s_compute_params
{
	int s;
	int n;
	int itt_max;
	int num_thread;
	double *a;
	double *b;
} compute_params;

void* compute(void* thrd)
{	
	int from, to;
	compute_params* params = (compute_params)* thrd; // struct to be passed
	int s = params.s;
	int n = params.n;
	int itt_max = params.itt_max;
	int num_thread = params.num_thread;
	double *a = params.a;
	double *b = params.b;

	int i, j;
	double  *t;     // pointer to solution vector, space to be malloced 
	double  *t1;    // pointer to next itteraton of solution vector, 
	                // space to be malloced
	double	*ttemp;	// used to swap t1 and t at each itteration
	int	itt;	// current itteration 
	double	sum;	// computes the inner products for A * t
	double 	error;  // max | t1[i] - t[i] |
	double 	errori; // | t1[i] - t[i] |
	
	if( (t=(double *)malloc(sizeof(double)*n)) == NULL) 
	{
  		fprintf(stderr," ERROR : malloc for t failed\n");
	}
	
	if( (t1=(double *)malloc(sizeof(double)*n)) == NULL) 
	{
  		fprintf(stderr," ERROR : malloc for t1 failed\n");
	}
	
	if( (err = (double*) malloc (sizeof (double) * itt_max)) == NULL) 
	{
  		fprintf(stderr," ERROR : malloc for err failed\n");
	}	

	// Initialize t
	
	for(i=0; i< n; i++) 
	{
  		t[i] = b[i];
	}
	
	if( s < num_thread)
	{
		from = (s * (itt_max + 1))/num_thread;
		to = ((s + 1) * (itt_max + 1))/num_thread - 1;
	}
	else if ( s == num_thread)
	{
		from = (s * (itt_max + 1))/num_thread;
		to = ((s + 1) * (itt_max + 1))/num_thread;
	}

	for(itt=from; itt<=to; itt++) 
	{
  		error=0.0;
  		for(i=0; i< n; i++) 
		{
    			sum = 0.0;
			double *a_itt = a + n*i;
			for(j=0; j< n; j++) 
			{
			   sum += *(a_itt++) * t[j];
			}

    		t1[i] = sum + b[i];
    		errori = fabs(t1[i]-t[i]);
    		if(errori > error) 
			{
      			error=errori;
    		}
  		}
		err[itt] = error;
  		ttemp = t1;
  		t1 = t;
  		t = ttemp;
	}

	pthread_exit(0);
	free(params);
}

int mcompute(int n, int seed, int itt_max, int num_thread, double *a, double *b)
{
	srand48((long int)seed);
	float resultb[n];	
	int	i, j;   // indices into arrays
	pthread_t* thread;	// pointer to a group of threads
	compute_params* params;

	float **resulta = (float**)malloc(sizeof(float*)*n);
	for(i=0; i< n; i++)
		*(resulta+i) = (float*)malloc(sizeof(float)*n);

	for(i=0; i< n; i++) 
	{
  		for(j=0; j< n; j++) 
		{
    			*(a+n*i+j) = 1.999 * (drand48() - 0.5) / n;
    			resulta[i][j] = *(a+n*i+j);
  		}
	}

	moutput_a(resulta, n);

	// Generate vector b 
	for(i=0; i< n; i++) 
	{
  		b[i] = 10.0 * drand48();
		resultb[i] = b[i];
	}

	moutput_b(resultb, n);
	
	thread = (pthread_t*)malloc(num_thread*sizeof(pthread_t));
	
	for (i = 0; i < num_thread; i++)
	{
		params = malloc(sizeof(compute_params));
		params.s = i;
		params.n = n;
		params.itt_max = itt_max;
		params.num_thread = num_thread;
		params.a = a;
		params.b = b;
		if(pthread_create(&thread[i], NULL, compute, (void*)params) != 0)
		{
			perror("Can't create thread!");
			free(thread);
			exit(-1);
		}
	}
	
	for (i = 0; i < num_thread; i++)
		pthread_join (thread[i], NULL);

	moutput_itt(err, itt_max);
	
	free(thread);
	return 0;
}

Open in new window

0
ZoppoCommented:
sorry, didn't see it before - here the cast is wrong:
compute_params* params = (compute_params)* thrd

Open in new window

It has to be
compute_params* params = (compute_params*) thrd

Open in new window

ZOPPO
0
crazy4sAuthor Commented:
however the rest of the errors are still occur:
matmul_compute.c: In function `compute':
matmul_compute.c:26: error: request for member `s' in something not a structure or union
matmul_compute.c:27: error: request for member `n' in something not a structure or union
matmul_compute.c:28: error: request for member `itt_max' in something not a structure or union
matmul_compute.c:29: error: request for member `num_thread' in something not a structure or union
matmul_compute.c:30: error: request for member `a' in something not a structure or union
matmul_compute.c:31: error: request for member `b' in something not a structure or union
matmul_compute.c: In function `mcompute':
matmul_compute.c:142: error: request for member `s' in something not a structure or union
matmul_compute.c:143: error: request for member `n' in something not a structure or union
matmul_compute.c:144: error: request for member `itt_max' in something not a structure or union
matmul_compute.c:145: error: request for member `num_thread' in something not a structure or union
matmul_compute.c:146: error: request for member `a' in something not a structure or union
matmul_compute.c:147: error: request for member `b' in something not a structure or union

Open in new window

0
ZoppoCommented:
Excuse me, it seems I'm not really concentrated, is a long day already. Of course if you use a pointer (which is correct here) you need to use "->" instead of "." to de-reference values of the struct like:
	int s = params->s;
	int n = params->n;
	int itt_max = params->itt_max;
	int num_thread = params->num_thread;
	double *a = params->a;
	double *b = params->b;

Open in new window

ZOPPO
0
crazy4sAuthor Commented:
so in mcompute function i should also change from . to ->
            params->s = i;
0
ZoppoCommented:
Yes, you need to do it whereever you access members of a struct via a pointer to the struct, otherwise a ".", i.e.:

compute_params params;
params.s = i;
compute_params* params_ptr = &params;
params_ptr->s = i;

Open in new window

0
crazy4sAuthor Commented:
hmm but in my mcompute function if i use -> i got no errors:
compute_params* params;
for (i = 0; i < num_thread; i++)
	{
		params = malloc(sizeof(compute_params));
		params->s = i;
		params->n = n;
		params->itt_max = itt_max;
		params->num_thread = num_thread;
		params->a = a;
		params->b = b;
		if(pthread_create(&thread[i], NULL, compute, (void*)params) != 0)
		{
			perror("Can't create thread!");
			free(thread);
			exit(-1);
		}
	}

Open in new window


if i use this, i'll get the similar erros as above:
compute_params* params;
for (i = 0; i < num_thread; i++)
	{
		params = malloc(sizeof(compute_params));
		params.s = i;
		params.n = n;
		params.itt_max = itt_max;
		params.num_thread = num_thread;
		params.a = a;
		params.b = b;
		if(pthread_create(&thread[i], NULL, compute, (void*)params) != 0)
		{
			perror("Can't create thread!");
			free(thread);
			exit(-1);
		}
	}

Open in new window

0
ZoppoCommented:
Yes, that's correct, since param is a pointer. In my previous sample I just called the instance param and the pointer param_ptr to show the difference - I usually give my variables names which indicate whether they are pointers or not, often something like 'pParams' or 'ptrParams' or 'ptr_params' or something.

But in your case the pointer's name is 'param', therefor the first version is correct.
0
crazy4sAuthor Commented:
got it!
i was a little confused at first, but thanks:)
0
ZoppoCommented:
You're welcome,

have a nice day,

best regards,

ZOPPO
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.