This code keeps giving me a segmentation fault why?


 
Thread Tools Search this Thread
Top Forums Programming This code keeps giving me a segmentation fault why?
# 1  
Old 12-24-2012
This code keeps giving me a segmentation fault why?

Code:
#include<stdlib.h>
#include <pthread.h> 
#include "tlpi_hdr.h"
#include <stdio.h> 
static volatile int glob = 0;

static struct { 
pthread_t t1,t2; 
} *thread; 


static void *     /* Loop 'arg' times incrementing 'glob' */ 
threadFunc(void *arg) 
{ 

int loops = *((int *) arg); 
int loc, j, s;
 
if (s != 0)
errExitEN(s, "pthread_mutex_lock");
for (j = 0; j < loops; j++) { 
loc = glob; 
printf("The ID of this thread is: %u\n", (unsigned int)pthread_self());
printf("glob = %d\n", glob);
sleep(2);
loc++; 
glob = loc; 
if (s != 0)
errExitEN(s, "pthread_mutex_unlock");


} 
return NULL; 
} 


int 
main(int argc, char *argv[]) 
{ 
int loops=0;
int s; 

loops = (argc > 1) ? getInt(argv[1], GN_GT_0, "num-loops") : 10000000; 
s = pthread_create(&thread[loops].t1, NULL, threadFunc, &loops);
if (s != 0) 
errExitEN(s, "pthread_create"); 
s = pthread_create(&thread[loops].t2, NULL, threadFunc, &loops);
if (s != 0) 
errExitEN(s, "pthread_create"); 
s = pthread_join(thread[loops].t1, NULL); 
if (s != 0) 
errExitEN(s, "pthread_join"); 
s = pthread_join(thread[loops].t2, NULL); 
if (s != 0) 
errExitEN(s, "pthread_join"); 
printf("final glob = %d\n", glob); 
exit(EXIT_SUCCESS); 
}

# 2  
Old 12-25-2012
You don't allocate any memory for your "thread" variable to point to.

So "thread[loops]" point to some random location.
This User Gave Thanks to achenle For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Why does this example C code run and yet SHOULD either not compile or give a segmentation fault?

Apologies for any typos... Well guys, been researching 'goto' in C and they say that you can't 'goto' labels in another function as a segmentation fault will occur. However I have found a way to 'goto' a label in another function that is NOT main() using the asm() function. As you know I... (14 Replies)
Discussion started by: wisecracker
14 Replies

2. Programming

C. To segmentation fault or not to segmentation fault, that is the question.

Oddities with gcc, 2.95.3 for the AMIGA and 4.2.1 for MY current OSX 10.14.1... I am creating a basic calculator for the AMIGA ADE *NIX emulator in C as it does not have one. Below are two very condensed snippets of which I have added the results inside the each code section. IMPORTANT!... (11 Replies)
Discussion started by: wisecracker
11 Replies

3. Solaris

Segmentation fault

Hi Guys, I just installed and booted a zone called testzone. When I logged in remotely and tried changing to root user I get this error: "Segmentation fault" Can someone please help me resolve this? Thanks alot (2 Replies)
Discussion started by: cjashu
2 Replies

4. Homework & Coursework Questions

Segmentation Fault

this is a network programming code to run a rock paper scissors in a client and server. I completed it and it was working without any error. After I added the findWinner function to the server code it starts giving me segmentation fault. -the segmentation fault is fixed Current problem -Also... (3 Replies)
Discussion started by: femchi
3 Replies

5. Programming

Using gdb, ignore beginning segmentation fault until reproduce environment segmentation fault

I use a binary name (ie polo) it gets some parameter , so for debugging normally i do this : i wrote script for watchdog my app (polo) and check every second if it's not running then start it , the problem is , if my app , remain in state of segmentation fault for a while (ie 15 ... (6 Replies)
Discussion started by: pooyair
6 Replies

6. Programming

C code : Segmentation fault

Hi Friends, I have written a small code in C which performs the below operations Task : 1 ) read line by line from a file. 2 ) assuming 3th and 4th fields of the file as GN and GNTO 3 ) The target file should contain all the fields except GNTO. 4... (3 Replies)
Discussion started by: kiran_bhatter
3 Replies

7. Programming

segmentation fault.

This code is causing a segmentation fault and I can't figure out why. I'm new to UNIX and I need to learn how to avoid this segmentation fault thing. Thank you so much. Thanks also for the great answers to my last post.:):b: int main() { mysqlpp::Connection conn(false); if... (3 Replies)
Discussion started by: sepoto
3 Replies

8. AIX

Segmentation fault in nsgetcinfo in aix 64-bit c code

Hello, I am running on a AIX5.2 server with Oracle 10g and 9i. My code compiles and works fine in 32-bit mode. The same code compiles in 64-bit and runs good. The program catches CNTRL-C signal to terminate. Only on 64-bit code when i hit CNTRL-C, the program exits with segmentation... (0 Replies)
Discussion started by: sumesh0710
0 Replies

9. UNIX for Dummies Questions & Answers

Segmentation Fault

Hi, While comparing primary key data of two tables thr bteq script I am getting this Error. This script is a shell script. *** Error: The following error was encountered on the output file. Script.sh: 3043492 Segmentation fault(coredump) Please let me know how to get through it. ... (5 Replies)
Discussion started by: monika
5 Replies

10. Programming

Why not a segmentation fault??

Hi, Why I don't receive a segmentation fault in the following sample. int main(void) { char buff; sprintf(buff,"Hello world"); printf("%s\n",buff); } If I define a buffer of 10 elements and I'm trying to put inside it twelve elements, Should I receive a sigsev... (22 Replies)
Discussion started by: lagigliaivan
22 Replies
Login or Register to Ask a Question