View previous topic :: View next topic |
Author |
Message |
LoTeK Apprentice
Joined: 26 Jul 2012 Posts: 270
|
Posted: Fri Feb 01, 2013 10:20 am Post subject: C: difference between snprintf and smprintf? |
|
|
hi,
In the helper functions of dwm http://dwm.suckless.org/dwmstatus/ there is used a function called: smprintf But google returns way more information on snprintf and less to nothing about smprintf.
If I'm playing around with those helper functions I always get the error:
Code: | undefined reference to smprintf |
If I just replace all smprintfs with snprintfs I'll get:
Code: | to few arguments to function snprintf |
I'm reading the very good book "Topics in C programming" at the moment, but although many functions are discussed, smprintf isn't mentioned. _________________ "I want to see gamma rays! I want to hear X-rays! Do you see the absurdity of what I am? I can't even express these things properly because I have to conceptualize complex ideas in this stupid limiting spoken language!" |
|
Back to top |
|
|
Genone Retired Dev
Joined: 14 Mar 2003 Posts: 9527 Location: beyond the rim
|
Posted: Fri Feb 01, 2013 10:24 am Post subject: |
|
|
Never heard about smprintf, so I assume it's simply a helper function those guys wrote themselves.
EDIT: Google listed https://github.com/apgwoz/dwm/blob/master/dwmstatus.c as a result for smprintf, which defines smprintf simply as a version of snprintf that determines the output string length and allocates the output buffer itself. Convenient, but lots of potential for memory leaks. |
|
Back to top |
|
|
Hu Moderator
Joined: 06 Mar 2007 Posts: 21631
|
Posted: Sat Feb 02, 2013 1:30 am Post subject: |
|
|
I concur with regard to memory leaks. If you can assume the use of _GNU_SOURCE, then glibc provides asprintf, with similar functionality, but a prototype that is slightly less prone to leaks. Otherwise, stick with snprintf. In most cases, if you cannot predict the size of the output buffer, allowing an automatically sized buffer is a bad choice. |
|
Back to top |
|
|
|