LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 09-21-2005, 11:38 AM   #1
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Rep: Reputation: 30
passing int array to thread?


hiho@ll

how does this really work:
Code:
int array[2]={0};
array[0]=10;
array[1]=20;

pthread_create(&thread,NULL,this->mythread,(void*)array);

void* mythread(void *arg){
 int *tmp=(int*)arg;
 printf("OUTPUT: %d %d\n",tmp[0],tmp[1]);
}
i get:
OUTPUT: 134583088 1078320720

what's this??
i think it's a cast problem
but how do i pass a int array to a thread?

thx@ll
 
Old 09-21-2005, 11:59 AM   #2
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
I'm guessing your array is a local variable on the stack. Once you leave the function that you declared that variable in, the memory for it goes away, so you are probably using a dangling pointer in your thread function. Try allocating the memory for the array with new/malloc instead.
 
Old 09-21-2005, 12:00 PM   #3
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Original Poster
Rep: Reputation: 30
edit: thx, i didn't see your post because i wrote this post beneath while you wrote your reply ;-)
i'll check it tomorrow
thx


i think the problem was/is that i want to code something like a proxy class

the proxy class generates two threads which are listening on 2 sockets using select();
because i want it really full duplex i use 2 threads

if i use a static char* buffer both threads get the socket descriptor numbers

but i can't really use a static buffer, not?
because if there is another object, the threads will overwrite themself, not?
hmm *thinking*

Last edited by Thinking; 09-21-2005 at 03:34 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
segfault when passing array[] as argument froboozle Programming 11 06-28-2005 04:06 PM
passing an array from html-forms to php prabhatsoni Linux - Software 2 06-04-2005 01:11 AM
Passing Arguments into the Thread Function George_gk Programming 2 01-31-2005 06:03 AM
invalid types int[int] for array subscript scuzzman Programming 2 11-16-2004 10:34 PM
Char array to int without losing value ? Dimitris Programming 3 01-14-2004 01:08 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration