Like Tree4Likes
  • 1 Post By Salem
  • 2 Post By Salem
  • 1 Post By vart

Pthread and malloc -Aborted (core dumped)

This is a discussion on Pthread and malloc -Aborted (core dumped) within the C Programming forums, part of the General Programming Boards category; c prog that take int x and pass it to thread to return x+10 i tried to free()mem i allocate ...

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    13

    Pthread and malloc -Aborted (core dumped)

    c prog that take int x and pass it to thread to return x+10
    i tried to free()mem i allocate in prog using malloc
    // problem i get this error Aborted (core dumped)
    note :
    prog work fine (test on cygwin) if i remove free(exitstat);
    but that lead to mem leak
    Code:
    #include <pthread.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    // c prog that take a int x and pass it to thread to return x+1
    // i tried to free()mem i allocate in prog using malloc
    void *fun(void *TX);
    
    
    int main()
    
    {
    
    int x =50;
    void *exitstat;
    pthread_t thread1;
    pthread_create(&thread1, NULL, fun, &x);
    pthread_join(thread1, &exitstat);
    int *result=(int *)exitstat;
    printf("%d,",*result);
    
    // problem is here i get this error  Aborted (core dumped)
    free(exitstat);
    
    return 0;
    }
    
    void *fun(void *TX)
    {
    int *num = (int*)malloc(sizeof(int)) ;
    num=(int*)TX;
    (*num)=(*num)+10;
    //free(num); // can't free it here need to return num
    return num;
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    Code:
    int *num = (int*)malloc(sizeof(int)) ;
    num=(int*)TX;
    Why do you malloc anything if you just overwrite the pointer immediately afterward? This causes a memory leak (you have this leak even if you call free(exitstat)), you lost the address returned by malloc and thus can't free anything. You are returning this changed address (i.e. address not returned by malloc), then trying to free that. That is the cause of the crash.

    But you don't actually need to malloc or free anything. You pass in a pointer to x as the data to fun. Inside fun, you can add 10 to the number stored at that pointer, then return the pointer again. It will still be pointing to x in main, which will have the new value.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by anduril462 View Post
    But you don't actually need to malloc or free anything. You pass in a pointer to x as the data to fun. Inside fun, you can add 10 to the number stored at that pointer, then return the pointer again. It will still be pointing to x in main, which will have the new value.
    Which could be done without thread. Just calling f function directly on x will work faster...
    Using join_thread in the same function where the x is allocated is pointless...

    If we go to something relative real
    1. create thread in one function and use join_thread in another - where the parameter variable is not available
    2. 1 will make passing pointer to local var to thread pointless
    3. So you will have to allocate space before calling thread, put value into allocated space and pass the pointer to allocated memory.
    4. In the function you will read the passed value - free the memory, increase the value.
    5. You will return the new value (not pointer to the local or allocated variable)
    6. In the waiting function you will check the exit code.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Jul 2013
    Posts
    13

    thanks for your help

    thanks for your help but what i really want to understand how to return any variable from thread
    (int -double -float- string- struct ..... )


    this code work fine for int but for double error show up
    cannot convert to a pointer type


    Code:
    #include <pthread.h> 
    #include <stdio.h>
    #include <Math.h>
    void*  compute_prime  (void*  arg) 
    {
       int num=*((int*)arg);
       
       double SQRT=(sqrt(num));
       
       
       
     
       
    // return (void*)  SQRT;
                return  (void*)  num; 
           } 
        
    
     int  main  () 
    {
      pthread_t  thread; 
      
      int  prime; 
     int value =25;
       pthread_create  (&thread,  NULL,  &compute_prime,  &value); 
       pthread_join  (thread,  (void*)  &prime); 
       printf("number is %G.\n",  prime); 
       return  0;
    Last edited by Hassan Ahmed; 07-08-2013 at 10:28 PM.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    Quote Originally Posted by Hassan Ahmed View Post
    thanks for your help but what i really want to understand how to return any variable from thread
    (int -double -float- string- struct ..... )


    this code work fine for int but for double error show up
    cannot convert to a pointer type
    You don't just get to use whatever types you want wherever you want and expect it to work. C isn't like that. You have to be careful to use the same data type everywhere, or to safely convert it. If you are returning an int *, then the data it points to should be an int, and the pointer you give to pthread_join must be a pointer to int. Also, if you want to print the value after pthread_join, you must use the correct printf format specifier. Currently you have a mix of int and double. Pick one type for the return statement from computer_prime, the variable containing the return value (i.e. prime), and the printf format specifier.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    Yet more cross-posting
    Your pthread_create calls are also broken by passing a pointer to a local variable.
    You're only 'saved from failure' by calling join.

    First, you allocate space for the thread arguments
    Code:
    p = malloc()
    *p = ....
    pthread_create  (&thread,  NULL,  &fun, p);
    The thread begins with
    Code:
    void *fun ( void *arg ) {
      type *p = arg;
      // extract the data, then
      free(p);
      // carry on with the work
    }
    The thread ends with
    Code:
    r = malloc()
    *r = ....
    return r;
    The thread consumer (the thing waiting for the thread to end) is
    Code:
    void *answer;
    pthread_join  (thread,  &answer); 
    type *r = answer;
    // use the answer
    free( answer );
    It doesn't matter where you create or join the thread, or what happens in the mean time.
    The life-time and uniqueness of the parameter memory and the result memory is assured.
    vart likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    If retval is not NULL, then pthread_join() copies the exit status of the target thread (i.e., the value that the target thread supplied to pthread_exit(3)) into the location pointed to by *retval.
    Look to me like instead of return pthread_exit should be called
    and if you pass int* pointer to pthread_exit
    you need to pass int** pointer to pthread_join which will copy the pointer value of int*

    Then you'll be able to read the int value and free the int* pointer
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Registered User
    Join Date
    Jul 2013
    Posts
    13

    thanks for your help

    finally get it
    Code:
    #include <pthread.h> 
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct 
    {char*name;
    int age;
    int arr[10];
    }person;
    
    void*fun(void *arg);
    
    int main()
    {
    person x;
    x.name="frommain";
    x.age=100;
    int i;
    pthread_t  thrd;
    void *exit;
    pthread_create(&thrd,NULL,fun,&x);
    pthread_join(thrd,&exit);
    
    printf("-------------- in main\n");
    person *result=(person*)exit;
    printf("name is %s\n",result->name);
    printf("age is %d\n",result->age);
    for(i=0;i<10;i++)
    {printf("a[%d] is %d\n",i,result->arr[i]);}
    
    
    
    
    
    
    free(exit);
    return 0;
    }
    
    void*fun (void*arg)
    {int i;
    
    person *ax=(person*)arg;
    printf("name is %s\n",ax->name);
    printf("age is %d\n",ax->age);
    person *ptr =(person*)malloc(sizeof(person));  
    ptr->name="alipapa";
    ptr->age=10;
    for(i=0;i<10;i++)
    {ptr->arr[i]=i;}
    //printf("name is %s\n",ptr->name);
    //printf("age is %d\n",ptr->age);
    
    
    
    return ptr;
    }

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    you should work on code alignment. Choose one style and use it. It will help catch nasty errors caused by misaligned code in the future.
    There are automatic tools like astyle utility which will help to keep your code nicely aligned.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    > person x;
    This is a local variable, which you're handing over to another thread (via a pointer).
    If this variable goes out of scope before the thread exits, you're toast.

    > void *exit;
    You should avoid naming variables with the same name as standard functions.

    > person *result=(person*)exit;
    > person *ax=(person*)arg;
    > person *ptr =(person*)malloc(sizeof(person));
    This is C, there is no need to cast void* pointers in order to perform assignment.
    vart and Hassan Ahmed like this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Registered User
    Join Date
    Jul 2013
    Posts
    13
    Quote Originally Posted by Salem View Post
    > person x;
    This is a local variable, which you're handing over to another thread (via a pointer).
    If this variable goes out of scope before the thread exits, you're toast.

    > void *exit;
    You should avoid naming variables with the same name as standard functions.

    > person *result=(person*)exit;
    > person *ax=(person*)arg;
    > person *ptr =(person*)malloc(sizeof(person));
    This is C, there is no need to cast void* pointers in order to perform assignment.
    how can i be sure 100% that variable not goes out of scope before the thread exits

    or it is race condition and luck

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by Hassan Ahmed View Post
    how can i be sure 100% that variable not goes out of scope before the thread exits

    or it is race condition and luck
    yes, it is race condition. So you cannot be sure. So you cannot use local vars as parameters to thread function.
    Hassan Ahmed likes this.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault (core dumped)
    By Kevin Jerome in forum C++ Programming
    Replies: 5
    Last Post: 09-09-2012, 12:58 AM
  2. Aborted (core dumped)? Error when running
    By Brian Justice in forum C++ Programming
    Replies: 8
    Last Post: 05-19-2012, 10:35 PM
  3. Segmentation fault (core dumped)????
    By yosipoa in forum C++ Programming
    Replies: 2
    Last Post: 07-20-2011, 01:18 PM
  4. Cause of a seg fault (core dumped)
    By towed in forum C Programming
    Replies: 3
    Last Post: 01-15-2011, 10:41 PM
  5. Bus error (Core Dumped)
    By RandomX in forum C Programming
    Replies: 10
    Last Post: 12-11-2006, 08:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21