Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualTheChubu

Posted 11 March 2013 - 08:30 PM

Care to elaborate? biggrin.png

 

I purposely stayed away from C's formatted output for the brief time I was learning C++.

 

You can overflow the buffer at any time, you don't know how long it is, you need to walk over the entire string in order to do any operation (which can easily lead to O(n2) algorithms) - strings in C basically contain everything that one should not do if one was going to design a string library.  See http://en.wikipedia.org/wiki/C_string_handling#Criticism and http://www.joelonsoftware.com/articles/fog0000000319.html for more.

 

 

Care to elaborate? biggrin.png

I purposely stayed away from C's formatted output for the brief time I was learning C++.

Each %s in the string means there should be another parameter that is a pointer to a string. The line should look like:

sprintf(str, "%s %s %s", a, b, c);
Because the function is expecting another parameter on the stack to go with the third %s, it will use whatever is in the next memory location after the b. This could be anything!

 

 

Care to elaborate? biggrin.png

 

I purposely stayed away from C's formatted output for the brief time I was learning C++.

 

C doesn't have strings. It has arrays of characters, and some fancy goggles for the programmer which make those arrays look and act a bit like strings if you're very careful.

Oh I see then it might trash the memory, thanks!


#1TheChubu

Posted 11 March 2013 - 08:28 PM

Care to elaborate? biggrin.png

 

I purposely stayed away from C's formatted output for the brief time I was learning C++.

 

You can overflow the buffer at any time, you don't know how long it is, you need to walk over the entire string in order to do any operation (which can easily lead to O(n2) algorithms) - strings in C basically contain everything that one should not do if one was going to design a string library.  See http://en.wikipedia.org/wiki/C_string_handling#Criticism and http://www.joelonsoftware.com/articles/fog0000000319.html for more.

 

 

Care to elaborate? biggrin.png

I purposely stayed away from C's formatted output for the brief time I was learning C++.

Each %s in the string means there should be another parameter that is a pointer to a string. The line should look like:

sprintf(str, "%s %s %s", a, b, c);
Because the function is expecting another parameter on the stack to go with the third %s, it will use whatever is in the next memory location after the b. This could be anything!

 

 

Care to elaborate? biggrin.png

 

I purposely stayed away from C's formatted output for the brief time I was learning C++.

 

C doesn't have strings. It has arrays of characters, and some fancy goggles for the programmer which make those arrays look and act a bit like strings if you're very careful.

Oh I see then its a pointer to nowhere, thanks!


PARTNERS