c++ - Sizeof and Strlen -



c++ - Sizeof and Strlen -

i trying implement encryption using salt , password. , since recommended size salt 64 bits, declared.

char salt[8];

i used rand_pseudo_bytes random salt way:

rand_pseudo_bytes((unsigned char*)salt, sizeof salt);

and because hexdump output different in length(sometimes 5, 24 bytes) each time compiled because wrongly used strlen instread of sizeof:

rand_pseudo_bytes((unsigned char*)salt, strlen(salt));

i tried next line figure out what's happening:

printf("\n%d\n",strlen(salt));

which outputs 24 each time.

so, question is: why strlen(salt)=24 when declared salt's length 8(sizeof(salt)=8)? understand 9(with '\0', although not exclusively sure how happen), 24 strikes me odd. give thanks you.

strlen going walk downwards pointer gave , count number of bytes until reaches null byte. in case, char array of 8 bytes has no null bytes, strlen happily continues past boundary part of memory beyond defined char array on stack, , whatever happens there determine behaviour of strlen. in case, 24 bytes past origin of array, there null byte.

c++ sizeof strlen

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -