C++ EasyBMP 指针问题

发布于 2024-10-14 12:48:21 字数 518 浏览 2 评论 0原文

以下代码导致 .ReadFromFile 行出现分段错误:

int main()
{
// Load in.bmp
BMP * original;

cout << "line " << __LINE__ << ": Got here!" << endl;
original->ReadFromFile("in.bmp"); //Error HERE!
int width  = original->TellWidth();
int height = original->TellHeight();
cout << "line " << __LINE__ << ": Got here!" << endl;

我正在使用 EasyBMP 库,它包含在主函数上方。我知道它与内存和指针有关,但我不知道用什么来代替“原始->”......我已经尝试过(*原始)。和(&原始)。但我似乎无法理解。有什么帮助吗?

谢谢!

The following code is causing a Segmentation Fault on the .ReadFromFile line:

int main()
{
// Load in.bmp
BMP * original;

cout << "line " << __LINE__ << ": Got here!" << endl;
original->ReadFromFile("in.bmp"); //Error HERE!
int width  = original->TellWidth();
int height = original->TellHeight();
cout << "line " << __LINE__ << ": Got here!" << endl;

I'm using the EasyBMP library, which is included above the main function. I know it has something to do with memory and pointers, but I can't figure out what to use in place of "original->"... I've tried (*original). and (&original). but I can't seem to get it. Any help?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月亮是我掰弯的 2024-10-21 12:48:22

您的指针original指向随机内存位置。尝试使用它会导致未定义的行为。您需要为BMP对象分配内存并将地址存储在该指针中。如果您希望对象在退出函数后仍然保留,请使用original = new BMP();(但不要忘记稍后删除它),否则您可以直接做BMP原图并使用。

Your pointer original is pointing to a random memory location. Trying to use it will cause the undefined behavior. You need to allocate memory for BMP object and store the address in this pointer. If you want your object to persist even after it goes out of the function use original = new BMP();(don't forget to delete it later though) else you can directly do BMP original; and use it.

挽心 2024-10-21 12:48:22

您已将 BMP 声明为指针但从未初始化它。

尝试使用:

BMP original;

BMP *original = new BMP();

第一个方法将在堆栈上创建原始内容,并且您不必释放它。第二种方法在免费存储中创建它,您需要使用删除来释放它。

delete original;

You have declared BMP as a pointer but never initialized it.

Try using:

BMP original;

or

BMP *original = new BMP();

The first method will create original on the stack, and you won't have to release it. The second method creates it in the free-store, and you need to use delete to free it.

delete original;
梦屿孤独相伴 2024-10-21 12:48:22

根据教程,您的代码应该是:

BMP Image;
Image.ReadFromFile( argv[3] );

According to the tutorial, your code should be:

BMP Image;
Image.ReadFromFile( argv[3] );
尾戒 2024-10-21 12:48:22

没有必要使用指针,请尝试以下操作:

// Load in.bmp
BMP original;

cout << "line " << __LINE__ << ": Got here!" << endl;
original.ReadFromFile("in.bmp"); //Error HERE!
int width  = original.TellWidth();
int height = original.TellHeight();
cout << "line " << __LINE__ << ": Got here!" << endl;

或者如果您需要使用堆,请执行以下操作:

BMP* original = new BMP();

完成后不要忘记释放内存

删除原件;

It is not necessary to use a pointer try this:

// Load in.bmp
BMP original;

cout << "line " << __LINE__ << ": Got here!" << endl;
original.ReadFromFile("in.bmp"); //Error HERE!
int width  = original.TellWidth();
int height = original.TellHeight();
cout << "line " << __LINE__ << ": Got here!" << endl;

or if you need to use the heap do:

BMP* original = new BMP();

and when you are done don't forget to free the memory

delete original;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文