Help with segmentation fault please!

This is a discussion on Help with segmentation fault please! within the C Programming forums, part of the General Programming Boards category; I have an assignment to implement a module for something called a SIX Lock. It's basically a complex locking mechanism ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    2

    Help with segmentation fault please!

    I have an assignment to implement a module for something called a SIX Lock. It's basically a complex locking mechanism used for concurrent data access. In any case I'm getting a segmentation fault that I've traced to one line of code but cannot figure out why!

    So I have this "class" implementing this SIXlock structure as defined below:

    Code:
    typedef struct
    {
    	pthread_mutex_t mutex;			/*our mutex*/
    	pthread_cond_t not_locked;		/*cond variable */
    	SIXlock_mode_t groupMode;		/*our overall group mode*/
    	pthread_t *myThreads;			/*pointer to threads*/
    	SIXlock_mode_t *grantedMode;	/*lock mode of each thread */
    	int numThreads;				/*number of threads in array*/
    
    } SIXlock_lock_t;
    Yup...I'm using the pthreads library and that's about it.
    So I have this create function which starts off like this:

    Code:
    /*-------------------------------------------------------------------------
    /Function: 	SIXlock_create
    /Input:		Pointer to a SIXlock object
    /Output:	Returns parameter pointer pointing to created lock
    /		Returns SIXlock_result_t notifying the user of the success/failure
    -------------------------------------------------------------------------*/
    SIXlock_result_t SIXlock_create(SIXlock_lock_t **lockPointer)
    {
    	int i;
    	lockPointer = NULL;
    
    	/*setup memory allocation*/
    	lockPointer = (SIXlock_lock_t **)malloc(sizeof( SIXlock_lock_t ));
    
    	/*if error, return result*/
    	if(lockPointer == NULL)
    		return SIXLOCK_RESULT_ERROR_MALLOC;
    	
    	/*initialize*/
    	pthread_mutex_init(&((*lockPointer)->mutex), NULL);
    
    ....
    
    }
    but when it gets to the:

    pthread_mutex_init(&((*lockPointer)->mutex), NULL);

    line, it throws a segmentation fault. Can someone please help????

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You haven't got the right indirection when setting your lockpointer - it should be
    Code:
    *lockpointer = malloc(...);
    Note also that you SHOULD NOT cast malloc in C - if you are using a C++ compiler, then it may be acceptable (but then you should be using new/delete!)

    Edit: And of course the
    Code:
    lockpointer = NULL;
    should be
    Code:
    *lockpointer = NULL;
    --
    Mats
    Last edited by matsp; 11-04-2008 at 08:13 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    2
    Thanks! Wow...don't know how I couldn't figure that one out

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 06:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 01:53 PM

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