views:

100

answers:

7

hi guys,

I would like to serialize vector. And dunno how to change pointer of vector.. To make it simple let's say I have a vector smt 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?

Thanks!!!

/******** EDIT 22.10.2010 *********/

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

vector 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 initiallt 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?

+4  A: 

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.

wheaties
I need the reverse implemantation of this.. I have a char* buffer, and I need to assign it to vector internap pointer...
MCA
@MCA see my edited response. You can't have the vector's internal pointer reassigned to your own pointer. You can have the vector contain what your array has when it is assigned as above. However, if you change the array, the vector will not change.
wheaties
I tried copy algorithm, since I never called constructor, vector is missing it's internal work. I posted another answer below... If I can spawn my vector, I can copy contents...
MCA
A: 

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

siukurnin
well.. ummm... never mind...
MCA
+2  A: 

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.

David Thornley
yea I got you. But still I need to mess with it. Otherwise I force ppl to delete vector on application! SO it would be great if I find a way to do that...
MCA
A: 

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. So 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?

MCA
You can edit your question if you have something to add to it.
dandan78
edit: memcpy can reach, malloc cannot.. im kinda sleepy!
MCA
So apparently you've allocated some memory with malloc, cast it as your object, and then are trying to push the proper data into it from a file? That kind of technique is not appropriate for things like vectors that have their own internally managed memory. If you want to load a vector from a file, then you should create it properly so that the constructor is called and then fill it using its normal public interface (push_back or whatever is appropriate).
TheUndeadFish
what about using .get_allocator() object! I tried constructor and allocation methods so far, didn't get any result...
MCA
The allocator is not likely to help you if you have not properly constructed the vector in the first place. Vector was not designed to allow outside code to mess around with its internals directly. If you can't accomplish what you want with the proper public interface of vector, then perhaps you need to revise your overall design.
TheUndeadFish
A: 

Would this be of any use to you?

vector<char*> v;
char* c = { 'v', 'e', 'c', 't', 'o', 'r' };  
v.push_back(c);
robev
I wish it would.. But internal allocation is messed up! so pushing back is not working...
MCA
A: 

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).

Ben Voigt
A: 

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.

Stefan Monov
ok this is my very first post in here, so I wasnot sure about answering my own question or editing it.. :) I am workin on a big project, we have a lot of code in here, so I just cannot add libraries, or remove vector from everywhere. First I need to find a way to work, if it doesn't work I need a metting with others to discuss about using another lib/remove vector/or do my job in a dişfferent way... And I have come up with a solution.. let's edit my prev post :)
MCA