473,320 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Mapping a vector<T> to a memory block...

I have a pointer to a memory block. Is there a way I can map a vector<Tto
this memory block? I would like to take advantage of the powerful vector<T>
member functions. Please note that I cannot copy the data into the vector
because the memory block is used/read by other objects.
Jun 19 '07 #1
8 3936
On Jun 19, 4:53 pm, "barcaroller" <barcarol...@music.netwrote:
I have a pointer to a memory block. Is there a way I can map a vector<Tto
this memory block? I would like to take advantage of the powerful vector<T>
member functions. Please note that I cannot copy the data into the vector
because the memory block is used/read by other objects.
Why not -make- the memory block a vector instead? Then instead of
other objects reading the memory block, they read the vector. The C++
standard guarantees that data stored in a vector will be in contiguous
memory, so you can simply say &v[0] if you want to get a pointer to
the first byte of the block.

Alternatively, it might help to know which member functions of vector
you are interested in. The STL provides plenty of powerful methods
you can use on plain vanilla arrays, so maybe you don't really need a
vector at all.

Jun 19 '07 #2

"Zachary Turner" <di***********@gmail.comwrote in message
news:11**********************@q69g2000hsb.googlegr oups.com...
>
Why not -make- the memory block a vector instead? Then instead of
other objects reading the memory block, they read the vector. The C++
standard guarantees that data stored in a vector will be in contiguous
memory, so you can simply say &v[0] if you want to get a pointer to
the first byte of the block.

Alternatively, it might help to know which member functions of vector
you are interested in. The STL provides plenty of powerful methods
you can use on plain vanilla arrays, so maybe you don't really need a
vector at all.
Unfortunately, the memory block is not allocated by me; all I have is a
pointer to it. It just occurred to me that even if I were able to map a
vector to a memory block, I would not be able to use any member function
that could potentially re-size the vector (since the memory block was
created using malloc and is not owned by me). The member functions I was
interested in were insert(), assign(), and erase().

What powerful plain-vanilla-array STL methods were you referring to?


Jun 19 '07 #3
barcaroller wrote:
"Zachary Turner" <di***********@gmail.comwrote in message
news:11**********************@q69g2000hsb.googlegr oups.com...
>Why not -make- the memory block a vector instead? Then instead of
other objects reading the memory block, they read the vector. The C++
standard guarantees that data stored in a vector will be in contiguous
memory, so you can simply say &v[0] if you want to get a pointer to
the first byte of the block.

Alternatively, it might help to know which member functions of vector
you are interested in. The STL provides plenty of powerful methods
you can use on plain vanilla arrays, so maybe you don't really need a
vector at all.

Unfortunately, the memory block is not allocated by me; all I have is a
pointer to it. It just occurred to me that even if I were able to map a
vector to a memory block, I would not be able to use any member function
that could potentially re-size the vector (since the memory block was
created using malloc and is not owned by me). The member functions I was
interested in were insert(), assign(), and erase().
With the caveats you mention, you could provide an allocator as the
second template parameter to std::vector to map the block.
What powerful plain-vanilla-array STL methods were you referring to?
The standard algorithms.

--
Ian Collins.
Jun 19 '07 #4
On Jun 19, 5:49 pm, "barcaroller" <barcarol...@music.netwrote:
What powerful plain-vanilla-array STL methods were you referring to?
Algorithms in STL only care about iterators, the type of collection
doesn't generally matter. A pointer is actually a valid iterator, as
such all generic algorithms can operate on normal arrays. Copy, sort,
search, etc can all be performed on plain arrays. Example:

int x[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int x2[] = {2, 3, 4, 5, 6, 7, 8, 9, 10};
std::copy(x, x+9, x2);

Inserting and deleting items are obviously container specific, however
so in that case you do need the member functions. A custom allocator
could almost have helped you deal with the malloc problem, but vector
being a stack based object will delete your memory block when going
out of scope, which obviously will be problematic.

It sounds like you may be out of luck. The only other possibility
would be that as soon as you receive a pointer to the buffer, copy it
into a vector, tell whoever owns it to go ahead and delete it, then
use the vector for everything. Of course, this may or may not work
depending on your situation and the design of the library that
allocates the buffer. But if you want to use a vector, the vector
must be allowed to own the memory for the items, there is no way
around that.

Jun 19 '07 #5
barcaroller wrote:
I have a pointer to a memory block. Is there a way I can map a vector<Tto
this memory block? I would like to take advantage of the powerful vector<T>
member functions. Please note that I cannot copy the data into the vector
because the memory block is used/read by other objects.

Write a custom allocator, that 'allocates' the pointer you need.

john
Jun 20 '07 #6
On Jun 20, 12:14 am, John Harrison <john_androni...@hotmail.com>
wrote:
barcaroller wrote:
I have a pointer to a memory block. Is there a way I can map a vector<Tto
this memory block? I would like to take advantage of the powerful vector<T>
member functions. Please note that I cannot copy the data into the vector
because the memory block is used/read by other objects.

Write a custom allocator, that 'allocates' the pointer you need.

john
If the allocator actually does something in its deallocate function,
his program will segfault at the end of scope of the vector since it
will delete memory owned by someone else. If the allocator does not
do something in its free function, it will introduce a memory leak
when vector reallocates memory after calling insert(), because it will
think that it freed the old memory and then try to allocate new memory.

Jun 20 '07 #7
Zachary Turner wrote:
On Jun 20, 12:14 am, John Harrison <john_androni...@hotmail.com>
wrote:
>barcaroller wrote:
>>I have a pointer to a memory block. Is there a way I can map a vector<Tto
this memory block? I would like to take advantage of the powerful vector<T>
member functions. Please note that I cannot copy the data into the vector
because the memory block is used/read by other objects.
Write a custom allocator, that 'allocates' the pointer you need.

john

If the allocator actually does something in its deallocate function,
his program will segfault at the end of scope of the vector since it
will delete memory owned by someone else. If the allocator does not
do something in its free function, it will introduce a memory leak
when vector reallocates memory after calling insert(), because it will
think that it freed the old memory and then try to allocate new memory.
Apologies for any mistakes in the following, I don't have a copy of
Josuttis handy, but what I was thinking of was

template <class T>
struct FixedAllocator
{
T* allocate(size_t n) { return ptr; }
void deallocate(T* ptr, size_t n) {}
};

ptr could be global or it could be passed to the constructor. Apart from
ptr not pointing at enough memory, and not being able to copy a vector
with this allocator (I'm guessing the OP could live with this) I don't
see a problem.

john
Jun 20 '07 #8
John Harrison wrote:
Zachary Turner wrote:
>On Jun 20, 12:14 am, John Harrison <john_androni...@hotmail.com>
wrote:
>>barcaroller wrote:
I have a pointer to a memory block. Is there a way I can map a
vector<Tto
this memory block? I would like to take advantage of the powerful
vector<T>
member functions. Please note that I cannot copy the data into the
vector because the memory block is used/read by other objects.
Write a custom allocator, that 'allocates' the pointer you need.

john

If the allocator actually does something in its deallocate function,
his program will segfault at the end of scope of the vector since it
will delete memory owned by someone else. If the allocator does not
do something in its free function, it will introduce a memory leak
when vector reallocates memory after calling insert(), because it will
think that it freed the old memory and then try to allocate new memory.

Apologies for any mistakes in the following, I don't have a copy of
Josuttis handy, but what I was thinking of was

template <class T>
struct FixedAllocator
{
T* allocate(size_t n) { return ptr; }
void deallocate(T* ptr, size_t n) {}
};

ptr could be global or it could be passed to the constructor. Apart from
ptr not pointing at enough memory, and not being able to copy a vector
with this allocator (I'm guessing the OP could live with this) I don't
see a problem.
Uhm, I take it, you left out the other (required) members of the allocator
for the sake of brevity. A more complete version could look like this:

#include <cstddef>
#include <memory>

template < typename T >
struct fixed_allocator : public std::allocator<T{
public:

typedef typename std::allocator<T>::pointer pointer;

fixed_allocator ( pointer ptr )
: std::allocator<T>()
, the_ptr ( ptr )
{}

template < typename U >
fixed_allocator ( fixed_allocator<Uconst & other )
: std::allocator<T( other )
, the_ptr ( other.the_ptr )
{}

pointer allocate ( std::size_t ) {
return ( the_ptr );
}

void deallocate ( pointer, std::size_t ) {}

template < typename U >
struct rebind {
typedef fixed_allocator<Uother;
};

private:

pointer the_ptr;

}; // fixed_allocator
#include <vector>
#include <cassert>

typedef std::vector< char, fixed_allocator<char fixed_char_vector;

int main ( void ) {
char* cp = new char [1000];
fixed_allocator<charca ( cp );
fixed_char_vector v ( ca );
for ( unsigned int i = 0; i < 200; ++i ) {
v.push_back( char(i) );
}
assert( &v[0] == cp );
}
BTW: I am not sure whether the standard allows one to deduce that the assert
will not fail. However, an implementation would have to do something funny
to make it fail.
Best

Kai-Uwe Bux
Jun 20 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Marc Schellens | last post by:
my dinkumware docu says, vector<...>::rbegin() returns an iterator which points just BEYOND the end of the controlled sequence. Is that true? so I cannot say: for( riter i=v.rbegin(); i !=...
2
by: john smith | last post by:
Hi, when there is a vector<> of pointers to some objects, does calling resize cause vector to call delete on each object, or is there a memory leak problem? for example: struct base {//some...
10
by: Stefan Höhne | last post by:
Hi, as I recon, std::vector::clear()'s semantics changed from MS VC++ 6.0 to MS' DOT.NET - compiler. In the 6.0 version the capacity() of the vector did not change with the call to...
6
by: Joe | last post by:
I have a: vector<string> which contains a few dozen elements. I want to find the index of the element containing a certain string. for example: vector<string> strings;...
4
by: Anu | last post by:
Hi, We have a class that has its own overloaded operator new and whose prototype seems to correspond to the standard placement new :- class AppClass { public: operator new (size_t size,...
5
by: Numeromancer | last post by:
From the C++-FAQ Lite: http://www.parashift.com/c++-faq-lite/containers.html#faq-34.3 ---------------------------- 34.3] Is the storage for a std::vector<Tguaranteed to be contiguous? Yes. ...
8
by: jacek.dziedzic | last post by:
Hi! I need to be able to track memory usage in a medium-sized application I'm developing. The only significant (memory-wise) non- local objects are of two types -- std::vector<and of a custom...
3
by: Rune Allnor | last post by:
Hi folks. I have a function that takes an element in a vector as argument. The naive interface goes as float computeSomething(const std::vector<float>& v, size_t i) { size_t j = i-1; size_t...
1
by: iammilind | last post by:
In one of my code, I was using vector<> for certain class. In one of my struct, I have 'const' member data. However, vector<>::clear() throws compile error with that:...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.