Click here to Skip to main content
14,643,058 members
Home » Discussions » ATL / WTL / STL
   

ATL / WTL / STL

 
GeneralRe: better version of strcpy() and printf() Pin
bkelly1315-Sep-12 15:34
Memberbkelly1315-Sep-12 15:34 
GeneralRe: better version of strcpy() and printf() Pin
Paul M Watt15-Sep-12 15:45
mentorPaul M Watt15-Sep-12 15:45 
GeneralRe: better version of strcpy() and printf() Pin
bkelly1315-Sep-12 16:23
Memberbkelly1315-Sep-12 16:23 
GeneralRe: better version of strcpy() and printf() Pin
xenotron16-Sep-12 6:54
Memberxenotron16-Sep-12 6:54 
GeneralRe: better version of strcpy() and printf() Pin
Richard MacCutchan16-Sep-12 0:10
mveRichard MacCutchan16-Sep-12 0:10 
AnswerRe: better version of strcpy() and printf() Pin
Richard Andrew x6415-Sep-12 17:01
professionalRichard Andrew x6415-Sep-12 17:01 
AnswerRe: better version of strcpy() and printf() Pin
Richard MacCutchan16-Sep-12 0:09
mveRichard MacCutchan16-Sep-12 0:09 
AnswerRe: better version of strcpy() and printf() Pin
xenotron16-Sep-12 1:37
Memberxenotron16-Sep-12 1:37 
Here is my opinion: strcpy is totally useless and totally unsafe. Use a string class in C++ and assign one string to another instead of strcpy. You can even implement refcounting if you write your own string class and then assignment will be super fast. You can of course use std::string for your needs.

If you write you own string class or if you extend your own string class from std::string then you can put in a format and vformat function (similarly to the MFC CString class) and you can do formatting so that the buffer size will be managed automatically for you. Here is an example to that: How to Format a String[^]. I would mention only one thing regarding this formatting function in the link: The use of the std::vector is unnecessary, you could put the string formatting directly to the string. even in that case the string resizing unnecessarily fills the whole string with a fill character before formatting the string into its buffer. With a custom string you can write more optimal code for this task.

The type safety of format parameters with the vararg nature of format functions is still endangering your program. Using the bad parameter types make your program crash at runtime when the formatting code gets executed. Example to a buggy formatting statement: format("Name: %s\n", 5). Gcc has an __attribute__ format printf to defend against such mistakes at compile time. You can mark you format function with this attribute and then gcc will catch such errors, it would find out for exmple that "%s" and 5 are not compatible. Unfortunately visual C++ doesn't have such feature but our current project is crossplatform so we compile it with both VC++ and gcc and this gcc feature is priceless.

Unfortunately this kind of formatting is too widespread and comfortable without similarly comfortable alternatives so this wont be dropped easily from C++. However a feature like the one gcc has can make this stuff quite usable and safe.
AnswerRe: better version of strcpy() and printf() Pin
Stephen Hewitt18-Sep-12 0:29
MemberStephen Hewitt18-Sep-12 0:29 
Questionget a number into static text control Pin
bkelly1313-Sep-12 17:05
Memberbkelly1313-Sep-12 17:05 
AnswerRe: get a number into static text control Pin
Richard MacCutchan13-Sep-12 21:52
mveRichard MacCutchan13-Sep-12 21:52 
GeneralRe: get a number into static text control Pin
bkelly1314-Sep-12 15:00
Memberbkelly1314-Sep-12 15:00 
GeneralRe: get a number into static text control Pin
Paul M Watt14-Sep-12 15:36
mentorPaul M Watt14-Sep-12 15:36 
GeneralRe: get a number into static text control Pin
Richard MacCutchan14-Sep-12 23:02
mveRichard MacCutchan14-Sep-12 23:02 
GeneralRe: get a number into static text control Pin
bkelly1315-Sep-12 5:14
Memberbkelly1315-Sep-12 5:14 
GeneralRe: get a number into static text control Pin
Richard MacCutchan15-Sep-12 5:42
mveRichard MacCutchan15-Sep-12 5:42 
QuestionMirroring system imagelist Pin
Huzifa Terkawi11-Sep-12 15:27
MemberHuzifa Terkawi11-Sep-12 15:27 
QuestionCAsyncSocket, first application Pin
bkelly139-Sep-12 5:26
Memberbkelly139-Sep-12 5:26 
AnswerRe: CAsyncSocket, first application Pin
Paul M Watt14-Sep-12 15:47
mentorPaul M Watt14-Sep-12 15:47 
QuestionAfxGetThread vs GetCurrentThreadID Pin
kishorekumarnaik6-Sep-12 6:52
Memberkishorekumarnaik6-Sep-12 6:52 
AnswerRe: AfxGetThread vs GetCurrentThreadID Pin
Jiří Miklík7-Sep-12 0:24
MemberJiří Miklík7-Sep-12 0:24 
Questionerror C2039: 'abort' : is not a member of 'std' Pin
KASR12-Sep-12 23:35
MemberKASR12-Sep-12 23:35 
AnswerRe: error C2039: 'abort' : is not a member of 'std' Pin
Richard MacCutchan3-Sep-12 0:50
mveRichard MacCutchan3-Sep-12 0:50 
GeneralRe: error C2039: 'abort' : is not a member of 'std' Pin
KASR13-Sep-12 1:44
MemberKASR13-Sep-12 1:44 
GeneralRe: error C2039: 'abort' : is not a member of 'std' Pin
Richard MacCutchan3-Sep-12 2:21
mveRichard MacCutchan3-Sep-12 2:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.