指针问题(仅在发布版本中) [英] Issue with Pointers (Only in Release Build)

查看:127
本文介绍了指针问题(仅在发布版本中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不确定如何描述这一点,但这里我去:



由于某种原因,当试图创建一个版本的游戏测试,敌人创建方面它不工作。

 敌人* e_level1 [3]; 
e_level1 [0] =& Enemies(sdlLib,500,2,3,128,-250,32,32,0,1)
e_level1 [1] =& Enemies(sdlLib,500,2,3,128,-325,32,32,3,1);
e_level1 [2] =&敌人(sdlLib,500,2,3,128,-550,32,32,1,1);

这是我创造我的敌人的方式。工作正常时,在调试配置,但是当我切换到版本配置,它似乎没有正确初始化敌人。



对我来说,这似乎有点奇怪,它在调试工作,但不是在发布,任何帮助赞赏什么是主要是我做错了。

解决方案

其他人已经指出了错误,即临时文件被立即销毁,但所有的答案远在使用手动内存管理 - 它在C ++中更习惯使用eg std :: vector 为这样的东西,例如

  std ::向量<敌人>敌人; 
enemies.push_back(Enemies(sdlLib,500,2,3,128,-250,32,32,0,1));
enemies.push_back(Enemies(sdlLib,500,2,3,128,-325,32,32,3,1));
enemies.push_back(Enemies(sdlLib,500,2,3,128,-550,32,32,1,1));

然后,您只需访问 Enemies 敌人[0] 通过敌人[2] ,当向量超出范围时,它们会自动清除。 / p>

unsure how to go about describing this but here i go:

For some reason, when trying to create a release build version of my game to test, the enemy creation aspect of it isn't working.

Enemies *e_level1[3];
e_level1[0] = &Enemies(sdlLib, 500, 2, 3, 128, -250, 32, 32, 0, 1);
e_level1[1] = &Enemies(sdlLib, 500, 2, 3, 128, -325, 32, 32, 3, 1);
e_level1[2] = &Enemies(sdlLib, 500, 2, 3, 128, -550, 32, 32, 1, 1);

Thats how i'm creating my enemies. Works fine when in the debug configuration but when i switch to the release config, it doesn't seem to initialize the enemies correctly.

To me, this seems a bit strange that it works in debug but not in release, any help appreciated on what mostly is what i've done wrong.

解决方案

Other people have already pointed out the error, namely that the temporaries are getting destroyed straight away, but all of the answers so far are using manual memory management - it's more idiomatic in C++ to use e.g. std::vector for something like this, e.g.

std::vector<Enemies> enemies;
enemies.push_back(Enemies(sdlLib, 500, 2, 3, 128, -250, 32, 32, 0, 1));
enemies.push_back(Enemies(sdlLib, 500, 2, 3, 128, -325, 32, 32, 3, 1));
enemies.push_back(Enemies(sdlLib, 500, 2, 3, 128, -550, 32, 32, 1, 1));

Then you just access the Enemies instances as enemies[0] through enemies[2] and they get cleaned up automatically when the vector goes out of scope.

这篇关于指针问题(仅在发布版本中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆