472,338 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

help on std::vector

Tim
Dear All,

std::vector seems using the copy constructor to allocate memory when
needed. Is it possible to control the memory allocation, such as by
using my own allocator? My question is as follow:

I have a smart pointer my_ptr, and the copy constructor uses deep
copy, however I want it to be shallow copy in std::vector when
reallocate memory. How can I implement it?

This seems a very challenging question as least for me. I appreciate
your kind help.

Best regards,

Tim

Sep 3 '07 #1
2 1904
On Sep 3, 2:24 pm, Tim <Tian.Xiao.2...@gmail.comwrote:
Dear All,

std::vector seems using the copy constructor to allocate memory when
needed. Is it possible to control the memory allocation, such as by
using my own allocator? My question is as follow:

I have a smart pointer my_ptr, and the copy constructor uses deep
copy, however I want it to be shallow copy in std::vector when
reallocate memory. How can I implement it?

This seems a very challenging question as least for me. I appreciate
your kind help.

Best regards,

Tim
1. You can use reserve on the vector, if you know the maximum number
of elements
that you'll use, to prevent re-allocation and copying.

2. Use a vector of raw pointers if the vector is being used as
temporary storage
(e.g. inside a function) to work with the elements.

3. Use a vector of some other pointer class that does shallow copy.

Hope that helps.

Sep 3 '07 #2
Tim wrote:
Dear All,

std::vector seems using the copy constructor to allocate memory when
needed. Is it possible to control the memory allocation, such as by
using my own allocator? My question is as follow:

I have a smart pointer my_ptr, and the copy constructor uses deep
copy, however I want it to be shallow copy in std::vector when
reallocate memory. How can I implement it?

This seems a very challenging question as least for me. I appreciate
your kind help.
There are some simple things you can do but it's not trivial to get
right. I think you're going to need to implement your own container to
do this right.
Sep 4 '07 #3

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

Similar topics

27
by: Jason Heyes | last post by:
To my understanding, std::vector does not use reference counting to avoid the overhead of copying and initialisation. Where can I get a reference...
20
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src;...
17
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be...
32
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the...
9
by: aaragon | last post by:
I am trying to create a vector of type T and everything goes fine until I try to iterate over it. For some reason, the compiler gives me an error...
6
by: lokchan | last post by:
i want to create a vector of pointer s.t. it can handle new and delete but also have std::vector interface can i implement by partial...
8
by: Lionel B | last post by:
On my platform I find that the std::vector<boolspecialisation incurs a significant performance hit in some circumstances (when compared, say, to...
13
by: jubelbrus | last post by:
Hi I'm trying to do the following. #include <vector> #include <boost/thread/mutex.hpp> #include <boost/shared_ptr.hpp> #include...
3
by: DevNull | last post by:
I have a program where we load a mapfile, comprised of a .csv with numbers which represent object types at a given position. Each position is in...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...

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.