Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
C: difference between snprintf and smprintf?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
LoTeK
Apprentice
Apprentice


Joined: 26 Jul 2012
Posts: 270

PostPosted: Fri Feb 01, 2013 10:20 am    Post subject: C: difference between snprintf and smprintf? Reply with quote

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
View user's profile Send private message
Genone
Retired Dev
Retired Dev


Joined: 14 Mar 2003
Posts: 9522
Location: beyond the rim

PostPosted: Fri Feb 01, 2013 10:24 am    Post subject: Reply with quote

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
View user's profile Send private message
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 21595

PostPosted: Sat Feb 02, 2013 1:30 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum