Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 09-21-2005, 10:38 AM   #1
Registered: Oct 2003
Posts: 249

Rep: Reputation: 30
passing int array to thread?


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


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?

Old 09-21-2005, 10:59 AM   #2
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, 11:00 AM   #3
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

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 02:34 PM.


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

All times are GMT -5. The time now is 11:32 AM.

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