473,394 Members | 1,866 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,394 software developers and data experts.

Reduce memory usage?

Let's say I have a vector of Base.
I want to convert it to a vector of Derived, where
Derived::Derived(const Base&); is defined.

Is it possible to convert the vector of Base to a vector of Derived
without incurring the memory overhead of two copies of everything? If
these were vectors of the same kind, I could just use vector::swap, but
unfortunately not in this example.

Joseph

Jan 9 '06 #1
3 3662
Joseph Turian wrote:
Let's say I have a vector of Base.
A std::vector<Base*>, I presume ?
I want to convert it to a vector of Derived, where
Derived::Derived(const Base&); is defined.

Is it possible to convert the vector of Base to a vector of Derived
without incurring the memory overhead of two copies of everything? If
these were vectors of the same kind, I could just use vector::swap, but
unfortunately not in this example.


Maybe something like this:

1. Define a (template) functor that returns dynamic_cast<Derived*>(p) in operator()(const Base* p)
2. use std::transform(v.begin(), v.end(), v.begin(), CastingFunctor<...>())

/S
--
Stefan Naewe
naewe.s_AT_atlas_DOT_de
Jan 9 '06 #2
Stefan Näwe wrote:
Joseph Turian wrote:
Let's say I have a vector of Base.

A std::vector<Base*>, I presume ?

I want to convert it to a vector of Derived, where
Derived::Derived(const Base&); is defined.

Is it possible to convert the vector of Base to a vector of Derived
without incurring the memory overhead of two copies of everything? If
these were vectors of the same kind, I could just use vector::swap, but
unfortunately not in this example.

Maybe something like this:

1. Define a (template) functor that returns dynamic_cast<Derived*>(p) in operator()(const Base* p)
2. use std::transform(v.begin(), v.end(), v.begin(), CastingFunctor<...>())


This must of course be:

std::transform(v.begin(), v.end(), w.begin(), CastingFunctor<...>())

Where w is a (large enough!) vector<Derived*>

/S
--
Stefan Naewe
naewe.s_AT_atlas_DOT_de
Jan 9 '06 #3
Stefan Näwe wrote:
Stefan Näwe wrote:
Joseph Turian wrote:

Let's say I have a vector of Base.

A std::vector<Base*>, I presume ?
I want to convert it to a vector of Derived, where
Derived::Derived(const Base&); is defined.

Is it possible to convert the vector of Base to a vector of Derived
without incurring the memory overhead of two copies of everything? If
these were vectors of the same kind, I could just use vector::swap, but
unfortunately not in this example.

Maybe something like this:

1. Define a (template) functor that returns dynamic_cast<Derived*>(p) in operator()(const Base* p)
Oops...
This must be:
1. Define a (template) functor that returns dynamic_cast<Derived*>(p) in operator()(Base* p)
2. use std::transform(v.begin(), v.end(), v.begin(), CastingFunctor<...>())

This must of course be:

std::transform(v.begin(), v.end(), w.begin(), CastingFunctor<...>())

Where w is a (large enough!) vector<Derived*>


This might work for you:

<----CODE---->

#include <iostream>
#include <iterator>
#include <string>
#include <algorithm>
#include <vector>

template<typename To>
struct CastingFunctor
{
template<typename From>
To* operator()(From* p) const { return dynamic_cast<To*>(p); }
};

struct Base
{
virtual ~Base() { }
};

struct Derived : public Base
{
};

int main(int argc, char* argv[])
{
const size_t N = 10;
std::vector<Base*> v;

for(size_t i=0; i<N; ++i)
{
v.push_back(new Derived);
v.push_back(new Base);
}

std::vector<Derived*> w(v.size());
std::transform(v.begin(), v.end(), w.begin(), CastingFunctor<Derived>());

std::copy(v.begin(), v.end(), std::ostream_iterator<void*>(std::cout, "\n"));
std::copy(w.begin(), w.end(), std::ostream_iterator<void*>(std::cout, "\n"));
return 0;
}

<----/CODE---->
/S
--
Stefan Naewe
naewe.s_AT_atlas_DOT_de
Jan 9 '06 #4

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

Similar topics

226
by: Stephen C. Waterbury | last post by:
This seems like it ought to work, according to the description of reduce(), but it doesn't. Is this a bug, or am I missing something? Python 2.3.2 (#1, Oct 20 2003, 01:04:35) on linux2 Type...
181
by: Tom Anderson | last post by:
Comrades, During our current discussion of the fate of functional constructs in python, someone brought up Guido's bull on the matter: http://www.artima.com/weblogs/viewpost.jsp?thread=98196 ...
5
by: Justice | last post by:
Currently I'm doing some experimenting with the XMLHTTP object in Javascript. Now, the XMLHttp object is asynchronous (at least in this case), and the following code causes a significant memory...
2
by: tomvr | last post by:
Hello I have noticed some 'weird' memory usage in a vb.net windows app The situation is as follows I have an app (heavy on images) with 2 forms (actually there are more forms and on starting...
2
by: Jarvis | last post by:
I've made a testing program to test the memory usage of some Data Forms. I create a MDI parent form with one single MDI child form, which is a Data Form generated by .NET Data Form Wizard. To...
3
by: Ricardo Q.G. | last post by:
in production environment we started to have a memory consumption problem at "asp.net wp". after a hard work we have discovered that Session.Abandon() does not reduce references to objects added to...
5
by: Mike S | last post by:
I'm working on a .NET application that was under development by a previous developer, who is no longer working with the company. The program basically extracts records from a source database and...
2
by: goRide | last post by:
Hi, i have a small application setting in tray. The main form is hidden but has a media player component on it which plays a url link (radio tuner...). Anyway, my application take about 35MB of...
37
by: Ajai Jose | last post by:
Hi , I work on an ARM processor based embedded system. Code is mainly in C language. The project has a huge source base. We are supposed to optimise it. Most datastructures are declared as static...
1
by: Jean-Paul Calderone | last post by:
On Tue, 22 Apr 2008 14:54:37 -0700 (PDT), yzghan@gmail.com wrote: The test doesn't demonstrate any leaks. It does demonstrate that memory usage can remain at or near peak memory usage even after...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.