CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2002
    Location
    Germany
    Posts
    451

    Performance question - char*, vector or similar

    Hi,

    I need to manipulate unsigned char data. The size of data is unknown by the function which I am about to implement (it comes as an argument).

    Is there any speed difference in if it is
    1. Allocated as char* using new
    2. A std::vector is used
    3. Microsoft specific implementation is used?

    Which would you recommend and why?

    Thanks

  2. #2
    Join Date
    Aug 1999
    Location
    <Classified>
    Posts
    6,882

    Re: Performance question - char*, vector or similar

    Vectors are not suitable for passing strings. Using a NULL terminated strings (char* pointer memory allocated by new or malloc, no probs) are best when you want to pass the argument to function. This way only pointer value is copied to argument variable, and not data.
    I assume you mean Win32 API by saying Microsoft specific implementation, I would say that using CRT functions (like new, alloc) and Win32 (HealAlloc) for NULL terminated strings will have almost same performance.
    Regards,
    Ramkrishna Pawar

  3. #3
    Join Date
    May 2002
    Location
    Germany
    Posts
    451

    Re: Performance question - char*, vector or similar

    Heya,

    I am not about to manipulate string in the sense like you explained. My data can contain many 0s, so a data stream like 0x00 0x81 0x62 0x00 0x05 should be 5 bytes valid data.

    Considering that way, what would be the best bet?

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Performance question - char*, vector or similar

    Quote Originally Posted by Krishnaa
    Vectors are not suitable for passing strings.
    Why not?
    Using a NULL terminated strings (char* pointer memory allocated by new or malloc, no probs)
    First, the OP never mentioned anything about NULL-terminated strings, just char data.

    But more to the point, a pointer to the first element in a vector<char> is a char *.
    Code:
    #include <vector>
    
    void foo(char *p)
    {
      // do something with p
    }
    
    int main()
    {
       int n = 10;
       // n has some value
       std::vector<char> p(10);
       foo(&p[0]);  // this works
    }
    The &p[0] is a char *, so there is no difference whether the data comes from a vector, a char* that you allocated data for using "new", or a straight plain old char array, you end up with the same thing in the foo() function.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; June 12th, 2006 at 09:38 AM.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Performance question - char*, vector or similar

    Quote Originally Posted by luftwaffe
    Heya,

    I am not about to manipulate string in the sense like you explained. My data can contain many 0s, so a data stream like 0x00 0x81 0x62 0x00 0x05 should be 5 bytes valid data.

    Considering that way, what would be the best bet?
    The vector<char> is a solution, a std::string is a solution. Asking for "which is best" -- that's for you to decide and test. That's like asking "what is the best car that gets 30 mpg or better".

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Aug 1999
    Location
    <Classified>
    Posts
    6,882

    Re: Performance question - char*, vector or similar

    Quote Originally Posted by Paul McKenzie
    Why not?
    First, the OP never mentioned anything about NULL-terminated strings, just char data.
    Sure vecror can be used for byte data, I though he might be talking about strings.

    But more to the point, what is the difference between a pointer to the first element in a vector<char> is a char *.
    Code:
    #include <vector>
    
    void foo(char *p)
    {
      // do something with p
    }
    
    int main()
    {
       int n = 10;
       // n has some value
       std::vector<char> p(10);
       foo(&p[0]);  // this works
    }
    The &p[0] is a char *, so there is no difference whether the data comes from a vector, a char* that you allocated data for using "new", or a straight plain old char array, you end up with the same thing in the foo() function.

    Regards,

    Paul McKenzie
    Regards,
    Ramkrishna Pawar

  7. #7
    Join Date
    May 2002
    Location
    Germany
    Posts
    451

    Re: Performance question - char*, vector or similar

    Thank you very much for the detailed explanation!

  8. #8
    Join Date
    Nov 2001
    Location
    Gothenburg
    Posts
    56

    Re: Performance question - char*, vector or similar

    I have used vector<CString> to store different strings. I think the stl container has a lot of advantages when you compare to a char array or similar.
    Asafor

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