Thread: Segmentation fault, I can't see why?

  1. #1
    Registered User
    Join Date
    Oct 2007

    Segmentation fault, I can't see why?

    struct sched_param *sp_daemon;
    sp_daemon = alloca(sizeof(*sp_daemon));
    sp_daemon->sched_priority = SCHED_SS_DAEMON_PRIORITY; //Just some high RT prio
    printf("THIS GETS PRINTED");
    if(pthread_setschedparam(pthread_self(), SCHED_FIFO, sp_daemon)) {
        perror("1. pthread_setschedparam failed!");
        return -1;
    Why do I get segfault here? I cant see it? wht am I doing wrong??

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    Is alloca() really what you want here? If you want a local variable on the stack, then just use one. If you want to ALLOCATE some memory for your structure, then use malloc() or similar.

    alloca is great for when you need some function-local dynamic (and relatively small) space.

    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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > if(pthread_setschedparam(pthread_self(), SCHED_FIFO, sp_daemon))
    The memory you pass to a thread needs to have a lifetime which is at least as long as the life of the calling function, and the life of the thread which is to be run.

    Local variables don't normally fall into this category.
    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.

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, 05: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, 12:53 PM