Thread: help about segmention fault

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    3

    help about segmention fault

    I always get a segmentation fault, can someone help me to modify. thanks in advance

    Code:
    #include <pthread.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/time.h>
    #include <string.h>
    #define MAX 10
    pthread_t thread[6];
    
    void *thread1()
    {
    
        int *a[16];
        int i, n;
    
    
        for (i = 0; i < 16; i++)
        {
    
        n=(size_t) ((rand() * 256.0) / (RAND_MAX + 1.0)) + 1;
            a[i] = (int*)malloc(n);
        if (a[i]==NULL)
            {
                printf("Error allocating memory!");
                exit (1);
            }
    
           printf("malloc a[%d]\n",i);
    
        }
    
        for (i = 16; i > 0; i--)
        {
    
            n=(size_t) ((rand() * 256.0) / (RAND_MAX + 1.0)) + 1;
            printf("remalloc a[%d]\n",i-1);
            a[i - 1] = (int*)realloc(a[i - 1], n);
        if ( a[i - 1]==NULL)
            {
                printf("Error reallocating memory!");
                exit (1);
            }
    
    
        }
        for (i = 0; i < 16; i += 2 )
            free(a[i]);
            printf("free1 a[%d]\n",i);
        for (i = 16; i > 0; i -= 2)
            free(a[i - 1]);
            printf("free2 a[%d]\n",i-1);
            pthread_exit(NULL);
    }
    
    
    void thread_create(void)
    {
            int temp,i;
            memset(&thread, 0, sizeof(thread));          //comment1
            /**/
           for (i = 6; i > 0; i--)
    {
            if((temp = pthread_create(&thread[i], NULL, thread1, NULL)) != 0)       //comment2
                    printf("thread create failed\n");
            else
                    printf("thread create\n");
    }
    }
    
    void thread_wait(void)
    {
       int i;
       for (i = 6; i > 0; i--)
    {
            if(thread[i] !=0) {                   //comment4
                    pthread_join(thread[i],NULL);
                    printf("1\n");
            }
    }
    }
    
    int main()
    {
    
            printf("\n");
            thread_create();
            printf("\n");
            thread_wait();
    
            return 0;
    }
    Last edited by yanglei_fage; 11-03-2011 at 09:04 PM.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You should have your debugger attached and when it breaks into the debugger, tell us which line is breaks on.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You're accessing thread out of bounds.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why did you put this in C++?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    pthread_t thread[6];
    Valid indexes: 0 - 5

    Code:
    for (i = 6; i > 0; i--)
    {
            if((temp = pthread_create(&thread[i], NULL, thread1, NULL)) != 0)
    BOOM!

  6. #6
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    Gosh, I get an error on that "if" line , but I don't know why?
    Code:
    >  gcc -Wall -W -pedantic 20111104-pthread.cpp -l pthread -o 20111104-pthread.bin
    20111104-pthread.cpp: In function ‘void thread_create()’:
    20111104-pthread.cpp:66: error: invalid conversion from ‘void* (*)()’ to ‘void* (*)(void*)’
    **********
    
    void thread_create(void)
    {
            int temp, i;
            memset(&thread, 0, sizeof(thread));          //comment1
            /**/
            for (i = 6; i > 0; i--)
    {
            if((temp = pthread_create( &thread[i],
                                       NULL,
                                       thread1,
                                       NULL
                                     )         // line 66
                )
                != 0)       //comment2
    **********
    
    // The prototype is:
      int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                         void *(*start_routine) (void *), void *arg);

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Because thread function should be
    void *thread1(void *p)

    Not
    void *thread1()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    Oh yeah! (void*) IS an object! duh
    Last night I was thinking (void), (void*) what's the difference? They're both nothing...
    Oops so now there is an unused parameter, and how about this linker error?
    Code:
    >  gcc -Wall -W -pedantic 20111104-pthread.cpp -lpthread  -o 20111104-pthread.bin
    20111104-pthread.cpp:9: warning: unused parameter ‘p’
    /tmp/ccZQ2uCa.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
    collect2: ld returned 1 exit status
    Who does that belong to?
    thanks

  9. #9
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    Oh, I see the linker problem was because the gcc input filename extension was ,cpp. So I simply changed to .c.

    So now I can run. Interestingly though, with the "for" condition left at:
    Code:
    pthread_t thread[6];
    ...
           for (i = 6; i > 0; i--){
    I get no segfault. Should that be ok?

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I get no segfault. Should that be ok?
    No, you just got the other side of the "luck" coin - in this case, your bad luck (and the OP's good luck).

    Stepping outside an array is bad news, regardless of whether it crashes or not.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I still fail to see why you posted C code in the C++ section?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with seg fault
    By homer_3 in forum C Programming
    Replies: 3
    Last Post: 02-05-2010, 01:33 PM
  2. Seg fault
    By lihvar in forum C Programming
    Replies: 2
    Last Post: 03-10-2006, 05:02 AM
  3. Seg fault out of no where
    By mart_man00 in forum C Programming
    Replies: 5
    Last Post: 02-25-2003, 08:51 PM
  4. Seg Fault... ???
    By justin69enoch in forum C Programming
    Replies: 2
    Last Post: 01-26-2003, 05:00 AM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM