Sign in to follow this  

quick DMA constructor question (c++)

This topic is 2050 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Thanks, but I've never used vectors or the STL, so TBH I don't really know what that means. I was just wondering if there was a way to do it in the c++ lanaguage. Of course I can just make a member function do the same thing (and I've already implemented it like that), but I like elegant solutions, and the thought occured to me that it was odd that you don't seem to be able to pass a value to a constructor if you are creating a dynamic array of those objects, since there doesn't seem to be any syntax for doing that.

Share this post


Link to post
Share on other sites
C++11 adds support for custom constructors that allow this sort of thing, but I'm not sure how widespread support is for it just yet.

Aside from that, your best bet is to call reserve() on the vector to get a bunch of memory allocated in one shot, then push_back() each item you want in the vector, as bradbobak described.

Share this post


Link to post
Share on other sites
[quote name='simon_brown75' timestamp='1336326016' post='4937822']
Thanks, but I've never used vectors or the STL, so TBH I don't really know what that means.
[/quote]
In case it's still not clear:
[code]#include <vector>
#include <iostream>

int main()
{
std::vector<int> v; // our vector contains ints
// If you want your vector to hold a different data type, just change int
// do your data type and push_back the correct values
// If memory reallocation is a problem for you, you can do:
v.reserve(20); // this step is unnecessary, but pre-allocates room for 20 ints

std::cout << "There are " << v.size() << " objects in v" << std::endl;

v.push_back(0); // Add these values to the vector
v.push_back(1);
v.push_back(3);
v.push_back(42);

std::cout << "Now there are " << v.size() << " objects in v" << std::endl;

// v now contains [0, 1, 3, 42]
for (std::size_t i = 0; i < v.size(); ++i)
{
std::cout << i << ": " << v[i] << std::endl;
}
}[/code]

[font=courier new,courier,monospace]std::vector[/font] will grow in size as needed. [font=courier new,courier,monospace]reserve()[/font] may be used to grow (in memory allocation, not in size) the vector in one step, rather than let it gradually grow (in memory allocation, not size).

The [font=courier new,courier,monospace]size()[/font] of a [font=courier new,courier,monospace]vector[/font] is how many objects are in it, not how much memory it has allocated (it will allocate extra memory when it needs to so that it doesn't have to allocate new memory every time you say [font=courier new,courier,monospace]push_back()[/font]).

If instead of [font=courier new,courier,monospace]int[/font]s, you want objects of type [font=courier new,courier,monospace]Foo[/font] that take an [font=courier new,courier,monospace]int[/font] in the constructor, you can do:
[code]
std::vector<Foo> v;
v.push_back(Foo(0));
v.push_back(Foo(100));
// etc...
[/code]

And again, if you already know how many [font=courier new,courier,monospace]Foo[/font]s you want to put in the vector, you can use [font=courier new,courier,monospace]reserve()[/font] to preallocate the memory in one step rather than having the [font=courier new,courier,monospace]vector[/font] gradually allocate memory. The vector will automatically allocate more memory as necessary, and [font=courier new,courier,monospace]reserve(X)[/font] can be used to tell the vector that it's going to need enough memory for [font=courier new,courier,monospace]X[/font] number of objects, and it can allocate all that memory in one step rather than gradually as you [font=courier new,courier,monospace]push_back()[/font] new [font=courier new,courier,monospace]Foo[/font]s.

Share this post


Link to post
Share on other sites

This topic is 2050 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this