473,320 Members | 1,867 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.

STL collection to wrap a raw pointer?

I have some old code that uses very large c style arrays extensively.
I am to the point where I need to write some new methods that access
this data. Instead of passing this data as a pointer and a count, I'd
like to pass it as a const collection reference. The problem is that I
can't seem to figure out how to intiialize any of the STL collections
with the data without making a copy of the data. Is this
possible?...perhaps with a custom allocator??

Thanks in advance!!

Dec 5 '05 #1
6 3449

314...@gmail.com wrote:
I have some old code that uses very large c style arrays extensively.
I am to the point where I need to write some new methods that access
this data. Instead of passing this data as a pointer and a count, I'd
like to pass it as a const collection reference. The problem is that I
can't seem to figure out how to intiialize any of the STL collections
with the data without making a copy of the data. Is this
possible?...perhaps with a custom allocator??

Thanks in advance!!


I would just implement a "raw collection" class that acts like an STL
container.

Dec 5 '05 #2
31****@gmail.com wrote:
I have some old code that uses very large c style arrays extensively.
I am to the point where I need to write some new methods that access
this data. Instead of passing this data as a pointer and a count, I'd
like to pass it as a const collection reference. The problem is that I
can't seem to figure out how to intiialize any of the STL collections
with the data without making a copy of the data. Is this
possible?...perhaps with a custom allocator??


The most STL-like way of doing it would be a pair of iterators. This
will work with arrays:
int myArray[ARRAY_SIZE];
// initialise or whatever

// Call function that takes iterators
myFunction(myArray, myArray+ARRAY_SIZE);
Then, if you do decide to "upgrade" your arrays to vectors, the
iterators semantics remain valid.

Take a look at just about anything in <algorithm>.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Dec 5 '05 #3
31****@gmail.com wrote:
I have some old code that uses very large c style arrays extensively.
I am to the point where I need to write some new methods that access
this data. Instead of passing this data as a pointer and a count, I'd
like to pass it as a const collection reference. The problem is that I
can't seem to figure out how to intiialize any of the STL collections
with the data without making a copy of the data. Is this
possible?...perhaps with a custom allocator??


A custom allocator won't do: the standard container classes require that
your objects support default construction, copy construction, and
assignment.

What I would do is: wrap the large c style arrays within a "smart array"
class (analogous to a smart pointer): the class (a) pretends to be an array
by overloading operator[] appropriately, (b) uses a reference count and COW
optimization internally, and (c) takes care of automatic lifetime
management. If you need this to be thread safe, be sure to have operator[]
return a proxy (otherwise the possibility of aliasing will mess things up).
This way, you can pretend that your arrays have copy semantics throughout
your code, physical copies will only be made when a logical copy is
modified, and when the last logical copy dies, the underlying c style array
gets killed. I am pretty sure something like this is already available as a
templated library. You might want to check whether boost has something like
this.
Best

Kai-Uwe Bux

Dec 5 '05 #4
Kai-Uwe Bux wrote:
31****@gmail.com wrote:
I have some old code that uses very large c style arrays extensively.
I am to the point where I need to write some new methods that access
this data. Instead of passing this data as a pointer and a count, I'd
like to pass it as a const collection reference. The problem is that I
can't seem to figure out how to intiialize any of the STL collections
with the data without making a copy of the data. Is this
possible?...perhaps with a custom allocator??


A custom allocator won't do: the standard container classes require that
your objects support default construction, copy construction, and
assignment.

What I would do is: wrap the large c style arrays within a "smart array"
class (analogous to a smart pointer): the class (a) pretends to be an
array by overloading operator[] appropriately, (b) uses a reference count
and COW optimization internally, and (c) takes care of automatic lifetime
management. If you need this to be thread safe, be sure to have operator[]
return a proxy (otherwise the possibility of aliasing will mess things
up). This way, you can pretend that your arrays have copy semantics
throughout your code, physical copies will only be made when a logical
copy is modified, and when the last logical copy dies, the underlying c
style array gets killed. I am pretty sure something like this is already
available as a templated library. You might want to check whether boost
has something like this.


Oops, I misunderstood: you do not want to put various c style arrays as
elements in a vector, but you want to access the c style array as though it
was a vector. Well, you can do that already since the STL algorithms
operate on iterators. Just use pointers into the array (and past it) as
range indicators for the algorithms from the STL, and you do not need to
actually turn your array into a vector.
Best

Kai-Uwe Bux

Dec 5 '05 #5

Kai-Uwe Bux wrote:
31****@gmail.com wrote:

A custom allocator won't do: the standard container classes require that
your objects support default construction, copy construction, and
assignment.


I guess not. The standard container requires that the object is
Copy-constructable (which means Copy construction and destruction) and
assignable (which means assignment). There is no requirement of
default-construction.

Eg:
class X {
X() { }
public:
X(int x) { }
X(const X&) { }
};

int main()
{
X p(7);
std::vector<X> a;
a.push_back(p);
}

The above code will work well. Observe that zero-arg constructor is
private.

Dec 6 '05 #6
Neelesh Bodas wrote:

Kai-Uwe Bux wrote:
31****@gmail.com wrote:

A custom allocator won't do: the standard container classes require that
your objects support default construction, copy construction, and
assignment.


I guess not. The standard container requires that the object is
Copy-constructable (which means Copy construction and destruction) and
assignable (which means assignment). There is no requirement of
default-construction.


You are right. There is no such general requirement. My memory
oversimplified the matter: what I was thinking of is covered by [20.1.4]:

The default constructor is not required. Certain container class member
function signatures specify the default constructor as a default argument.
T() shall be a well-defined expression (8.5) if one of those signatures
is called using the default argument (8.3.6).

Best

Kai-Uwe Bux
Dec 6 '05 #7

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

Similar topics

8
by: Generic Usenet Account | last post by:
To settle the dispute regarding what happens when an "erase" method is invoked on an STL container (i.e. whether the element is merely removed from the container or whether it also gets deleted in...
10
by: aa7im | last post by:
I have a similiar thread going about this topic but I decided to break it off into a seperate discussion. Question: What is the best way to determine if an object "IsNew" to a collection? ...
2
by: Victor Nazarov | last post by:
I've tried to implement some garbage collection library for see. Here is an example of it's usage. Do you think that it is usefull at all and deserve development? #include <stddef.h> #include...
19
by: Jamey Shuemaker | last post by:
I'm in the process of expanding my knowledge and use of Class Modules. I've perused MSDN and this and other sites, and I'm pretty comfortable with my understanding of Class Modules with the...
28
by: Michael Primeaux | last post by:
What is the recommended pattern for implementing a synchronized (thread-safe) class that inherits from Collection<T>? For example, I want to implement a SyncRoot property . I do see where I can...
19
by: Born | last post by:
Here are my challenges to you: 1) Suppose object A has to do cleanup before it dies. Also suppose there are multiple references to A. How can I determine when and who to call Dispose (if you use...
0
by: pamela fluente | last post by:
Wrapping the old classic collections was straightforward. But with System.Collections.Generic we have that particular constructor (Of SomeType...) and I am not clear how I can wrap these typed...
5
by: pamela fluente | last post by:
I have been posting this question with no success. I do not know if I am not being clear of the question is too difficult :-)) or unclear. It seems to me that the need to wrap a collection is quite...
158
by: pushpakulkar | last post by:
Hi all, Is garbage collection possible in C++. It doesn't come as part of language support. Is there any specific reason for the same due to the way the language is designed. Or it is...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
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...

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.