-
June 12th, 2006, 05:34 AM
#1
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
-
June 12th, 2006, 05:42 AM
#2
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
-
June 12th, 2006, 05:51 AM
#3
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?
-
June 12th, 2006, 05:55 AM
#4
Re: Performance question - char*, vector or similar
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.
-
June 12th, 2006, 05:58 AM
#5
Re: Performance question - char*, vector or similar
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
-
June 12th, 2006, 06:11 AM
#6
Re: Performance question - char*, vector or similar
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
-
June 12th, 2006, 06:44 AM
#7
Re: Performance question - char*, vector or similar
Thank you very much for the detailed explanation!
-
June 12th, 2006, 08:13 AM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|