Thread: Printing variables safely using printf, wprintw

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    43

    Printing variables safely using printf, wprintw

    I was interested to know if there is any process already in place to check variables when printing them using printf or wprintw.

    Here is the scenario. There are variables being changed and altered in another thread. Then in another thread wprintw prints the contents of the variable to the screen terminal concurrently. Sometime the program segfaults randomly.
    A hack (although I'm not sure if actually works) is to do a check like so:

    Code:
    wprintw(window, "%d\n", X ? X : 0);
    Check if X is valid, but if not then output 0 instead.

    Is this really necessary? Does this just test if X == 0 or does it test if X is an int?

    Any knowledge greatly appreciated.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you declared int X; then X is an int.

    Your segfault is being caused by something else.

    Perhaps if you compile everything with the "-g" flag, and run the whole thing in a debugger, you'd get a better answer.

    You'll find these gdb commands useful
    info threads - display all the threads
    info local - show local variables in the current stack frame
    up
    down - stack frame navigation
    bt - print stack backtrace

    So when the program segfaults, use 'bt' to find out where, use 'up' to locate the innermost function which you wrote, then start examining variables.

    > A hack (although I'm not sure if actually works) is to do a check like so:
    > wprintw(window, "%d\n", X ? X : 0);
    Welcome to the world of cargo cult programming.
    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.

  3. #3
    Registered User
    Join Date
    Jan 2016
    Posts
    43
    I have already debugged the problem and the problem lies somewhere in the ncurses (wprintw) implementation that I have done. Basically there are variables being updated in another thread and the ncurses wprintw lazily prints to term in another thread. It is possible wprintw is catching X in the middle of another atomic operation occurring on X from the other thread. The obvious solution is mutexs.

    I would still like to know the answer to my original question. In printf what happens if the print function is called when the variable is not fully updated?

    Second, what's the way to print variables in an absolutely lazy fashion without using mutexs or slow the program down?

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by wiqxlzxsxj View Post
    I would still like to know the answer to my original question. In printf what happens if the print function is called when the variable is not fully updated?
    I assume what you're asking is if it's possible for printf to actually segfault in that situation, which seems unlikely if all it's doing is accessing the value of an integer. It will just get the wrong value. It could only cause a segfault if it was used as an array index or something like that. If X is a pointer then obviously a bad value could cause a segfault.

    If window is a pointer, you should check that it's value is good (not just non-null, but its proper value). Actually, even if it's an integer index into an array, or whatever, check it. Could it have been stomped somewhere?

    It is possible wprintw is catching X in the middle of another atomic operation occurring on X from the other thread. The obvious solution is mutexs.
    I don't see how that would be possible. Do you know what an "atomic operation" is? Kind of means the opposite.

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    Quote Originally Posted by wiqxlzxsxj View Post
    I have already debugged the problem and the problem lies somewhere in the ncurses (wprintw) implementation that I have done. Basically there are variables being updated in another thread and the ncurses wprintw lazily prints to term in another thread. ...
    If you are using threads, is your implementation of ncurses thread safe? I was not aware that there were any.

  6. #6
    Registered User
    Join Date
    Jan 2016
    Posts
    43
    I am asking if printf will segfault? But by atomic I am referring to atomicity of a given operation in each mutually exclusive thread. So if X is being changed in the other thread and at the same, coincidentally, printf from another tries to read X what value of X will printf get if an int at all?

    I understand there is a separate thread-safe implementation of ncurses. I'm using the standard -dev package.

    If I alter my implementation slightly such that the ncurses window gets updated in series after X is modified (as you would in a single thread) then after a while the terminal starts getting incrementally garbled.

  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
    Short, Self Contained, Correct Example
    Can you produce one of these - a two-thread ncurses program which demonstrates the problem?
    We might then be able to tell you what other things you're doing wrong.

    Because reading the int isn't your real problem, it's something else.

    Or let me put it another way:
    wprintw(window, "%d\n", 42);
    does this crash in the same random way?
    If so, then it's nothing to do with reading integer variables.
    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
    Jan 2016
    Posts
    43
    OK that's a fair enough explanation I was looking for. In terms of the assembly instructions, an int will always be an int.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing pointer to int in printf ( )
    By adi_K in forum C Programming
    Replies: 6
    Last Post: 01-28-2016, 08:58 PM
  2. Printf printing twice
    By travis9090 in forum C Programming
    Replies: 4
    Last Post: 11-17-2015, 02:23 AM
  3. printf in a while loop is printing twice instead of once...
    By Danni111111 in forum C Programming
    Replies: 2
    Last Post: 09-21-2013, 01:04 AM
  4. printf printing twice?
    By drshmoo in forum C Programming
    Replies: 5
    Last Post: 03-19-2011, 02:41 AM
  5. printf not printing
    By deadhippo in forum C Programming
    Replies: 5
    Last Post: 05-04-2008, 12:36 AM

Tags for this Thread