CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 29
  1. #1
    Join Date
    Jun 2009
    Posts
    118

    Question how to detect NULL bytes in a char array ?

    Hi All,

    I want to detect if any NULL bytes exist in a char array.

    Consider the following code snippet:

    Code:
    char buffer[1000000];
    ifstream fstr("myfile", ios::binary);
    fstr.read(buffer, 1000000);
    //check if buffer has any NULL characters
    for(int i = 0; i < 1000000; i++)
    {
         if(!buffer[i]) detected = true;
    }
    Assuming that fstr has 1000000 bytes, constructing a for loop like above seems inefficient (in terms of speed) to me to detect NULL characters.

    Buffer length could be much higher.

    Which methodology do you suggest to detect NULL characters in the fastest way in a char array ?

    Thanks.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: how to detect NULL bytes in a char array ?

    Just use strlen!
    Victor Nijegorodov

  3. #3
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: how to detect NULL bytes in a char array ?

    I'm not sure that you've got much choice other than to iterate through the chars. Even if you were to have an array of the appropriate length and treated it as an array of int or long you would only end up having to compare each one against four masks.

    Better than using an explicit loop, use something like the following.

    Code:
    #include <algorithm>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
        char     buffer[1000000];
        ifstream fstr("myfile", ios::binary);
    
        fstr.read(buffer, 1000000);
    
        const char *BEGIN = buffer;
        const char *END = buffer + 1000000;
    
        bool detected = (find(BEGIN, END, 0) != END);
    }
    You can adapt it for finding any value.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: how to detect NULL bytes in a char array ?

    Again:
    Code:
        char     buffer[1000000];
        ....
        bool detected = strlen(buffer) < 1000000;
    Victor Nijegorodov

  5. #5
    Join Date
    Jun 2009
    Posts
    118

    Re: how to detect NULL bytes in a char array ?

    Quote Originally Posted by VictorN View Post
    Again:
    Code:
        char     buffer[1000000];
        ....
        bool detected = strlen(buffer) < 1000000;
    What if I want to count for the number of NULL characters in a char array ?

    Given the following value for buffer with no terminating null character:

    Code:
    buffer[0] = 'a'
    buffer[1] =  0
    buffer[2] = 0
    buffer[3] = 'a'
    How can I calculate the number of NULL characters (e.g., 2 in above example) without iterating through the array members ?

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: how to detect NULL bytes in a char array ?

    Quote Originally Posted by aryan1 View Post
    How can I calculate the number of NULL characters (e.g., 2 in above example) without iterating through the array members ?
    Is this case you have to iterate...
    Victor Nijegorodov

  7. #7
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: how to detect NULL bytes in a char array ?

    1) your original loop is OK, except you should break out once a NULL
    is detected.

    2) I would use std::find

    3) strlen assumes that there is a NULL ... if there is not a NULL in
    the buffer, it would continue checking untill it finds one.

    4) I would expect that the read takes up more time than the
    checking for NULL. You might want to break the read up into
    blocks (512 or 1024) and check each block after you read it.
    If a NULL is found early, it would save having to do a lot of reads.

  8. #8
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: how to detect NULL bytes in a char array ?

    If you want to count all the NULLs in the array, use std::count ...

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: how to detect NULL bytes in a char array ?

    Quote Originally Posted by Philip Nicoletti View Post
    3) strlen assumes that there is a NULL ... if there is not a NULL in
    the buffer, it would continue checking untill it finds one.
    Good point, Philip!
    I missed the fact that buffer might be full filled with data without terminated NULL!
    Victor Nijegorodov

  10. #10
    Join Date
    Jun 2009
    Posts
    118

    Re: how to detect NULL bytes in a char array ?

    Quote Originally Posted by Philip Nicoletti View Post
    If you want to count all the NULLs in the array, use std::count ...
    So the following code snippet will do the job:

    Code:
    const char *BEGIN = buffer;
    const char *END = buffer + 1000000;
    
    size_t numberOfNullChars = count(BEGIN, END, 0);

  11. #11
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: how to detect NULL bytes in a char array ?

    If you were to use std::vector then you can do this.

    Code:
    #include <algorithm>
    #include <fstream>
    #include <vector>
    
    using namespace std;
    
    int main()
    {
        vector<char> buffer(1000000);
    
        ifstream fstr("myfile", ios::binary);
    
        fstr.read(&buffer[0], buffer.size());
    
        vector<char>::size_type null_count = count(buffer.begin(), buffer.end(), 0);
    }
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  12. #12
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: how to detect NULL bytes in a char array ?

    Also, note ... if you are only interested in the number of NULLs
    in the file and are looking at the entire file, you do not need a buffer:

    Code:
    #include <algorithm>
    #include <fstream>
    #include <iterator>
    
    using namespace std;
    
    int main()
    {
        ifstream fstr("myfile", ios::binary);
    
        size_t null_count = count(istreambuf_iterator<char>(fstr), istreambuf_iterator<char>(), 0);
    }

  13. #13
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: how to detect NULL bytes in a char array ?

    Isn't the STL wonderful!
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  14. #14
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: how to detect NULL bytes in a char array ?

    It's also probably worthy of note that every one of the posted solutions except for Philip's latest solution have the same horrible bug. That is that the number of bytes read from the file may be far less than the number of bytes in the buffer, yet the check for null characters covers the whole buffer. So what about the values found in the remainder of the buffer? The data may have no null characters but the remainder of the buffer might, thus an incorrect result may be produced.

  15. #15
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: how to detect NULL bytes in a char array ?

    Point taken, but I worked on the assumption that this is a very simplified example to demonstrate the algorithm for finding nulls, not robust file handling.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured