I've been unsuccessfully trying to create an array of IloModel object. Is there an easy way to do it ?
This is what I've tried so far:
IloModel* models = new(env) IloModel[S];
The code compiles but whenever I try to add constraints to each one of the models, the following error message is displayed:
Error:trying to add to an empty handle IloModel
I've removed the argumento from the new operator and added the following lines to code I'm wrinting:
cout << "0" << endl;
for(int i=0; i < S; S++)
models[i] = IloModel(env);
But now the computer freezes when I run the code.
You are creating an array of empty model objects (i.e. objects without handle). After creating the array of empty models you have to initialize each of the models:
for (int i = 0; i < 5; ++i) models[i] = IloModel(env);
I am not sure about the placement-new syntax for allocating the array. Unless there is a very good reason not to use the default operator new, it is probably easier to read to just us that or even allocate the array on the stack.
EDIT: Here is a full code snippet that works for me:
IloEnv env;
int S = 5;
IloModel *models = new IloModel[S];
for (int i = 0; i < S; ++i)
models[i] = IloModel(env);
Related
I'm new in C++; here is my problem.
DataInterface is a struct defined.
I would like to create Datainterface_1, Datainterface_2, Datainterface_3 as an instance of existing DataInterface. How to do that.
I wrote the following :
for (int i = 0; i < 3; ++i)
{
DataInterface* Datainterface_$i = .....;
}
It doesn't work.
Any ideas?
Use a container - like std::vector or std::array - to hold multiple instances.
I'm working on a school assignment. It is an assignment in which every 2 weeks, we have to either expand or change the layout of it. This week, we are forced to use pointers. I'm having a hard time understanding memory and how to allocate it properly without having segmentation faults.
I've created a struct array which is initialized to a char pointer. Every time i loop, after the 1st loop i get "segmentation faults". I simply don't understand why?
I could include the whole code but according to gdb my issue is pertaining to 1 specific line.
const int arraySize = 100;
int counter = 0;
struct contacts{
char * name;
char * date;
char * note;
};
contacts * contactList[arraySize] =
contactList = new contacts;
for(int i = 0; i <= counter; i++){
contactList[i]->name = new char[20]; //Segmentation Fault here
std::cout << contactList[i]->name << std::endl;
//first 1 outputs = fine
//2nd output = segmentation error
counter++;
}
The code is simplified and minimilized for easy reading. If anyone wants it i can insert the whole code. Just be wary that is relatively big. I have set breakpoint through my code to narrow it down. It has come down to that specific statement. Everything else i perfectly fine, especially since it all compiles perfectly fine.
Any hints or assistance with it can be great.
Also I'm not allowed to use any vectors, strings, etc., only cstrings.
User mentioned that i only create 1 contact.
contacts * contactList[arraySize];
contactList = new contacts;
//Instead it should be like this:
contacts * contactList[arraySize];
contactList = new contacts[arraySize];
Update:
I've tried using what everyone recommended.
contacts* contactList[arraySize];
contactList = new contacts[arraySize];
But i get this error:
error: incompatible types in assignment of‘ContactClass::contacts*’ to ‘ContactClass::contacts* [100]’
Your first problem is in this line:
contacts * contactList[arraySize] = new contacts;
it should be
contacts* contactList = new contacts[arraySize];
next problem is here
for(int i = 0; i <= counter; i++){
should be
for(int i = 0; i < arraysize; i++){
contactList[i].name = new char[20];
}
I'm trying to create a Pointer to a dynamic 3D Array full of Pointers. I'm working with Voxel, so let's say that t_cube is my object.
First, I tried doing this:
t_cube* (*m_Array)[][][];
I thought I could do like
m_Array = new t_cube[sizeX][sizeZ][sizeY];
Compiling this failed, however.
Next I tried this:
t_cube *(m_Model[]); // This is my .h
{
t_cube *model_Tempo[sizeX][sizeZ][sizeY]; // And this is in my class constructor.
m_Model = model_Tempo;
}
Again, this failed to compile.
I hope this example would be helpful to solve your problem:
Since, we are dealing with Pointer of 3-D Array. So, if I write it in C++ grammar, it would be like:
t_cube *array[x_size][y_size][z_size];
But, you already mentioned, it fails to execute.
Now, do the same thing using Dynamic Allocation Approach.
t_cube ****array; // Since, it a pointer to the 3D Array
array = new t_cube ***[x_size];
for(int i=0; i<x_size; i++) {
array[i] = new t_cube **[y_size];
for(int j =0; j<y_size; j++) {
array[i][j] = new t_cube *[z_size];
}
} /* I'm sure this will work */
And, the reasons you were facing trouble:
The size of the m_Array could be very large : x_size * y_size * z_size * sizeof(t_cube) .
You must have defined m_Array locally (inside the function), which is the major reason of program malfunction.
As above, I'm trying to create and then fill an array of structures with some starting data to then write to/read from.
I'm still writing the cache simulator as per my previous question:
Any way to get rid of the null character at the end of an istream get?
Here's how I'm making the array:
struct cacheline
{
string data;
string tag;
bool valid;
bool dirty;
};
cacheline **AllocateDynamicArray( int nRows, int nCols)
{
cacheline **dynamicArray;
dynamicArray = new cacheline*[nRows];
for( int i = 0 ; i < nRows ; i++ )
dynamicArray[i] = new cacheline [nCols];
return dynamicArray;
}
I'm calling this from main:
cacheline **cache = AllocateDynamicArray(nooflines,noofways);
It seems to create the array ok, but when I try to fill it I get memory errors, here's how I'm trying to do it:
int fillcache(cacheline **cache, int cachesize, int cachelinelength, int ways)
{
for (int j = 0; j < ways; j++)
{
for (int i = 0; i < cachesize/(cachelinelength*4); i++)
{
cache[i][ways].data = "EMPTY";
cache[i][ways].tag = "";
cache[i][ways].valid = 0;
cache[i][ways].dirty = 0;
}
}
return(1);
}
Calling it with:
fillcache(cache, cachesize, cachelinelength, noofways);
Now, this is the first time I've really tried to use dynamic arrays, so it's entirely possible I'm doing that completely wrong, let alone when trying to make it 2d, any ideas would be greatly appreciated :)
Also, is there an easier way to do write to/read from the array? At the moment (I think) I'm having to pass lots of variables to and from functions, including the array (or a pointer to the array?) each time which doesn't seem efficient?
Something else I'm unsure of, when I pass the array (pointer?) and edit the array, when I go back out of the function, will the array still be edited?
Thanks
Edit:
Just noticed a monumentally stupid error, it should ofcourse be:
cache[i][j].data = "EMPTY";
You should find your happiness. You just need the time to check it out (:
The way to happiness
I think I'm confused with instantiating objects.. well.. not properly object because new statements make a compile error. My background is in Python and Java and I'm stuck in front of C++ way of creating objects that aren't classes.
I'm translating an algorithm from C# and for machine learning and it uses an array of multidimensional arrays.
C# code:
public double Learn(int[][] observations, int symbols, int states, ...
// ...
double[][, ,] epsilon = new double[N][, ,]; // also referred as ksi or psi
double[][,] gamma = new double[N][,];
for (int i = 0; i < N; i++)
{
int T = observations[i].Length;
epsilon[i] = new double[T, States, States];
gamma[i] = new double[T, States];
}
I've decided to use the Boost library for the multidimensional array, and I have:
typedef boost::multi_array<double, 2> matrix2D;
typedef boost::multi_array<double, 3> matrix3D;
vector<matrix3D> epsilon;
vector<matrix2D> gamma;
cout << "HMM::learn >> before" << endl;
for (int i = 0; i < N; i++)
{
int T = observations[i].size();
epsilon[i] = matrix3D(boost::extents[T][states][symbols]);
gamma[i] = matrix2D(boost::extents[T][states]);
}
and I get this runtime error:
HMM::learn >> before
std::bad_alloc' what(): std::bad_alloc
The vectors have no space allocated (well it may be reserved already but you can't reference it with the array indexers). Change the lines:
epsilon[i] = matrix3D(boost::extents[T][states][symbols]);
gamma[i] = matrix2D(boost::extents[T][states]);
To:
epsilon.push_back(matrix3D(boost::extents[T][states][symbols]);
gamma.push_back(matrix2D(boost::extents[T][states]);
that should solve it. In your case since you know the array size you should reserve that much space in the vectors so that it reduces the reallocations needed:
epsilon.reserve(N);
gamma.reserve(N);