I'm trying to find the boundary of a sequence of nonzeroes in a row matrix starting from the maximum point and then setting the values outside the boundary to zero.
for(int i=maxloc.x; i < 280 ; i++){
if(!foundBound && sum_r.at<uchar>(0,i) == 0){
foundBound=true;
bb.rightEdge = i;
}
else if(foundBound){
cout << i << endl;
sum_r.at<uchar>(0,i) = 0; <--- offending line
}
}
But the program crashes when I run it. If I comment out sum_r.at<uchar>(0,i) = 0; then the program runs fine until the end. I also got a insufficent memory error once but I can't replicate it. Is there also a better way of accessing the individual elements of a Mat?
The program crash indicates that you are probably going outside the boundaries of your matrix. Check the following:
maxloc.x < 280
sum_r.cols == 280
Related
I'm having trouble understanding why my return data is garbage when I don't use debug to print it out and is fine when I do print it out. I am using C++ make_tuple and tie on the other end for float values. If I don't include enough info let me know!
I have tried checking for uninitialized data by printing out my functions. I also use this exact same code in other parts of the program with no issue.
To give a background of what this program is. I am reading an adc value getting the max value (with error checking) and then sending it for a pass-fail for the system and display to the user. I can work around this in a few ways but I am mostly just curious about this bug.
std::tuple<float,float> hardware_control::hv_check()
{
float hv_filtered_max = 0;
float hv_filtered_avg = 0;
int samples = HV_SAMPLES;
float hv_adc_read[samples];
int non_zero_samples = 0;
int i = 0;
int loops = 0;
//here we will take the a number of samples, average and find the max
while((i < samples) && (hv_filtered_max < HV_Voltage_MAX_CHECK)) // check if less than HV_MIN to speed up test (basically stop testing if it has passed the check)
{
hv_adc_read[i] = check_adc(7);
if(hv_adc_read[i] > 0 && hv_adc_read[i] < 10)
{
hv_filtered_avg += hv_adc_read[i];
non_zero_samples++;
i++;
}
if((hv_adc_read[i] > hv_filtered_max) && hv_adc_read[i] < 10)
{
hv_filtered_max = hv_adc_read[i];
}
loops++;
if(loops > 500) // stop sampling at 500 if we never get anything (this is protection for it possibly freezing i we sample nothing)
{
hv_filtered_max = 0;
break;
}
}
hv_filtered_avg = hv_filtered_avg/non_zero_samples;
return std::make_tuple(hv_filtered_avg,hv_filtered_max);
}
hardware_control hwc;
//where I call and return the data
std::tie(Ins_Data.hv_avg,Ins_Data.hv_max) = hwc.hv_check();
//Me printing out the values
qDebug()<<"MAX_OUTSIDE"<<Ins_Data.hv_max<<endl;
Ins_Data.hv_errors = hwc.HV_error_check();
log_data.push_back("HV_AVG");
log_data.push_back(QString::number(Ins_Data.hv_avg*3));
log_data.push_back("HV_MAX");
log_data.push_back(QString::number(Ins_Data.hv_max*3));
Why this annoys me so bad is that every time I print it out with the qDebug() function it works! if I comment it out, it goes back to 3.8581*10^-38
The value magically comes back to the correct value.
What's going on here? My guess is the make_tuple and tie is corrupting the memory but if so then why is it only sporadically doing it? and why only one of the floats?
SOLVED
I was sampling beyond my initialized array. My array is set to "HV_SAMPLES" however the max number of loops was 500, therefore it sampled beyond the size of my array. Debug functionality must have added some cushion between the array and other values allowing it to output correctly.
I have gathered a large amount of extremely useful information from other peoples' questions and answers on SO, and have searched duly for an answer to this one as well. Unfortunately I have not found a solution to this problem.
The following function to generate a list of primes:
void genPrimes (std::vector<int>* primesPtr, int upperBound = 10)
{
std::ofstream log;
log.open("log.txt");
std::vector<int>& primesRef = *primesPtr;
// Populate primes with non-neg reals
for (int i = 2; i <= upperBound; i++)
primesRef.push_back(i);
log << "Generated reals successfully." << std::endl;
log << primesRef.size() << std::endl;
// Eratosthenes sieve to remove non-primes
for (int i = 0; i < primesRef.size(); i++) {
if (primesRef[i] == 0) continue;
int jumpStart = primesRef[i];
for (int jump = jumpStart; jump < primesRef.size(); jump += jumpStart) {
if (primesRef[i+jump] == 0) continue;
primesRef[i+jump] = 0;
}
}
log << "Executed Eratosthenes Sieve successfully.\n";
for (int i = 0; i < primesRef.size(); i++) {
if (primesRef[i] == 0) {
primesRef.erase(primesRef.begin() + i);
i--;
}
}
log << "Cleaned list.\n";
log.close();
}
is called by:
const int SIZE = 500;
std::vector<int>* primes = new std::vector<int>[SIZE];
genPrimes(primes, SIZE);
This code works well. However, when I change the value of SIZE to a larger number (say, 500000), the compiler returns a "segmentation error." I'm not familiar enough with vectors to understand the problem. Any help is much appreciated.
You are accessing primesRef[i + jump] where i could be primesRef.size() - 1 and jump could be primesRef.size() - 1, leading to an out of bounds access.
It is happening with a 500 limit, it is just that you happen to not have any bad side effects from the out of bound access at the moment.
Also note that using a vector here is a bad choice as every erase will have to move all of the following entries in memory.
Are you sure you wanted to do
new std::vector<int> [500];
and not
new std::vector<int> (500);
In the latter case, you are specifying the size of the vector, whose location is available to you via the variable named 'primes'.
In the former, you are requesting space for 500 vectors, each sized to the default that the STL library wants.
That would be something like (on my system : 24*500 bytes). In the latter case, 500 length vector(only one vector) is what you are asking for.
EDIT: look at the usage - he needs just one vector.
std::vector& primesRef = *primesPtr;
The problem lies here:
// Populate primes with non-neg reals
for (int i = 2; i <= upperBound; i++)
primesRef.push_back(i);
You only have N-2 elements in your vector pushed back, but then try to access an element at N-1 (i+jump). The fact that it did not fail on 500 is just dumb luck that the memory being overwritten was not catastrophic.
This code works well. However, when I change the value of SIZE to a larger number (say, 500000), ...
That may blow your stack, and be to big allocated with it. You need dynamic memory allocation for all of the std::vector<int> instances you believe to need.
To achieve that, simply use a nested std::vetcor like this.
std::vector<std::vector<int>> primes(SIZE);
instead.
But to get straight on, I seriously doubt you need number of SIZE vector instances to store all of the prime numbers found, but just a single one initialized like this:
std::vector<int> primes(SIZE);
I'm coding a game that utilizes a 'grid', which I have created using a 2 dimensional array of structs, which contain a char value and a boolean value. In my program's .h file, I declare the struct and create the grid.
struct Tile
{
char letter;
bool active;
};
Tile grid [6][5];
In my .cpp file, I initialize the grid so that all values are blank.
for (int i = 0; i < 7; ++i)
{
for (int j = 0; j < 6; ++j)
{
grid[i][j].active == false;
//grid[i][j].letter = '.';
//it always crashes when i try doing the above line
}
}
The function that prints the grid, printGrid, is below
for (int i = 0; i < 7; ++i)
{
for (int j = 0; j < 6; ++j)
{
cout << i;
//the above statement is for debugging purposes so that I can see
//which column easier
std::cout << grid[i][j].letter;
}
std::cout << std::endl;
}
cout << "1 2 3 4 5 6" << endl;
Now, the original goal was to have the default .letter value be '.'. But for some reason, when I tried to do this, there are disastrous results; the screen fills up with characters moving so fast I can't entirely see what they are (I recall some hearts and smiley faces), along with an obnoxious, rapid beeping. So I decided to leave that commented line out.
When I run the program without that line, for some reason, the "grid" always displays characters in certain spots, without any input from the user, or without me having expressly declared any values to that spot. For instance, the spot of the 1st column from the left and the bottom row, always has a character in it (grid[6][5].letter). It changes every time I run the program, and I've seen it range from a heart, to the letter A, to the spanish 'n' (the one with a ~ over it).
I thought to myself, "Hey, since grid[6][5] is the spots that are always buggy, I'll just declare those individual spot's .letter values to be blank (' ')!". That didn't work.
I've got no idea why this one spot is giving me trouble. There were other areas that would have an abnormal character, but I was able to neutralize them by setting their .letter values to blank. If anyone has any idea on how to fix this, pleas
EDIT: The other abnormal characters, which appear at grid[6][0], grid[6][1], grid[6][5], and grid[6][4], all make my program crash at later stages if I set them to blank (' '); however, blanking grid[6][5] is the one that makes it crash at the get go. I tried using a debugger, but it wasn't able to tell me anything helpful.
you're running over the end of your arrays
Tile grid [6][5]; needs to be Tile grid [7][6];
or you need to loop only to i < 6 and j < 5.
Today I tried to program the Sieve of Eratosthenes and it works as far as it provides me with the prime numbers. But I have a problem with the dynamic array I don't understand.
First problem: As soon as I try to enter a "big" value for n (for example 120), the program crashes, it doesn't even allocate the memory.
Second problem: If I enter a value like 50 it is able to give out the correct prime numbers but crashes before it deletes the array.
Third problem: If I enter a very small value like 5 it is able to execute the entire program, it gives out the correct numbers and deletes the memory.
But I don't understand why it acts so differently. 120 boolean values can't crash my memory, at least I think so. And why isn't it able to delete an array of 50 values but is actually able to delete an array of 5 values?
Can anyone tell me what's the problem?
int n;
cin >> n;
n=n+1;
bool *feld = new bool[n];
for(int i=2;i<n;i++)
{
int j=i*i;
feld[j]=true;
for(;j<n;j+=i)
feld[j]=true;
}
for(int i=2;i<n;i++)
if(!feld[i])
cout << i << endl;
else;
delete[] feld;
feld = NULL;
Your problem is here:
int j=i*i;
feld[j]=true;
there is no check as to whether j < n so you are stomping over unallocated memory when j >= n.
This code is wrong
bool *feld = new bool[n];
for(int i=2;i<n;i++)
{
int j=i*i;
feld[j]=true;
...
}
Suppose n == 10 and i == 9, then j == 81 but you only have 10 elements in your bool array.
This is how it works when you write bugged programs, sometimes it seems to work, it might even give the right answer, other times it will crash. This is a very important lesson, and you're actually lucky to have learned it early.
Actually It's not just that feld[j]=true; is causing the error.
Also, you don't need that line at all before the loop.
because, it's the first case inside the loop.
I wrote a small program in which, I want to set the value of a frame to 255 based on a vector:
result = cv::Mat::zeros(frame.size(),CV_8UC1);
std::vector<cv::Point2f> imageCorners;
.......................................................
for ( int i = 0 ; imageCorners.size();i++){
std::cout << imageCorners[i]<< std::endl;
result.at<uchar>(imageCorners[i]) = 255;
cv::imshow("result",result);
}
my question is: why the program crashes just after finishing the loop ?? even I see that result is correct ? the error message that I get is :
vector subscript out of range
for ( int i = 0 ; imageCorners.size();i++){
// ^^^^^^^^^^^^^^^^^^^
The underlined part is the condition. In this case you are saying "keep looping until the size of imageCorners is "false" (i.e. 0)". But you never change the size of the vector, so this condition never stops the loop, i keeps getting bigger, until you try to access an index that isn't actually in imageCorners.
Presumably you mean to loop until i gets bigger than the vector. Then use
for (int i=0; i < imageCorners.size(); ++i) {
This looks dodgy to me:
for ( int i = 0 ; imageCorners.size();i++){
you surely wanted to write something like:
for ( int i = 0 ; i < imageCorners.size();i++){
The condition of your loop, imageCorners.size() only yields the number of elements stored in the container. The statement will always evaluate to true as soon as you put one element into imageCorners. What you want is i < imageCorners.size().
for ( int i = 0 ; imageCorners.size();i++)
I think this loop will run forever if imageCorners.size() is different than 0. So when this
std::cout << imageCorners[i]<< std::endl;
gets executed, at some point i will be out of bounds and the program will crash.