what in the c++ program below could lead to segmentation fault #include "component.h"…
- User
what in the c++ program below could lead to segmentation fault
#include "component.h" #include "cimage.h" #include "bmplib.h" #include #include #include #include // You shouldn't need other #include's - Ask permission before adding
using namespace std;
// TO DO: Complete this function CImage::CImage(const char* bmp_filename) { // Note: readRGBBMP dynamically allocates a 3D array // (i.e. array of pointers to pointers (1 per row/height) where each // point to an array of pointers (1 per col/width) where each // point to an array of 3 unsigned char (uint8_t) pixels [R,G,B values])
codeimg_ = nullptr; img_ = readRGBBMP(bmp_filename, h_, w_); // ================================================ // TO DO: call readRGBBMP to initialize img_, h_, and w_; // Leave this check if(img_ == NULL) { throw std::logic_error("Could not read input file"); } // Set the background RGB color using the upper-left pixel for(int i=0; i < 3; i++) { bgColor_[i] = img_[0][0][i]; } // ======== This value should work - do not alter it ======= // RGB "distance" threshold to continue a BFS from neighboring pixels bfsBgrdThresh_ = 60; // ================================================ // TO DO: Initialize the vector of vectors of labels to -1 vector<int> row(w_ , -1); labels_.resize(h_, row); // ================================================ // TO DO: Initialize any other data members}
// TO DO: Complete this function CImage::~CImage() { for (int i=0; i < h_;i++){ if (img_[i] != nullptr){ for (int j=0; j < w_;j++){ delete[] img_[i][j]; } delete [] img_[i]; } } delete [] img_; img_ = nullptr; }
// Complete - Do not alter bool CImage::isCloseToBground(uint8_t p1[3], double within) { // Computes "RGB" (3D Cartesian distance) double dist = sqrt( pow(p1[0]-bgColor_[0],2) + pow(p1[1]-bgColor_[1],2) + pow(p1[2]-bgColor_[2],2) ); return dist <= within; }
// TO DO: Complete this function size_t CImage::findComponents() { size_t count = 0;
for (int i=0; i < h_; i++){ for (int j=0; j < w_;j++){ if (!isCloseToBground(img_[i][j], bfsBgrdThresh_) && labels_[i][j] == -1){ Component newcomp = bfsComponent(i, j, count); components_.push_back(newcomp); count++; } } } return count; }
- Assistant