Click here to Skip to main content
15,910,981 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionRe: common id in GUI Pin
Rajkumar R21-Feb-08 2:40
Rajkumar R21-Feb-08 2:40 
QuestionStuck up with NULL character Pin
abhijitr21-Feb-08 1:26
abhijitr21-Feb-08 1:26 
GeneralRe: Stuck up with NULL character Pin
CPallini21-Feb-08 2:16
mveCPallini21-Feb-08 2:16 
GeneralRe: Stuck up with NULL character Pin
David Crow21-Feb-08 3:40
David Crow21-Feb-08 3:40 
GeneralRe: Stuck up with NULL character Pin
Member 75496021-Feb-08 4:54
Member 75496021-Feb-08 4:54 
GeneralRe: Stuck up with NULL character Pin
David Crow21-Feb-08 6:36
David Crow21-Feb-08 6:36 
GeneralRe: Stuck up with NULL character Pin
Member 75496021-Feb-08 7:34
Member 75496021-Feb-08 7:34 
GeneralRe: Stuck up with NULL character Pin
James R. Twine21-Feb-08 8:29
James R. Twine21-Feb-08 8:29 
   Your code example may confuse the OP... Your second if() test checks the length of the passed string against the length of the str buffer, instead of "length of the str buffer - 1", so the strcpy(...) executed if the if() passes will overwrite one past the end of the str buffer when it writes a terminating NUL character.

   The final else if() test copies the source string correctly, but the line after that places the terminating NUL is using a hardcoded index of 1, so if str is longer than 2 characters, you will copy a truncated version of the source string and then truncate it further to one character.

   Use of sizeof() to determine the character length of the buffer only works for (1) arrays that are part of the object, or automatically/locally allocated as arrays (i.e. if str is actually a pointer to an array and not a real array, it will not work as expected); and (2) only in ANSI builds - for UNICODE, the calculations will be off.

   The copying of the source string can be further simplified to:
    dwStrBufferLen = /* character length of the <code>str</code> buffer */
    ::strncpy( str, Astr, dwStrBufferLen );
    str[ dwStrBufferLen - 1 ] = '\0';
   This will work regardless of the length of the target string, truncating it if necessary, and will ensure that a terminating NUL is always there.

   Since ::strncpy(...) will not go past the length specified, it is one less place to be concerned with doing a correct off-by-one offset.  If it runs out of space before the end of the source string, the following line will write the terminating NUL.

  Peace!

-=- James
Please rate this message - let me know if I helped or not!<hr></hr>If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
See DeleteFXPFiles



GeneralRe: Stuck up with NULL character Pin
Member 75496021-Feb-08 8:53
Member 75496021-Feb-08 8:53 
GeneralRe: Stuck up with NULL character Pin
Ali Rafiee21-Feb-08 9:53
Ali Rafiee21-Feb-08 9:53 
GeneralRe: Stuck up with NULL character Pin
abhijitr21-Feb-08 23:12
abhijitr21-Feb-08 23:12 
AnswerRe: Stuck up with NULL character Pin
abhijitr22-Feb-08 3:30
abhijitr22-Feb-08 3:30 
GeneralRe: Stuck up with NULL character Pin
Ali Rafiee22-Feb-08 5:10
Ali Rafiee22-Feb-08 5:10 
GeneralRe: Stuck up with NULL character Pin
abhijitr27-Feb-08 21:35
abhijitr27-Feb-08 21:35 
GeneralAbout polymorphism.. Pin
rowdy_vc++20-Feb-08 23:22
rowdy_vc++20-Feb-08 23:22 
GeneralRe: About polymorphism.. [modified] PinPopular
CPallini20-Feb-08 23:47
mveCPallini20-Feb-08 23:47 
GeneralRe: About polymorphism.. Pin
Rajkumar R21-Feb-08 0:03
Rajkumar R21-Feb-08 0:03 
GeneralRe: About polymorphism.. Pin
rowdy_vc++21-Feb-08 0:47
rowdy_vc++21-Feb-08 0:47 
GeneralRe: About polymorphism.. Pin
Rajkumar R21-Feb-08 1:02
Rajkumar R21-Feb-08 1:02 
GeneralRe: About polymorphism.. Pin
David Crow21-Feb-08 4:03
David Crow21-Feb-08 4:03 
Questionconvert color image to grayscale image ? Pin
gentleguy20-Feb-08 21:29
gentleguy20-Feb-08 21:29 
AnswerRe: convert color image to grayscale image ? Pin
Iain Clarke, Warrior Programmer20-Feb-08 22:06
Iain Clarke, Warrior Programmer20-Feb-08 22:06 
AnswerRe: convert color image to grayscale image ? Pin
CPallini20-Feb-08 22:16
mveCPallini20-Feb-08 22:16 
GeneralRe: convert color image to grayscale image ? Pin
Rajkumar R20-Feb-08 22:55
Rajkumar R20-Feb-08 22:55 
GeneralRe: convert color image to grayscale image ? Pin
CPallini20-Feb-08 23:22
mveCPallini20-Feb-08 23:22 

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.