Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/How can i handle - cast to pointer from integer of different size?
Next
Answered
Fiona Davis
  • 29
Fiona Davis
Asked: May 18, 20222022-05-18T18:49:57+00:00 2022-05-18T18:49:57+00:00In: c

How can i handle – cast to pointer from integer of different size?

  • 29

. Advertisement .

..3..

. Advertisement .

..4..

I get an error:

cast to pointer from integer of different size

when I try to run the following code:

#include <stdio.h>
 #include <stdlib.h>
 #include <time.h>
 #include <sys/time.h>
 #include <pthread.h>
 
 #define NTHREADS 4
 
 int dim ;
 pthread_mutex_t m; /* Mutex protecting the sum value */
 pthread_t thread_id[NTHREADS]; /* Thread ids */
 float **A, **B, **C;
 
 void *prod (void *s){
  int *id=(int *)s;
  int idd=*id;
 
 
  /* Define local variables */
  int i,j,k, start, end, len ;
  float **Aa, **Bb, **Cc;
 
  start = dim*idd; /* Start of this threads slice of the vectors */
  end = start + dim; /* End of the slice */
 
 
  for (i = 0 ; i < dim; i++)
  {
  for (j = 0; j < dim; j++)
  {
  Cc[i][j] = 0;
  for (i=start; i<end ; i++) {
 
  Cc[i][j] += Aa[i][k] * Bb[k][j];
  }
  }
  }
  pthread_mutex_lock (&m); /* Lock the mutex */
  C[i][j] += Cc[i][j]; /* Update the shared variable */
  pthread_mutex_unlock (&m); /* Unlock the mutex */
 
  pthread_exit(NULL); /* Done! */
 }
 
 int main ( int argc, char *argv[] )
 {
  void *status;
  float **A, **B, **C;
  int i,j,k;
 
  if ( argc == 2)
  dim = atoi(argv[1]); // get the dimension of the matrix
  // from the command prompt
 
  else
  dim = 128;
 
 
 
  A = (float **)malloc(sizeof(float*)*dim);
  B = (float **)malloc(sizeof(float*)*dim);
  C = (float **)malloc(sizeof(float*)*dim);
 
  for (i = 0 ; i < dim; i++)
  {
  A[i] = (float *)malloc(sizeof(float)*dim);
  B[i] = (float *)malloc(sizeof(float)*dim);
  C[i] = (float *)malloc(sizeof(float)*dim);
  }
 
  for (i=0; i<dim; i++)
  {
  for (j = 0 ; j < dim; j++)
  {
  A[i][j]=rand();
  B[i][j]=rand();
  }
  }
 
  struct timeval t1, t2;
  gettimeofday(&t1, NULL);
 
  // you need to parallelize this
  // perform the multiplication
  for(i=0;i<NTHREADS;i++) {
 
  pthread_create(&thread_id[i], NULL, prod, (void *)i);
  }
  /* Wait on the other threads */
  for(i=0;i<NTHREADS;i++) {
  pthread_join(thread_id[i], &status);
  }
 
  gettimeofday(&t2, NULL);
 
  double t = (t2.tv_sec - t1.tv_sec) + (t2.tv_usec - t1.tv_usec ) / 1000000.0;
  // take the difference and report it in seconds
  printf("execution time %f seconds\n",t);
 }
pthread_create(&thread_id[i], NULL, prod, (void *)i);

How to fix the cast to pointer from integer of different size. Please give me some good ideas.

pthreads
  • 2 2 Answers
  • 61 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    hdtutoria Expert
    2022-06-13T12:45:50+00:00Added an answer on June 13, 2022 at 12:45 pm

    The cause: This error appears because you’re converting an int to void *, which may have a different size.

    The wrong line is:

    int *id=(int *)s;

    The solution: When you run the pthead_create function to convert an int to a void*, you should convert it back to an integer type. So, it should be:

    int id = (int)s; 

     

     

    • 20
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Rose Morvan
    2022-05-25T20:48:58+00:00Added an answer on May 25, 2022 at 8:48 pm

    Correct way to achieve this is by referencing the variable “i” (check http://man7.org/linux/man-pages/man3/pthread_create.3.html):

    pthread_create(&thread_id[i], NULL, prod, (void *)&i);
    • 10
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.