C++ Learning Community Forum
August 01, 2010, 02:50:45 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Hello. Smiley
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Confused with C string weird behaviour...  (Read 605 times)
syazhani
I wonder how long can titles be... because the longer it is the more attention I can get!
Dr. of C++ology
****
Posts: 529


Cats > Dogs


View Profile
« on: March 14, 2009, 06:26:55 AM »

Here is my little simple function before fixing:

Code
GeSHi (c):
void charStrToHexStr(char* t, int size, char* hexStrEncrypted)
{
   int i; //Index of input
 
   char c[2]; //Temporary to hold hex value
 
   hexStrEncrypted[0] = '\0';
   for(i = 0; i < size; i++)  //Iterate through input
   {
       sprintf(c, "%02X", t[i]&0xFF); //Convert input into hex and put into c[2].
                                   //1 char will have 2 hex digit max so c[0] holds higher order
                                   //hex (0xF0) and c[1] holds lower order hex (0x0F)
       strcat(hexStrEncrypted, c); //Append c to hexStrEncrypted
   }
   hexStrEncrypted[i*2+1] = '\0';  //Terminate output with null
 
}
 
Created by GeSHI 1.0.7.18

The purpose is just to convert a char into it's hex equivalent but as a string. So char 'a' will be converted into string "41".

The problem is if input string t is longer than 1, I get into infinite loop with counter i gets reset to 0 on every iteration. What a weird behaviour!

The problem is fixed buy changing size of char c[] to 3 instead of 2. I can't think WHY. c[2] should be enough to hold the hex digits which will be at most 2 per char. c[0] = first hex, c[1] = second hex, c[2] = null.

Why? Huh
Logged

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."- Brian W. Kernighan

What people misunderstood about Islam presented in comical way: http://ummahfilms.com
C-Man
Does anyone even read this ?
Global Moderator
Dr. of C++ology
*****
Posts: 988



View Profile WWW
« Reply #1 on: March 14, 2009, 09:18:40 AM »

well you are forgetting that a C string isn't just the chars but also the null terminator
what happens is your sprintf writes the two digits then the 3rd null terminator goes out ofrange and overwrites the counter which is right next to c in memory

the C syntax for arrays

type array[number_of_elements_it_contains]

so c[N] will have N chars and the indexes will be valid only 0 trough N-1 not 0 trough N
Logged

syazhani
I wonder how long can titles be... because the longer it is the more attention I can get!
Dr. of C++ology
****
Posts: 529


Cats > Dogs


View Profile
« Reply #2 on: March 14, 2009, 12:22:18 PM »

I see. All these time I thought the valid index is up to N, but is reserved for the null character.
Logged

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."- Brian W. Kernighan

What people misunderstood about Islam presented in comical way: http://ummahfilms.com
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!