C++ two arrays with some duplicate data - c++

I'm working on N-body simulations in C++, I have collected all particles together using an array of a particle class. I need to work on sub-collections of particles separately, perhaps in a new array, but would still like to keep the full collection at the same time.
I already have code which takes an array of particles and acts on them, so ideally I'd like to just make new sub arrays pointing to the same data. Is there a good (neat, and efficient) way to define a sub-array pointing to the same memory as the main array of all the particles?
For example:
particle *full = new particle [10];
// define full here
particle *sub = // last 5 entries of full
func(full); // work on whole collection
func(sub); // work on sub collection
func(full); // work on whole collection with changes on sub[]
I found this post about working on sub arrays which isn't really what I want to do.
Any suggestions would be fantastic, this is my first large-ish project.

In C++ I'd use vectors instead of arrays (personal taste).
so
vector<particle> full;
//fill in `full`
vector<particle*> sub;
for(unsigned i=0;i<5;i++) if(full.size()>5-i) sub.push_back(&full[i]);
Now you have two vectors, sub's elements being pointers to full's elements

You may pass size to your method, and then:
particle *full = new particle [10];
// define full here
particle *sub = full + (10 - 5);// last 5 entries of full
func(full, 10); // work on whole collection
func(sub, 5); // work on sub collection
func(full, 10); // work on whole collection with changes on sub[]
delete[] full;

You can try using memcpy
void * memcpy ( void * destination, const void * source, size_t num );
Just pass the source arg as full+5, sizeof(particle) as size_t num and destination as the new pointer. From there they should be independent.
EDIT:
Use #Jarod42 approach if you want just a reference to the array instead of a whole copy

Related

C++ How to create a dynamic array of vectors?

I'm having problem initialising an array of std::vectors.
I'm declaring and initialising it like this:
vector<component_change*>* _changes;
_changes = new vector<component_change*> [numThreads];
in the hope that it's in the same form as this:
int * foo;
foo = new int [5];
but when I hit a breakpoint after the initialisation, _changes' size is 0.
What am I doing wrong and how can I fix it?
I don't want to use a vector of vectors as the number I need remains constant throughout the program but depends on the current hardware. And I'm not just looking for a single vector (Each vector will be used by a different thread then merged when the threads have finished their tasks).
Thanks guys! :)
Your program is correct. But you misinterpreted the debugger. _changes's size is not 0, but the first vector in your array (the one _changes points at) is empty. Thats because the debugger does not know if _changes points at a single element or an array (in that case the compiler would not know how many elements are in that array). Simply use a vector and call std::vector::shrink_to_fit.
If the size can be determined at compile time use a std::array. If the size is a run-time argument then use a vector and don't change the size of the container.
Are you interested in have a vector for each thread, or a vector containing items used by each thread? I assumed the later, but my answer could be adapted.
This is using a statically sized array; (this syntax is close).
const int NUMBER_OF_THREADS = 5;
component_change* _changes[NUMBER_OF_THREADS] =
{
new component_change(1),
new component_change(2),
new component_change(3),
new component_change(4),
new component_change(5)
}
If the number of threads is dynamic, you will have to use a new...
int NUMBER_OF_THREADS = system.getThreadCount();
component_change* _changes = new component_change[NUMBER_OF_THREADS];
for (int i = 0; i < NUMBER_OF_THREADS; i++)
{
_changes[i] = new component_change(i+1);
}
If you want to a std::vector:
int NUMBER_OF_THREADS = system.getThreadCount();
std::vector<component_change*> _changes;
_changes.reserve(NUMBER_OF_THREADS);
for (int i = 0; i < NUMBER_OF_THREADS; i++)
{
_changes.push_back(new component_change(i+1));
}
I think you're kind of mislead, this size that you are reading belongs to the vector in the first element of the array. Its size is equal to 0 since no elements have been inserted in the vector yet.
new vector is usually wrong.
You should use, with most preferred if possible first,
std::vector<component_change> _changes(numThreads);
or
std::vector<std::unique_ptr<component_change>> _changes(numThreads);
or
std::vector<component_change*> _changes(numThreads);
or if each element of the vector should itself contain an array of components (it's not clear in your question)
std::vector<std::vector<**component_change**>> _changes(numThreads);
Declaring the component as one of the above ways, depending on your needs.
Note that the pointers begin not pointing to anything. You'd have to allocate the individual components as a separate step.
The following creates an array of numThreads vectors, not a vector of numThread elements.
new vector<component_change*> [numThreads]

Arbitrary Dimensional Array

So I'm trying to create an n-dimensional array structure for use in a maze generating program.
I've simplified my problem (for the purposes of trying to get the theory figured out before making it templatized and adding all the necessary helper functions)
So my problem currently boils down to wanting to make an ArbitraryArray class that takes in an argument to its constructor specifying the number of dimensions. Each dimension will have length = 5. (for now)
This is what I have so far:
class ArbitraryArray{
public:
int array[5];
ArbitraryArray*subArray;
ArbitraryArray(){}
ArbitraryArray(int depth){
if (depth == 2) subArray = new ArbitraryArray[5];
else if (depth > 2) for (int i = 0; i < 5; i++) subArray = new ArbitraryArray(depth - 1);
}
};
And I'd create a 2 dimensional object like so:
ArbitraryArray testArray(2);
Or a 3 dimensional object like so:
ArbitraryArray testArray(3);
Problem is, when I tested it for depth = 3 and then tried to set an integer value, via:
testArray.subArray[3].subArray[4].array[4] = 7;
I received a runtime error, leading me to believe that I'm doing something wrong in how I allocate these objects dynamically.
Also, I included an empty default constructor since that gets called by lines like:
subArray = new ArbitraryArray[5];
I'm aware this may not be the best way to go about creating an arbitrary dimensional array data structure, but I'd really like to figure out why this implementation is not working before potentially looking for better methods.
Also I am aware I shouldn't have a line like:
int array[5];
And that it should be a pointer instead so that there isn't a ton of wasted memory allocation for all the levels of the array above the bottom dimension. And I intend to modify it to that after I get this basic idea working.
How about using std::vector for allocating the correct amount of blank memory, which would be
sizeof(T) * dim1 * dim2 * dim3 * ...
Then write a helper class which takes care of the indexing, i.e., it will compute i from given (x,y,z,...), whatever many dimensions you might have.
The beauty of this approach, IMHO, lies in not having to fiddle with pointers, and the helper class simply implements an indexing scheme of your preference (row major or column major).
EDIT
When using std::valarray, things may become easier, as you can use std::slice and/or std::gslice to calculate your indexing for you.
Haven't compiled anything, just visual inspection. What about this:
template<int array_length>
class ArbitraryArray{
public:
int array[array_length];
ArbitraryArray ** subArray;
ArbitraryArray(){}
ArbitraryArray(int depth){
if (depth == 1)
subArray = 0;
else {
subArray = new ArbitraryArray*[array_length];
for (int i = 0; i < array_length; i++)
subArray[i] = new ArbitraryArray(depth - 1);
}
}
};
Well, for once, if depth is greater than 2, you create five ArbitraryArrays, but you save all their pointers in one SubArray pointer. SubArray needs to be an array of pointers to ArbitraryArrays, try ArbitraryArray *subArray[5]; and for (int i = 0; i < 5; i++) subArray[i] = new ArbitraryArray(depth - 1) and see what happens.
In your example you are creating an array that is all over the place in memory instead of one array that is stored in a continuous block of memory. This could cause some issues depending on you handle the memory. e.g. using memcpy on it will never work.
I think a little more flexible approach would be create one large array and instead have an index into the array based on the number of dimensions
int n = static_cast<int>(pow( 5.0, static_cast<double>(depth) ));
Type* a = new Type[ n ];
i.e. since you base your array size on 5, a 2-dim size would be 5x5 and a 3-dim 5x5x5
to access an element in the array say a[2,2,3] (0-based) it could be calculated as
a[2*5*5 + 2*5 + 3]
Just use the Boost multi_array class. It is very flexible, efficient and can perform bounds checking.
Boost Multi-Array

Arrays and overwriting in C++ - can I reuse the same object?

I'm making an array of vectors to save as normals. Because we haven't learned how to do vectors in class yet, I've made a struct which serves just as well:
struct vector3
{
double xcoord;
double ycoord;
double zcoord;
};
Then, at the beginning of my function, I have this:
vector3 vector;
vector3* normalField = new vector3[x];
While the function loops through, in each loop, it applies new values to "vector" - at the end of the function, it sets a part of the array to be vector.
normalField[x] = vector;
The idea is to save memory by not creating a whole bunch of new vectors, and because I have no clue when and where I would be able to use the delete function on that group of vectors. Will this work? Or not? What's the best way of doing this?
The code is very wordy as a whole - I'm writing an algorithm to create a field of normals for procedurally generated terrain. I don't use the built-in vector class because we're not supposed to for some stupid reason. I blame the professor.
The assignment normalField[x] = vector is going to deep copy the data in vector; you will be creating as many vectors as there are elements in normalField[].
Remember too that in C++ the only difference between a struct and a class is that in a struct data members and functions are public by default, but in a class they are private by default.
What you want can be implemented either via array, and creating a new, bigger array when your need for vectors is increased (essentially replicating the behaviour of std::vector), or by using a linked list, which could look like this:
struct vector3List {
vector3 v;
vector3List * next;
};
More refined solutions exist, of course, but the choice depends on what you need to do on the vectors.
In case you are not sure how a list is used, here is an example:
vector3List * listBegin = new vector3List();
// Setup your vector
listBegin->v.coordX = 6;
// Set the end of the list
listBegin->next = 0;
// You need one more vector
listBegin->next = new vector3List();
// Use some kind of helper pointer to keep track of what vector you are examining
// if you need it
vector3List * iterator = listBegin->next;
// Setup this new second vector
iterator->v.coordX = 5;
// Remember to set the end of the list!
iterator->next = 0;
// Iterate throgh the list
iterator = listBegin;
while ( iterator != 0 ) {
// Do stuff
iterator = iterator->next;
}
This is, of course, a naive implementation, but you get the idea.

pointer to string that can be used to dynamically allocate array of strings

Im writing a project and its for a car lot and im creating classes. I need to fullfill certain requirements. For accessory descriptions I need to use a pointer to a string that can be used to dynamically allocate an array of strings with the exact number of accessories. Each element will hold the name of the accessory.
If the number of accessories is 0, there is no need to allocate space, set the pointer to null.
And also pointer to a double that can eb used to dynamically allocate an array of doubles with the same number of elements as accessories. Each element will hold the cost of the associated accessory, that is, the cost in element 0 is the cost of the accessory in element 0.
If the number of accessories is zero, set the pointer to null since there is no need to allocate space.
Heres what my class is so far without those last two requirements. Im stumped.
#ifndef VEHICLE_H
#define VEHICLE_H
class Vehicle
{
public:
Vehicle();
protected:
int vin_number;
string manufacturer;
string model;
string color;
double basecost;
int accessories;
string accessory_list;
private:
};
#endif // VEHICLE_H
Please help it's an online course and ive been googling and reading for hours.
You should not dynamically allocate an array of string.
If you decide to use C++, you should be using STL and collections. Like this:
std::list<std::string> accessory_list;
If you decide to use C, a dynamically allocated string list could look like this:
//init
int accessory_count = 0;
int accessory_cap = 20;
char** accessory_list = calloc (sizeof(char*), accessorry_cap);
//add:
if (accessory_count==accessory_cap) {
accessory_cap += 20;
accessory_list = realloc (accessory_list, sizeof(char*)* accessorry_cap);
}
accessory_list[accessory_count++] = new_accessory.
If you really need a dynamic array of strings, you can do:
int accessory_arr_cap = 20;
string* accessory_arr = new string[accessory_arr_cap];
But since there is no realloc possible in this case, you will have to copy the entire array into new one if you need to enlarge it.
If the cost and name of the option are related, put them in a struct:
struct Option
{
char* Name;
double price;
}
Than what you are looking for is a collection, perhaps a std::vector<Option>. I'll leave it up to you to google the std::vector, it's a good learning exercise.
On a side note, do you have to use C++? You might find another language like C#, or Java a little easier to learn to program with.
If you can't use vectors, make your own collection. I won't post the code because I sense this is an assignment but here's how they normally work:
instantiate with a default size array of say 10.
keep a variable with this max in it
keep a variable with the current number of items in it (starts at 0, maybe call it count or something)
when you add a pointer, put it in element 'count' and increment the counter
if count = capacity then allocate a new array 2*capacity, copy all elements into it, delete the old one and assign the new array to the variable that was the old array.

How to remove elements from dynamically allocated array?

I have a dynamically allocated array :
myRectangle lastRectanglesArray = new myRectangle[lastMaxLabel];
I would like to loop through all elements in this array and remove these that will meet my condition (e.g. too big rectangle).
I have been thinking that I can loop through this array and get the number of elements that would satisfy my condition and then allocate a new array. But how can I 'transfer' these 'wanted' elements into my new array ?
Just for the record: I cannot use STL containers.
myRectangle * lastRectanglesArray = new myRectangle[lastMaxLabel];
// initialize the entries in the lastRectanglesArray
// create a temporary array which contains info about each individual
// entry. namely, it only holds info about whether the entry should
// be kept, or deleted.
// we also use the 'entries' value, which is the number of entries
// in the new array
bool * entriesToKeep = new bool[lastMaxLabel];
int entries = 0;
// check each entry, and mark whether it should be kept or deleted
for (int i = 0; i != lastMaxLabel; ++i) {
// check whether the entry should be kept or deleted...
// here, i just put a function with signature like:
// bool shouldKeepRectangle(const myRectangle &);
entriesToKeep[i] = shouldKeepRectangle(lastRectanglesArray[i]);
if (entriesToKeep[i]) ++entries;
}
// create a new array that will contain the entries that should be kept
myRectangle * rectanglesArray = new myRectangle[entries];
// assign the entries in the new array
for (int i = 0, j = 0; i != lastMaxLabel && j != entries; ++i) {
if (entriesToKeep[i])
rectanglesArray[j++] = lastRectanglesArray[i];
}
// free the memory held by the temp array
delete [] entriesToKeep;
// if the old array is not needed anymore, delete it
delete [] lastRectanglesArray;
// and here you have rectanglesArray, a brand new array that contains
// only the elements that you need.
Just move the next array location over the one that needs to be deleted, and shift everything over til the end of the array.
Yours look like the perfect case for using a Linked List. You would however have to do away with the new myRectangle[lastMaxLabel] part as you would have to implement it as pert of your Insert() function.
This way you would not require to transfer the wanted elements into a new array, but just delete the unwanted element.
Any more light on your use-case would help us to think of better alternatives.
I agree with Michael Chinen - use std::vector instead. You'll avoid lots of other potential problems this way. If you really want to use dynamic arrays, see this question: Remove an array element and shift the remaining ones
if you have a big amount of data in array that will be a problem for shifting using loop
maybe you should build your own array management class (find,add,deleteAt,etc).
my suggestion use link list node method.. it will be faster rather then you use loop for shifting.