指针问题 [英] Problem with pointers

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

问题描述

你好,

我正在使用遗传算法制作这个TSP求解器。我遇到了这个问题,我无法解决这个问题。问题是,我将两个亲本基因和两个子基因传递给交叉函数,然后交叉函数应该为子基因分配记忆并相应地填充它们。但是在函数完成后,两个子基因都是NULL。我调试并发现在交叉功能中,它们被分配了单独的内存并相应地填写。但是当函数返回时,它们变为NULL。我正在使用静态函数。这有点被指责吗?但我尝试将其更改为常规成员函数,但仍会出现同样的问题。如果有人能提供帮助,我会很高兴。我在下面提供相关代码。谢谢。


Hello,
I am making this TSP solver using a genetic algorithm. And I ran into this problem and I can''t work it out. The problem is, I pass the two parent genes and the two child genes into the crossover function and then the crossover function is supposed to allocate memory for the child genes and fill them accordingly. But after the function has finished, the two child genes are NULL. I debugged and found out that inside the crossover function they are allocated separate memory and are filled out accordingly. But when the function returns, they become NULL. I am using a static function. Is this in someway to be blamed? But I tried changing it into a regular member function and still the same problem occurs. I'' d be very glad if anyone can help. I am providing the relevant code below. Thanks.


展开 | 选择 | Wrap | 行号

推荐答案

任何人都可以帮忙吗?




静态函数通常用作包装器...它很奇怪你用这种方式..我的建议是..只是使用更多的私有变量。并将你编写的所有代码放在私有函数中。在静态函数中声明类的对象,然后调用你从静态函数编写的私有函数,看看..


问候,

Xoinki

Static Functions are generally used as wrappers.. its strange that u have used this way.. My suggestion would be.. just use more private variables.. and put all the code you have written inside a private function.. declare an object of the class in the static function and then call the private function you have written from the static function and see..

Regards,
Xoinki


1。你确定这是正确的交叉逻辑,你根本不会使用父。


2.看到麻烦的是你传递子指针是值,所以无论地址的变化如何孩子指针值你将会在当地发挥作用。因此,您必须将指针传递给指向子项的指针,以便返回的函数可以进行更改。


那么您究竟在做什么? GA很酷GA很棒我用它做了很多工作。
1. Are you sure this is correct crossover logic, you ar enot using parent at all.

2. See the trouble is you are passing the child pointers be value so whatever change in the address of child pointer value you willmake wills tay local to function. So you will have to pass a pointer to pointer to child so that the change is available with the returned function.


So what exactly are you doing with GA. GA is cool GA is fantastic I have done lot of work with it.


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

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