• AVR Freaks

Hot!ChaN's xprintf() and arithmetic on pointer to void yields Undefined Behavior

Author
AndersG
Senior Member
  • Total Posts : 174
  • Reward points : 0
  • Joined: 2008/08/05 04:51:24
  • Location: 0
  • Status: offline
2019/08/17 07:17:06 (permalink)
0

ChaN's xprintf() and arithmetic on pointer to void yields Undefined Behavior

I find it quite useful and have used it for several projects. Especially when you just want to print formatted to a serial port. With the latest compilers however are there 7 identical warnings. The complete code is here: http://www.elm-chan.org/fsw/strf/xprintf.html
 
 if (c == '*') { /* Minimum width from an argument */
w = va_arg(arp, int);
c = *fmt++;
} else {
while (c >= '0' && c <= '9') { /* Minimum width */
w = w * 10 + c - '0';
c = *fmt++;
}
}
if (c == 'l' || c == 'L') { /* Prefix: Size is long */
f |= 4; c = *fmt++;

 
It is the w = va_arg() that triggers the warning. Code works OK though, but I find it annoying. Anyone have any ideas?
#1

7 Replies Related Threads

    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3233
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: ChaN's xprintf() and arithmetic on pointer to void yields Undefined Behavior 2019/08/17 10:46:47 (permalink)
    0
    Include this header file: "stdarg.h"
     
    Show me yours and I'll show you mine.
    (I'll look through that source code if you look through my source code)

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #2
    davekw7x
    Entropy++
    • Total Posts : 1810
    • Reward points : 0
    • Joined: 2012/01/16 12:01:07
    • Location: Second star on the right, straight on till morning
    • Status: offline
    Re: ChaN's xprintf() and arithmetic on pointer to void yields Undefined Behavior 2019/08/17 19:01:04 (permalink)
    0
    AndersG
    ...Code works OK though, but I find it annoying....

     
    Me, too
     
    Over the years I have found that various irrelevant warnings from various versions of XC8 have been annoying.
    For xprintf.c, XC8 version 1.45 gives me warnings like   (xprintf.c:151: warning: (1496) arithmetic on pointer to void yields Undefined Behavior) but later XC8 versions (2.05, 2.10) do not. [Edit]---Yes they do!---[/Edit]
    I played with xprintf (required a little minor surgery to avoid recursion), but didn't find any particular advantage. 
     
     
    Regards,

    Dave
    post edited by davekw7x - 2019/08/17 19:15:20

    Sometimes I just can't help myself...
    #3
    NKurzman
    A Guy on the Net
    • Total Posts : 17709
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re: ChaN's xprintf() and arithmetic on pointer to void yields Undefined Behavior 2019/08/17 21:25:08 (permalink)
    +1 (1)
    GCC is very loose on the standards. But since it is common people code to it. It appears this library is depending on undefined behavior. That means it will not work on every compiler. The issue is not with the compiler, it is with the library.
    As far a recursion, well That is the price you pay be able to code in C on a pic16 or 18.
    #4
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11285
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: ChaN's xprintf() and arithmetic on pointer to void yields Undefined Behavior 2019/08/17 22:48:31 (permalink)
    0
    xprintf.c:151: warning: (1496) arithmetic on pointer to void yields Undefined Behavior

     
    Line 151 doesn't contain any pointers?  And I can see any arithmetic on void pointers in that code.
     
    didn't find any particular advantage

     
    I wouldn't expect it to be able to compete well with XC8's on-the-fly printf implementation.
    #5
    AndersG
    Senior Member
    • Total Posts : 174
    • Reward points : 0
    • Joined: 2008/08/05 04:51:24
    • Location: 0
    • Status: offline
    Re: ChaN's xprintf() and arithmetic on pointer to void yields Undefined Behavior 2019/08/18 06:18:16 (permalink)
    0
    I wouldn't expect it to be able to compete well with XC8's on-the-fly printf implementation.
     
    Can you elaborate? Can I redirect the built-in printf() to a serial port? Ah.. Apparently I can. Silly me! It is even in the manual... 
     
    3.5.8 How Do I Use Printf to Send Text to a Peripheral?
    #6
    ric
    Super Member
    • Total Posts : 23545
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: ChaN's xprintf() and arithmetic on pointer to void yields Undefined Behavior 2019/08/18 06:28:50 (permalink)
    +3 (3)
    XC8 evaluates your code at compile time and decides how much printf functionality needs to be included. E.g. if you never use %f or a variable format string that might contain one, then it omits all the floating point support, saving heaps of code space.

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #7
    AndersG
    Senior Member
    • Total Posts : 174
    • Reward points : 0
    • Joined: 2008/08/05 04:51:24
    • Location: 0
    • Status: offline
    Re: ChaN's xprintf() and arithmetic on pointer to void yields Undefined Behavior 2019/08/18 07:41:55 (permalink)
    0
    OK. Replacing xprintf() with printf() resulted in larger code though. Not that it matters in this case. Not using floating point.
    #8
    Jump to:
    © 2019 APG vNext Commercial Version 4.5