Find the answer to your Linux question:
Results 1 to 2 of 2
I'm into ncurses programming again, this time writing my own hex editor. I have this group of functions that draw the screen, top title bar, bottom status bar, and the ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux User
    Join Date
    Dec 2011
    Location
    Turtle Island West
    Posts
    458

    vsnprintf behaving oddly/non-static var affects other vars...


    I'm into ncurses programming again, this time writing my own hex editor. I have this group of functions that draw the screen, top title bar, bottom status bar, and the main hexwin. Everything was good until a local var, q, in another function stopped indexing properly for no reason. I started playing around, turning other variables static, until I got to this one. As soon as I made foo static, q went back to indexing properly. It was most bizarre.
    Code:
    #define MAXLEN 255
    void mv_printf ( int y, int x, char *fmt, ... ) {
        va_list ap;
        // If this is not static, it seems to gibble variables in other functions
        static char foo[MAXLEN+1];
        int i = 0;
    
        memset ( foo, 0, MAXLEN+1 );
        va_start(ap, fmt);
        // and this i occasionally doesn't equal strlen(foo)
        i += vsnprintf(foo, MAXLEN, fmt, ap);
        va_end(ap);
    
        // I dunno. Seems to like this. Sometimes we get a funny strlen
        // and no trailing NULL ?!?
        for ( i = MAXLEN; foo[i] <= ' '; foo[i--] = 0 );
    
        mvaddstr ( y, x, foo );
    }
    Occasionally, the resulting string in foo didn't have a trailing NULL also until I added that for() loop at he end.

    Any notion why non-static foo would gibble other local vars?

    Is there some compiler/preprocessor directive that controls this kind of thing?

  2. #2
    Linux User
    Join Date
    Dec 2011
    Location
    Turtle Island West
    Posts
    458
    After debugging for many hours thinking it was some strange error on my part, I realized that my PC was slowly crashing. I must have corrupted something in my hacking. Shortly after I posted, bizarre errors started showing up in other programs, and then my window manager started going wonky. Once I closed everything including a few heavy apps and freed up some major memory, things appeared to go back to normal, but I rebooted just to be on the safe side.

    Sometimes my hacking can get a bit heavy handed I guess. Occasionally I try some brute force technique just to see what happens, so I can understand a process better, thinking my 2.6.6 kernel is superman or something.

    I thought it was one of those 'if ( i = 6 )' instead of 'if ( i == 6 )' mistakes or something.

    Anyway, after reboot, the problems went away.

    I gotta be more careful mixing heavy mallocs, intensive fgetc's and major memory moves with SIGSEGV handlers.

    Live and learn. I'll call this [SOLVED].

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •