return &this->Buffer[0]; Avoid doing that. Should you ever push anything into your vector, it might get reallocated and then this pointer will no longer be valid.
So you want to pass a vector as a c-string? Wouldn't an std::string be a better choice? Anyway, if you have a 0, change it to ' ' or something. What kind of output do you want? (what does log->dump exactly do?)
It doesn't return nothing. It returns the address of the data in the vector. The first byte of that data happens to be '0'. There isn't anything wrong with that.
It seems like you are trying to do something in a confused way. Perhaps a small self-contained program exhibiting your problem would help others to offer suggestions.
In my case vector stores an information which was received on socket. If packet contains any corupted or bad data, it is dropped and logged..., so the prototype of the function is:
class mem {
public:
std::vector<char> data;
mem(){
}
~mem(){
}
constchar * contents() const {
return &this->data[0];
}
};
int main (int argc, char * const argv[]) {
//OpenBSD 4.8 / CodeBlocks
mem * stack = new mem();
stack->data.push_back(0); // 0 - is the first
stack->data.push_back('t');
stack->data.push_back('e');
stack->data.push_back('s');
stack->data.push_back('t');
stack->data.push_back(0);
stack->data.push_back('1');
stack->data.push_back('2');
std::cout << "The result is: " << stack->contents() << "\n";
std::cout << "The result of direct access: " << &stack->data[0] << "\n";
}
So we have class, in class vector and method contents() which !! return pointer to the first value in vector.
The results of the exec of the program above is
1 2 3 4 5 6 7 8 9 10 11
Program loaded.
run
[Switching to process 778]
The result is:
The result of direct access:
Running…
kill
quit
The Debugger has exited with status 0.
If I will comment the line [ stack->data.push_back(0); // 0 - is the first] then the result is:
1 2 3 4 5 6 7 8
Program loaded.
run
[Switching to process 812]
Running…
The result is: test
The result of direct access: test
kill
quit
So, there is problem with operator [] in vector, every NULL in the memory is considered as line terminator. And this is a problem because, sometimes I need a direct access to vector data outside of the class.
As I understand I have to do copy or memcpy all data to temporary array variable and pass it, or pass the pointer to the vector...
First, there is no problem with the operator[] in vector.
In order to do something with the data that is in the vector, you need to know what it is. Is it two strings, or is it a string followed by an integer, or is it something else? Let's just say that it's a bunch of strings, where the first string is a NULL string (thus the 0 as the first byte. You could do something like this:
1 2 3 4 5 6 7 8 9 10 11 12
char *c = (char *)stack->contents();
char *begin = c;
char *end = c + stack->length(); // You need to write the length() function to return
// the vector's size.
while ( c < end )
{
if (*c == '\0') {
cout << begin << "\n";
begin = c + 1;
}
c++;
}
So, there is problem with operator [] in vector, every NULL in the memory is considered as line terminator. And this is a problem because, sometimes I need a direct access to vector data outside of the class
This has nothing to do with vector. C strings (char arrays) use null characters to mark the end of the string. That's how the computer knows when to stop printing.
If your first character is a 0, then the computer thinks the string has ended. It has no way to know there are characters afterwards.
Try this to see what I mean:
cout << "This will print \0 this will not print";
cout will stop printing as soon as it hits the null character ('\0') in there because it thinks that's the end of the string.
abellia's approach is one possible solution. Another might be to use strings instead of vector<char>. I think (but am not sure) that you can print around null characters if you print strings:
1 2
string foo = "This is a test \0 maybe this will print?";
cout << foo;
Although I'm not sure if that would work.... I'm at work now so I can't test it to be sure.
When I told " the problem with operator [] ... " I did not mean that it has any errors...
Yea, I know that 0x00 is terminator, I simply explained and translated on English my idea incorrectly (maybe)... ---> "it returns nothing because of the zero in vector."
I can not use string )
But, for current problem your solution is not the best one because it require to check all memory from start to end and store it before return...
I have one idea but it require use of assembler in code (that is not a problem for me but problem for another people)... Or just simply create temporary array and do copy...