I'm writing a program with java that analyses stock data.
I almost got it working but now it gives me an ArrayOutOfBounds Exception.
int n = closingPrices.size();
double[][] cParray = new double[n][1];
for(int i = 0; i < n; i++)
{
cParray[i][1] = closingPrices.get(i);
}
I hope you can give me help on how to solve this problem..
the size of cParray[i] is 1. It can have only one element with the index [0]
so try cParray[i][0] = closingPrices.get(i)
OR double[][] cParray = new double[n][2]
Related
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];
}
When I run my program with valgrind it gives an invalid read and write of size 8 error. I have broke my head over this but I can't see what's going wrong.
The valgrind errors occur in the last and second last lines of this code:
void MLPerceptron::returnOutputActivation(vector<Feature> imageFeatures,vector<double>& outputActivation){
int train = 1;
for (unsigned int i = 0; i<imageFeatures.size();i++){
activations[0] = imageFeatures[i].content;
feedforward(train);
activationsToOutputProbabilities();
setMinActivation(outputActivation,activations[2]);
}
}
void MLPerceptron::setMinActivation(vector<double>& minOutputActivation,vector<double> currentActivation){
for(unsigned int i=0;i<currentActivation.size();i++){
if(minOutputActivation[i] > currentActivation[i])
minOutputActivation[i] = currentActivation[i];
}
}
The vectors are initialized in another function and then given to the function returnOutputActivations, this happens in a different file see here:
void MLPController:: createOutputProbabilitiesVectorTest(vector<vector<Feature> >& testSet){
unsigned int nOutputProbabilities = settings.mlpSettings.nOutputUnits;
vector<double> input;
input.reserve(nOutputProbabilities*nMLPs);
for(int j=0; j<nMLPs; j++){
vector<Feature>::const_iterator first = testSet[j].begin();
vector<Feature>::const_iterator last = testSet[j].begin()+numPatchesPerSquare[j];
vector<double> inputTemp = vector<double>(nOutputProbabilities, 10.0);
mlps[0][j].returnOutputActivation(vector<Feature>(first,last),inputTemp);
input.insert(input.end(),inputTemp.begin(),inputTemp.end());
}
Feature newFeat = new Feature(input);
newFeat.setLabelId(testSet[0][0].getLabelId());
inputTrainSecondLayerMLP.push_back(newFeat);
}
I know that there already a lot of posts about the valgrind error but it didn't help me to figure out what's going wrong.
I think
Feature newFeat = new Feature(input)
is the problem. This will allocate a new Feature on the heap, but you will lose its address and it will thus not be deleted. Use Feature* newFeat = new Feature(input) as per MikeCAT's suggestion.
I am getting Segmentation fault: 11 error when trying to run my program (I'm quite a n00b with c++ so take it easy on me). I know it has something to do with memory allocation but I'm not sure what exactly I am doing wrong. Can anyone please help and spot the problem/s?
Basically I'm trying to chop one vector into many small vectors, and analyse each one separately.
std::vector<double> test::getExactHit(std::vector<double> &hitBuffer, double threshold){
int resolution = 100;
int highestRMSBin = 0;
std::vector<double> exactHit(8192);
double* rmsInEachBin = new double[hitBuffer.size()/resolution];
double highestRMSValue = threshold;
for(int i = 0; i<hitBuffer.size()-resolution; i+=resolution){
std::vector<double>::const_iterator first = hitBuffer.begin() + i;
std::vector<double>::const_iterator last = hitBuffer.begin() + i + resolution;
std::vector<double> hitBufferBin(first, last);
rmsInEachBin[i/resolution] = calcRMS(hitBufferBin);
if(rmsInEachBin[i/resolution]>highestRMSValue){
highestRMSValue = rmsInEachBin[i/resolution];
highestRMSBin = i;
}
}
for(int j = 0 ; j < exactHit.size(); j++) {
exactHit[j]=hitBuffer[j+highestRMSBin];
}
return exactHit;
}
Please deallocate all the memory assigned using new or else it will cause memory leak and other bugs also might get introduced because of this .
http://cs.baylor.edu/~donahoo/tools/gdb/tutorial.html
You can debug using GDB , It will be handy to know a debugger if you are programming in C++ .
Hope this info will help you .
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 am working in VC++ 2008, and am trying to allocate a multi-dimensional array of chars to do some file work. I know that whenever an array is allocated all the members of the array should be initialized usually in a successive order. what I have currently is this.
char ** thing = new char *[lineY];
for (int ii = 0; ii < lineY; ii++){
thing[ii] = new char[lineX];
}
... // working with array
// deleting each part of the array.
for (int ii = 0; ii < lineY; ii++){
delete [] thing[ii];
}
delete [] thing;
the problem that I am running into is that if I add the array to the watch list, or put a break right after its been allocated the debugger states that the array is equal to a number like 51, or 32, and not a block of space with indexes, and values, but when I try to initialize the values of each index by making my allocation this:
char ** thing = new char *[lineY];
for (int ii = 0; ii < lineY; ii++){
thing[ii] = new char[lineX];
for (int jj = 0; jj < lineX; jj++){
thing[ii][jj] = '';
}
}
edit: the compiler throws "C2137 empty character constant" am I doing something wrong?
edit: read msdn on the error number, and found answer
You cannot write thing[ii][jj] = '' because '' is an empty character constant which isn't allowed. Try replacing '' with something like ' ' (with the space between 's)
Are lineX and lineY compile-time constants? In that case:
std::array<std::array<char, lineX>, lineY> thing;
Otherwise:
std::vector<std::vector<char> > thing(lineY, std::vector<char>(lineX));