LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-14-2013, 03:00 PM   #1
jyunker
Member
 
Registered: Aug 2009
Posts: 167

Rep: Reputation: 0
Passing Arrays to Functions and Program Compiles with Warnings.


I am trying to compile the following code and I get two warnings. What am I doing wrong?

The code is:

Code:
int sum1;
int sum2;

void thread1(int v[], int v_count) {
sum1 = 0;
int i;
for (i = 0; i < v_count; i++)
sum1 += v[i];
}
void thread2(int v[], int v_count) {
sum2 = 0;
int i;
for (i = 0; i < v_count; i++)
sum2 += v[i];
}


int main ( int argc, char** argv )
{
   int N = 5;
   int i;
   int array1[N];
   for (i =0; i < N; i++) 
   array1[i]= i;
   thread1(array1[i],N);
   thread2(array1[i],N);
     
   return 0;
}

Now when I compile it I get the follwing output; It consists of two warnings:

cc example2.c -o exam -lm -lrt -g
example2.c: In function ‘main’:
example2.c:25: warning: passing argument 1 of ‘thread1’ makes pointer from integer without a cast
example2.c:4: note: expected ‘int *’ but argument is of type ‘int’
example2.c:26: warning: passing argument 1 of ‘thread2’ makes pointer from integer without a cast
example2.c:10: note: expected ‘int *’ but argument is of type ‘int’


The program compiles, but it gives the above warning and it then crashes during execution.


The output is:

./exam
Segmentation fault (core dumped)


Now I believe that these warnings and the program crash are all interconnected and I am not sure what is going on.

I am trying to populate an array, array1[], with integer values and then send it to one of two functions, thread1 or thread2. It does not seem to get there hence the execution crashes.

I just need to know how to fix this program so it runs.

Any help appreciated. Thanks in advance.

Respectfully,


jyunker
 
Old 11-14-2013, 06:38 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,436

Rep: Reputation: 1507Reputation: 1507Reputation: 1507Reputation: 1507Reputation: 1507Reputation: 1507Reputation: 1507Reputation: 1507Reputation: 1507Reputation: 1507Reputation: 1507
Code:
thread1(array1[i],N);
is passing just the value of a single element of the array (and even worse, since i==N at that point it's the content of a memory location one element beyond the end of the array). When you want to reference the whole array, omit the subscript:
Code:
thread1(array1,N);
 
Old 11-15-2013, 01:15 PM   #3
jyunker
Member
 
Registered: Aug 2009
Posts: 167

Original Poster
Rep: Reputation: 0
modifying serial code to run on two cores usig p threads

Now how do I modify the code so thread1 is on one thread (say thread 1) and thread2 is on another thread (say thread 2).

I am using p threads.

Any help appreciated. Thanks in advance.

Respectfully,

jyunker
 
Old 11-15-2013, 01:28 PM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,696

Rep: Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261
I would say refer to the manual... the man page on pthread_create.

If it looks anything like mine, there is a complete example included.
 
  


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
problem in passing struct arrays to functions in C shifter Programming 2 10-15-2013 12:55 PM
How to execute a program for passing structures to functions? Pranav Prabhu Programming 2 11-23-2011 07:34 PM
perl: Passing arrays to subroutine kenneho Programming 8 07-07-2009 06:14 AM
Simple C++ Program: Program Compiles But Won't Run (Segmentation Fault) violagirl23 Programming 3 01-09-2008 12:09 AM
Passing bidimensional arrays to function in C pittopitto Programming 5 05-18-2006 09:31 AM


All times are GMT -5. The time now is 12:37 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration