损坏的大小与 C++ 中的 prev_size
corrupted size vs. prev_size in C++
我是c++的新手,我今天开始使用它,遇到一个我自己无法解决的错误。
当我 运行 我的代码有时工作正常,有时我会在几秒钟后出错。
我得到的错误是:
- “损坏的大小与 prev_size 中止(核心转储)”
- “分段错误(核心已转储)”
这是我的代码:
#include <opencv2/opencv.hpp>
#include <iostream>
#include <random>
using namespace cv;
using namespace std;
using chrono::duration;
using chrono::duration_cast;
using chrono::high_resolution_clock;
using chrono::milliseconds;
class Product
{
public:
Point location = Point(-80, 0);
Scalar color;
Product(int given_y, Scalar given_color)
{
location.y = given_y;
color = given_color;
cout << "Product created" << endl;
}
Mat update(double speed, Mat img, double dt)
{
location.x += speed* dt;
circle(img, location, 25, Scalar(255, 255, 255), FILLED, LINE_8);
circle(img, location, 20, color, FILLED, LINE_8);
putText(img, "12", location + Point(-17, +7), FONT_HERSHEY_DUPLEX, .8, Scalar(255, 255, 255), 1, LINE_AA);
return (img);
}
};
int main(int argc, char **argv)
{
// Constant variables
const int kScreenWidth = 1920;
const int kScreenHeight = 1080;
const int kUpdateFpsMilliseconds = 600;
const String kWindowName = "Moving dots";
const Mat kBlackImg(kScreenHeight, kScreenWidth, CV_8UC3, Scalar(0, 0, 0));
// Variables
Scalar red = Scalar(255, 0, 0);
Scalar green = Scalar(0, 255, 0);
Scalar blue = Scalar(0, 0, 255);
Scalar cyan = Scalar(0, 255, 255);
Scalar magenta = Scalar(255, 0, 255);
Scalar yellow = Scalar(255, 255, 0);
Scalar black = Scalar(0, 0, 0);
Scalar white = Scalar(255, 255, 255);
vector<Product> products;
Mat img;
double speed = .2;
double fps;
int frame_counter = 0;
int loop_dt;
int time_since_fps_update = 0;
int counter = 0;
bool remove_product = false;
bool full_screen = false;
// Random number generators
random_device rd_seed;
mt19937 rng(rd_seed());
uniform_int_distribution<mt19937::result_type> rd_screen_height(50, kScreenHeight - 50);
uniform_int_distribution<mt19937::result_type> rd_new_product(0, 100);
namedWindow(kWindowName, WINDOW_NORMAL);
setWindowProperty(kWindowName, WND_PROP_FULLSCREEN, WINDOW_FULLSCREEN);
auto loop_start = high_resolution_clock::now();
while (true)
{
// Black image
Mat img = kBlackImg.clone();
// Add new products
if (rd_new_product(rng) < .1 * loop_dt)
{
products.push_back(Product(rd_screen_height(rng), red));
}
// Update products
if (products.size() > 0)
{
for (int i = 0; i <= products.size(); i++)
{
img = products[i].update(speed, img, loop_dt);
}
}
// Show text and border
stringstream temp;
temp << "FPS: " << fps << " Products: " << products.size();
putText(img, temp.str(), Point(18, 38), FONT_HERSHEY_DUPLEX, .8, white, 1, LINE_AA);
rectangle(img, Point(6, 6), Point(kScreenWidth - 6, kScreenHeight - 6), white, 12);
rectangle(img, Point(6, 6), Point(kScreenWidth - 6, 60 - 6), white, 12);
rectangle(img, Point(6, 6), Point(kScreenWidth - 6, kScreenHeight - 6), cyan, 3);
rectangle(img, Point(6, 6), Point(kScreenWidth - 6, 60 - 6), cyan, 3);
// Update screen
cvtColor(img, img, COLOR_RGB2BGR);
imshow(kWindowName, img);
// Exit script
if (waitKey(1) >= 0)
{
break;
};
// Handle FPS counter
frame_counter++;
loop_dt = duration_cast<milliseconds>(high_resolution_clock::now() - loop_start).count();
loop_start = high_resolution_clock::now();
time_since_fps_update += loop_dt;
if (time_since_fps_update > kUpdateFpsMilliseconds)
{
fps = (double)frame_counter / (double)time_since_fps_update * 1000;
time_since_fps_update -= kUpdateFpsMilliseconds;
frame_counter = 0;
}
}
destroyWindow(kWindowName);
return 0;
}
我认为这可能与颜色标量或乘积向量有关。
感谢任何帮助!
罪魁祸首可能是:
for (int i = 0; i <= products.size(); i++)
{
img = products[i].update(speed, img, loop_dt);
}
应该是< products.size()
。向量使用从 0 到 size()-1 的索引,就像几乎所有其他标准容器一样。
我是c++的新手,我今天开始使用它,遇到一个我自己无法解决的错误。
当我 运行 我的代码有时工作正常,有时我会在几秒钟后出错。
我得到的错误是:
- “损坏的大小与 prev_size 中止(核心转储)”
- “分段错误(核心已转储)”
这是我的代码:
#include <opencv2/opencv.hpp>
#include <iostream>
#include <random>
using namespace cv;
using namespace std;
using chrono::duration;
using chrono::duration_cast;
using chrono::high_resolution_clock;
using chrono::milliseconds;
class Product
{
public:
Point location = Point(-80, 0);
Scalar color;
Product(int given_y, Scalar given_color)
{
location.y = given_y;
color = given_color;
cout << "Product created" << endl;
}
Mat update(double speed, Mat img, double dt)
{
location.x += speed* dt;
circle(img, location, 25, Scalar(255, 255, 255), FILLED, LINE_8);
circle(img, location, 20, color, FILLED, LINE_8);
putText(img, "12", location + Point(-17, +7), FONT_HERSHEY_DUPLEX, .8, Scalar(255, 255, 255), 1, LINE_AA);
return (img);
}
};
int main(int argc, char **argv)
{
// Constant variables
const int kScreenWidth = 1920;
const int kScreenHeight = 1080;
const int kUpdateFpsMilliseconds = 600;
const String kWindowName = "Moving dots";
const Mat kBlackImg(kScreenHeight, kScreenWidth, CV_8UC3, Scalar(0, 0, 0));
// Variables
Scalar red = Scalar(255, 0, 0);
Scalar green = Scalar(0, 255, 0);
Scalar blue = Scalar(0, 0, 255);
Scalar cyan = Scalar(0, 255, 255);
Scalar magenta = Scalar(255, 0, 255);
Scalar yellow = Scalar(255, 255, 0);
Scalar black = Scalar(0, 0, 0);
Scalar white = Scalar(255, 255, 255);
vector<Product> products;
Mat img;
double speed = .2;
double fps;
int frame_counter = 0;
int loop_dt;
int time_since_fps_update = 0;
int counter = 0;
bool remove_product = false;
bool full_screen = false;
// Random number generators
random_device rd_seed;
mt19937 rng(rd_seed());
uniform_int_distribution<mt19937::result_type> rd_screen_height(50, kScreenHeight - 50);
uniform_int_distribution<mt19937::result_type> rd_new_product(0, 100);
namedWindow(kWindowName, WINDOW_NORMAL);
setWindowProperty(kWindowName, WND_PROP_FULLSCREEN, WINDOW_FULLSCREEN);
auto loop_start = high_resolution_clock::now();
while (true)
{
// Black image
Mat img = kBlackImg.clone();
// Add new products
if (rd_new_product(rng) < .1 * loop_dt)
{
products.push_back(Product(rd_screen_height(rng), red));
}
// Update products
if (products.size() > 0)
{
for (int i = 0; i <= products.size(); i++)
{
img = products[i].update(speed, img, loop_dt);
}
}
// Show text and border
stringstream temp;
temp << "FPS: " << fps << " Products: " << products.size();
putText(img, temp.str(), Point(18, 38), FONT_HERSHEY_DUPLEX, .8, white, 1, LINE_AA);
rectangle(img, Point(6, 6), Point(kScreenWidth - 6, kScreenHeight - 6), white, 12);
rectangle(img, Point(6, 6), Point(kScreenWidth - 6, 60 - 6), white, 12);
rectangle(img, Point(6, 6), Point(kScreenWidth - 6, kScreenHeight - 6), cyan, 3);
rectangle(img, Point(6, 6), Point(kScreenWidth - 6, 60 - 6), cyan, 3);
// Update screen
cvtColor(img, img, COLOR_RGB2BGR);
imshow(kWindowName, img);
// Exit script
if (waitKey(1) >= 0)
{
break;
};
// Handle FPS counter
frame_counter++;
loop_dt = duration_cast<milliseconds>(high_resolution_clock::now() - loop_start).count();
loop_start = high_resolution_clock::now();
time_since_fps_update += loop_dt;
if (time_since_fps_update > kUpdateFpsMilliseconds)
{
fps = (double)frame_counter / (double)time_since_fps_update * 1000;
time_since_fps_update -= kUpdateFpsMilliseconds;
frame_counter = 0;
}
}
destroyWindow(kWindowName);
return 0;
}
我认为这可能与颜色标量或乘积向量有关。
感谢任何帮助!
罪魁祸首可能是:
for (int i = 0; i <= products.size(); i++)
{
img = products[i].update(speed, img, loop_dt);
}
应该是< products.size()
。向量使用从 0 到 size()-1 的索引,就像几乎所有其他标准容器一样。