LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-12-2010, 12:12 AM   #1
zak100
Member
 
Registered: Jul 2009
Posts: 262

Rep: Reputation: 2
Passing array to thread function


Hi,
I am trying to pass an array to thread program. Its printing the val[0] value but its not printing value at val[1]. Can somebody help me with this problem.

Code:
  

#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdio.h>

unsigned int counter=0;
pthread_mutex_t mutex= PTHREAD_MUTEX_INITIALIZER;

static void *thread_func(void *arg){
int val[2];
int i;

val[0]=*(int *)arg;
val[1]=*(int *)(arg+1);
printf("val[0]=%d      val[1]=%d", val[0], val[1]);
} 

main(){
pthread_t thread1, thread2;
int thread1_stuff[2];

thread1_stuff[0]=110;
thread1_stuff[1]=10;

pthread_create(&thread1, NULL, thread_func, thread1_stuff);
//pthread_create(&thread2, NULL, thread_func, &thread2_stuff);
pthread_join(thread1, NULL);
//pthread_join(thread2, NULL);
return EXIT_SUCCESS;
}

Zulfi.
 
Old 08-12-2010, 12:42 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Suggested change:
Code:
static void *thread_func(void *arg){
  int *val_p = (int *) arg;

  fprintf(stderr, "val[0]=%d, val[1]=%d\n", val_p[0], val_p[1]);
  ...
 
Old 08-12-2010, 01:27 AM   #3
zak100
Member
 
Registered: Jul 2009
Posts: 262

Original Poster
Rep: Reputation: 2
I have tried this. Its working. Thanks for your help.

Zulfi.
 
Old 08-12-2010, 01:51 AM   #4
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
When I compiled and ran the code, val[1] had a value, but it was the wrong one.

Rewritten your thread function a bit to (hopefully) show where it goes wrong
Code:
static void *thread_func(void *arg){
    int val[2];
    int i;

    val[0]=*(int *)arg;
    val[1]=*(int *)(arg+1);
    printf("val[0]=%d (0x%08X)     val[1]=%d (0x%08X)\n", val[0], val[0], val[1], val[1]);

    for(i=0;i<sizeof(int)*2;i++)
    {
        printf("%p -> 0x%02X\n", arg+i, *(unsigned char*)(arg+i));
    }
    printf("\n");

    return NULL;
}
Output:
Code:
wim@btd-techweb01:~/forums_linuxquestions/lq825699$ ./lq825699
val[0]=110 (0x0000006E)     val[1]=167772160 (0x0A000000)
0xbffff798 -> 0x6E }arg
0xbffff799 -> 0x00 }    }arg+1
0xbffff79a -> 0x00 }    }
0xbffff79b -> 0x00 }    }
0xbffff79c -> 0x0A      }
0xbffff79d -> 0x00
0xbffff79e -> 0x00
0xbffff79f -> 0x00
It displays the address locations of arg (for two integers) and the values that are stored in there.
 
Old 08-12-2010, 11:07 AM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Wim -

You're absolutely right, but let me spell it out:

Code:
  /* Intent: cast "arg" to an int pointer, get the second int value */
  val[1]=*(int *)(arg+1);

  /* Actuality:
     "arg" and "arg+1" are both BYTE pointers (per your sample output)
     We cast "arg+1" to an int pointer...
     ... but we're already screwed...
     ... because we already have the wrong address
   */
Code:
  /* Better (possibly: I haven't actually tried it ;)) */
  val[1] = *((int *)arg)+1)
Code:
  /* Best: just cast the pointer itself, up-front, then just use it */
  int *val_p = (int *) arg;
  val[0] = val_p[0];
  val[1] = val_p[1];
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash array Add function example using indirect array reference as function argument bobywelsh Programming 10 07-05-2010 04:44 AM
passing array and variable to function in bash script ajaypitroda Programming 2 07-07-2009 11:10 PM
ARGGGH! Passing array of structs to function in C. CoderMan Programming 5 02-05-2009 10:44 PM
passing int array to thread? Thinking Programming 2 09-21-2005 11:00 AM
Passing Arguments into the Thread Function George_gk Programming 2 01-31-2005 05:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration