开发者

c++ vector How to change internal pointer

I would like to serialize vector. And I dunno how to change pointer of vector.. To make it simple, let's say I have a vector something like:

vector<char> v;

And I have this pointer:

char* c = { 'v', 'e', 'c', 't', 'o', 'r' };

And I would like my vector v's internal pointer points my char* c:

&v[0] -> c 

How can I adjust vector that it would point c? Is there any way to do it?


EDIT 22.10.2010

So guys, after debugging vector I came up with this solution:

vector<char> dump;
memcpy(&myVector, &dump, sizeof(myVector)); // I change contents so it can work
myVector.assign(buf, buf+5); // copy contents into it from buffer (I don't like this part)

And to work this I needed to define

_ITERATOR_DEBUG_LEVEL=0

because it's initially set to 2 and it actually does a debug check (I guess) This is not defined in release mode also right? So it's my workaround for now, I would like to force ppl to remove vector in long term... So guys what are you thinking about this solution? And dangers you see?


I actually need to serialize vector by memcpy. I want to serialize my whole class (like taking a snapshot of memory) and write it to a file, and when reloading read it from file and restore the object as without calling its constructor and using new operator.

Umm this vector container is taking place on stack memory, so memcpy cannot reach it since it's only concern is heap... Well this piece of code might work for me for now:

copy(buf, buf + 5 myVvector.begin());

Well this is not working also, since I do not use new keyword on my class, this vector, as far as I understan, is not initialized. S开发者_如何学JAVAo if I try to copy, push_back_fill this vector with smt, it gives me this error:

Unhandled exception at 0x00ec455b in newDel.exe: 0xC0000005: Access violation reading location 0x00654b4c.

which makes sense... So anybody knows how can I call constructor/initializor of vector from outside?


If you had

vector<char> foo;

foo.push_back( 'v' );
foo.push_back( 'e' );
foo.push_back( 'c' );
foo.push_back( 't' );
foo.push_back( 'o' );
foo.push_back( 'r' );

char *c = &foo[0];

That makes a pointer point to the first item of the contents of the vector. This will remain valid until you attempt to resize the vector.

EDIT:

You can do what you want this way:

copy( c, c+6, back_inserter( myVector ) );

which is the copy call from algorithm and the back_inserter function call from iterator.


There is no standard way to do that. You could dig into a given implementation and see how to deal with that (although there may not actually be a way), but your trick could fail if your library was changed or updated. Library authors generally don't feel the need to support such uses.

vector<> is a container class. It isn't a wrapper for a C-style array, although it maintains one internally. Its behavior and performance are specified by the Standard, but not its internals (except that its contents must be stored in contiguous memory). Library authors take advantage of that freedom to make the best implementations they can.

Suppose you could assign to the internal pointer. That doubtless points to memory from free store, but it may not point to the beginning of the memory, the return value from new or malloc(). The size of the memory must be at least enough to hold the capacity times the size of the elements, but it may in fact be larger. The vector<> might rely on its contents being part of a specific pool, or allocated and deallocated in a specific way. The allocation may affect other data members of vector<>, such as perhaps a pointer to .end(). In any of these cases, you couldn't manage the memory and variables yourself without knowing the exact implementation.

In general, meddling with implementation internals is error-prone and can potentially break, with or without immediately obvious effects, with any change to the library. Nor is it often necessary, or even desirable. Find another way to serialize the vectors.


If it's "internal" then probalby not to be tampered with !


Would this be of any use to you?

vector<char*> v;
char* c = { 'v', 'e', 'c', 't', 'o', 'r' };  
v.push_back(c);


If you use another vector for your I/O buffer, you could then use std::swap to change the content (it will update the pointer and whatever other data is appropriate, like where the count is stored).


I saw your own answer (which btw shouldn't be posted as an answer, it should've been edited into the question). So you want to do serialization in C++. Give up on memcpy and use proper serialization facilities such as boost::serialization. It's not nice but it's necessary. Otherwise, it will simply not work - C++ doesn't work that way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜