-2

I have a dynamic char array that was deserialized from a stream.

Content of char *myarray on the file (with a hexal editor) :

      4F 4B 20 31 32 20 0D 0A 00 00 B4 7F

strlen(myarray) returns 8, (must be 12)

Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
  • 1
    Can you show the code that deserializes the data from the stream? – huysentruitw Aug 06 '12 at 11:41
  • 12
    `strlen` returns the size of a string, but your `myarray` does not hold a string, so `strlen` is useless here. –  Aug 06 '12 at 11:41
  • I know the size. In a function, I copied "myarray" to a referance parameter "char *copymyarray". But "copymyarray" has 8_bytes instead of 12_bytes. – ismylhakkituran Aug 06 '12 at 12:22
  • 2
    How did you copy? `strcpy` also stops at the first 0 byte. That would be inadequate here. `memcpy` copies how many bytes you tell it to copy. How do you determine the size of `copymyarray`? If you're using `sizeof`, that will tell you the size of a `char*`, regardless of how much was allocated or what the contents are. – Daniel Fischer Aug 06 '12 at 12:41
  • A related question [How to find the sizeof(a pointer pointing to an array)](http://stackoverflow.com/questions/492384/how-to-find-the-sizeofa-pointer-pointing-to-an-array) – Bo Persson Aug 06 '12 at 12:51

6 Answers6

10

strlen counts the characters up to the first 0 character, that's what it's for.

If you want to know the length of the deserialized array, you must get that from somewhere else, the deserialization code should know how large an array it deserialized.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
5

strlen(myarray) returns the index of the first 00 in myarray.

updogliu
  • 6,066
  • 7
  • 37
  • 50
2

Which language are you asking about?

In C, you'll need to remember the size and pass it to anything that needs to know it. There's no (portable) way to determine the size of an allocated array given just a pointer to it and, as you say, strlen and other functions that work with zero-terminated strings won't work with unterminated lumps of data.

In C++, use std::string or std::vector<char> to manage a dynamic array of bytes. Both of these make the size available, as well as handling deallocation for you.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
0

9th char is 00. i.e '\0'. This is the reason you are getting 8 instead of 12.
strlen() takes it as Null terminator.

Jeyaram
  • 9,158
  • 7
  • 41
  • 63
0

A C-style String is terminated by NULL, and your char* contains a NULL-Byte at the 9th position, thus strlen returns 8, as it counts the elements until it finds a NULL byte.

(from http://www.cplusplus.com/reference/clibrary/cstring/strlen/):

A C string is as long as the amount of characters between the beginning of the string and the terminating null character.

As you're using the char* for binary data, you must not use the strlen function, but remember (pass along) the size of the char array.

In your case, you could serialize the size of the dynamic array on transmission, and deserialize it before allocating / reading the array.

oliverguenther
  • 1,167
  • 1
  • 17
  • 31
0

From cplusplus.com (http://www.cplusplus.com/reference/clibrary/cstring/strlen/):

The length of a C string is determined by the terminating null-character

You can not expect that it would count the whole string if you have '\0' in the middle of it.

It such scenarios I have found it to be best to serialize the length of the message alongside the data in the stream. For example, you first serialize the length of the char array - 12 and then you serialize the actual data (characters). That way when you read the data you would first read the length and then you can read that much characters and be sure that is your char array.

Lyubomir Vasilev
  • 3,000
  • 17
  • 24