读/写受保护的内存?
我目前正在尝试学习 C++,但我在使用下面的代码时遇到问题。
class Vector2
{
public:
double X;
double Y;
Vector2(double X, double Y)
{
this->X = X;
this->Y = Y;
};
SDL_Rect * getSdlOffset()
{
SDL_Rect * offset = new SDL_Rect();
offset->x = this->X;
offset->y = this->Y;
return offset;
};
};
Visual Studio 调用 getSdlOffset() 时抛出以下错误
未处理的类型异常 'System.AccessViolationException' 发生在crossEchoTest.exe
附加信息:尝试 读或写受保护的内存。这 通常表明其他 内存已损坏。
我有 C#/java 背景,但我迷失了......任何帮助将不胜感激。
I'm trying to learn C++ currently, but I'm having issues with the code below.
class Vector2
{
public:
double X;
double Y;
Vector2(double X, double Y)
{
this->X = X;
this->Y = Y;
};
SDL_Rect * getSdlOffset()
{
SDL_Rect * offset = new SDL_Rect();
offset->x = this->X;
offset->y = this->Y;
return offset;
};
};
Visual studio throws throw the following error when calling getSdlOffset()
An unhandled exception of type
'System.AccessViolationException'
occurred in crossEchoTest.exeAdditional information: Attempted to
read or write protected memory. This
is often an indication that other
memory is corrupt.
I've got a C#/java background, and I'm lost... Any help would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您从未初始化过 X 或 Y...您这些值可能是什么?它们很可能指向 00000X00(我很生疏,这可能不是正确的地址,但你被指向程序分配空间之外的内存......因此我将 C/C++“转换”为 Java 的“GPF” (超过 11 年前)所以我可以欣赏您对指针行为方式的想法 - 我可以向您保证指针是 C/C++ 中最难理解的部分,因此您的学习方向是正确的。请记住,与 Java/C# 不同,C/C++ 不会让你伤害自己或其他程序的操作系统内存空间/内存空间我一直记得在我学习 C 时老师曾经告诉我的话——“用 C。你有一双凯夫拉靴子和一把枪,在你开枪射击自己的脚之前是否穿上靴子取决于你,因为你会向自己开枪某些时候......”祝你学习 C++ 好运,坚持下去,不要灰心
。WM
You never initialized X or Y... what do you those values might be? More than likely they are point to 00000X00(I am rusty this may not be the right address, but you are pointed to memory outside of your programs allocated space... thus the "GPF" I was C/C++ "convert" to Java(over 11 years ago) so I can appreciate your ideas of how a pointer might behave--I can assure you that pointers are the most difficult part of C/C++ to understand, so you are on the right track in your learning. Just keep in mind that unlike Java/C#, C/C++ do not keep you from hurting yourself or the OS memory space/memory space of other programs. I alway remember what a teacher once told me when I was learning C--"With C you get a Kevlar boot and a gun, it is up to you whether or not you put the boot on before you shoot yourself in the foot, because you will shoot yourself at some point..." Good luck to you on learning C++, just hang in there and don't get discouraged.
WM
哎呀,语言混乱,当我这么困的时候,对我来说没有SO。
oops, language confusion, no SO for me when so sleepy.
该函数中实际上只有两件事可能出错,要么
this
不好,或者offset
不好。由于您从new SDL_Rect();
获得offset
,所以offset
可能是坏指针的唯一方法是 new 失败但不抛出,这不太可能。因此我们推断
this
是不好的。由于您从未显示用于分配此值的代码,因此我猜测您的代码看起来像这样。您需要向我们展示代码,如果您向我们展示创建 Vector2 对象的位置,我们可能会更具体
There's really only two things can can go wrong in that function, either
this
is bad oroffset
is bad. Since you getoffset
fromnew SDL_Rect();
the only way thatoffset
can be a bad pointer is if new fails but doesn't throw, which doesn't see very likely.Thus we deduce that
this
is bad. Since you never show the code that you are using to allocate this, I'm going to guess that your code looks something like this.You need to show us the code where you If you will show us the where you create the Vector2 object, we could maybe be more specific
可以肯定温特穆特是正确的。还有另一个问题得到了一些很好的答案。您可能想查看那里只是为了了解有关正在发生的情况的更多背景信息:
Release 中的 AccessViolationException模式(C++)
希望有帮助!
Pretty sure Wintermute is correct. There was another SO question that got some great answers. You may want to look in there just to get some more background about what is going on:
AccessViolationException in Release mode (C++)
Hope it helps!