473,325 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Problem with pointers

93
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.


Expand|Select|Wrap|Line Numbers
  1. static void Crossover(CGene & parent1, CGene & parent2, CGene * child1, CGene * child2)
  2.     {
  3.                child1 = new CGene(parent1);
  4.            child2 = new CGene(parent2);
  5.  
  6.                double doCrossover = Random((double)0.0f, (double)CROSSOVER_RATE);
  7.  
  8.         //Perform crossover CROSSOVER_RATE% of the time
  9.         if(doCrossover < CROSSOVER_RATE)
  10.         {
  11.             //Partially Matched Crossover
  12. //--------------------------------------------------------------------
  13.             int low, high;                                //Two crossover points
  14.             low = Random(0, GENE_LENGTH);
  15.             high = Random(0, GENE_LENGTH);
  16.  
  17.             if(low > high)
  18.             {
  19.                 int temp = low;
  20.                 low = high;
  21.                 high = temp;
  22.             }
  23.  
  24.             int * temp = new int[high - low];
  25.  
  26.             //Actual crossover in the range [low, high)
  27.             for(int i = low; i < high; i++)
  28.             {
  29.                 temp[i - low] = child1->gene[i];
  30.             }
  31.             for(i = low; i < high; i++)
  32.             {
  33.                 child1->gene[i] = child2->gene[i];
  34.             }
  35.             for(i = low; i < high; i++)
  36.             {
  37.                 child2->gene[i] = temp[i - low];
  38.             }
  39.  
  40.             //Perform the rehabilitation here
  41.  
  42.         }
  43.     }
  44.  
  45.  


Expand|Select|Wrap|Line Numbers
  1. CGene(const CGene & gene)
  2.     {
  3.         this->gene = new int[GENE_LENGTH];
  4.         for(int i = 0; i < GENE_LENGTH; i++)
  5.         {
  6.             this->gene[i] = gene.gene[i];
  7.         }
  8.         this->fitness = gene.fitness;
  9.         this->cost = gene.cost;
  10.     }
  11.  
Oct 11 '07 #1
11 1664
DumRat
93
Can anyone plz help?
Oct 11 '07 #2
xoinki
110 100+
hi,
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
Oct 11 '07 #3
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.
Oct 11 '07 #4
JosAH
11,448 Expert 8TB
Make both child1 and child2 references to CGene pointers. Now you're using a
pass by value mechanism that can't affect any of the pointers outside your
function. You want to set two (other) CGene pointers to a value instead.

The C 'equivalent' (mind the quotes) would be to pass pointers to pointers of
CGene objects but that is so C-ish.

kind regards,

Jos
Oct 11 '07 #5
Banfa
9,065 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. static void Crossover(CGene & parent1, CGene & parent2, CGene * child1, CGene * child2)
  2.     {
  3. // <snipped, not relevent to the problem>
  4.     }
  5.  
You have declared you function incorrectly.

Think of it this way if you wanted a function to pass an int back in a parameter you would declare it as taking a pointer to an int and pass the address of an int to the function

Expand|Select|Wrap|Line Numbers
  1. void Function(int *number)
  2. {
  3.     *number = 10;
  4. }
  5.  
  6. int main()
  7. {
  8.     int myInt;
  9.  
  10.     Function(&myInt);
  11.  
  12.    return 0;
  13. }
  14.  
If you wanted a function to pass a double back in a parameter you would declare it as taking a pointer to a double and pass the address of a double to the function

Expand|Select|Wrap|Line Numbers
  1. void Function(double *number)
  2. {
  3.     *number = 10.1;
  4. }
  5.  
  6. int main()
  7. {
  8.     double myDouble;
  9.  
  10.     Function(&myDouble);
  11.  
  12.    return 0;
  13. }
  14.  
In the general case for any type T if you wanted a function to pass a T back in a parameter you would declare it as taking a pointer to a T and pass the address of a T to the function

Expand|Select|Wrap|Line Numbers
  1. typedef <SomeType> T;
  2.  
  3. void Function(T *number)
  4. {
  5.     *number = T(10);
  6. }
  7.  
  8. int main()
  9. {
  10.     T myT;
  11.  
  12.     Function(&myT);
  13.  
  14.    return 0;
  15. }
  16.  
In your example the type of the variable you are trying to pass back is CGene *, there fore T = CGene * and in order to pass a CGene * back through a parameter the function needs to declare the parameter type as CGene **.

Your function prototype should be

Expand|Select|Wrap|Line Numbers
  1. static void Crossover(CGene & parent1, CGene & parent2, CGene ** child1, CGene ** child2)
  2.     {
  3. // <snipped, not relevent to the problem>
  4.     }
  5.  
Oct 11 '07 #6
Banfa
9,065 Expert Mod 8TB
Make both child1 and child2 references to CGene pointers. Now you're using a
pass by value mechanism that can't affect any of the pointers outside your
function. You want to set two (other) CGene pointers to a value instead.
Damn piped to the post, BTW in a reference to a pointer which way round do the * and & come?
Oct 11 '07 #7
DumRat
93
Hey, thanks!
However, later I got the same problem again with deleting a pointer.
Taking your advice, I was able to solve the first problem.
But I could not delete an object by deleting the pointer passed to the outside by a function. The delete keyword did not clear any memory. I tried all the tricks you told to use here. Passing by reference, pointer to pointers, etc.
Is there something about delete that causes this abnormal behaviour? Or is it me just implementing badly?
Anyway, I got the solution to my first problem. Thanks
Oct 12 '07 #8
DumRat
93
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.

I was doing this for a TSP solver. I love(not literally) GAs too though I do not know too much about them. Glad to find another GA enthusiast!
I got to know about GAs from this very good article. And I've used them on couple of occasions. Not for real problems. Just for fun.

http://www.ai-junkie.com/ga/intro/gat1.html
Oct 12 '07 #9
JosAH
11,448 Expert 8TB
Damn piped to the post, BTW in a reference to a pointer which way round do the * and & come?
If T& is a reference to a T then a CGene*& is a reference to a CGene*.
A CGene&* would be a pointer to a reference of a CGene.

kind regards,

Jos
Oct 12 '07 #10
Banfa
9,065 Expert Mod 8TB
If T& is a reference to a T then a CGene*& is a reference to a CGene*.
A CGene&* would be a pointer to a reference of a CGene.
That is what I figured I just wanted to check :D
Oct 12 '07 #11
Banfa
9,065 Expert Mod 8TB
Hey, thanks!
However, later I got the same problem again with deleting a pointer.
Taking your advice, I was able to solve the first problem.
But I could not delete an object by deleting the pointer passed to the outside by a function. The delete keyword did not clear any memory. I tried all the tricks you told to use here. Passing by reference, pointer to pointers, etc.
Is there something about delete that causes this abnormal behaviour? Or is it me just implementing badly?
Anyway, I got the solution to my first problem. Thanks
The delete keyword does not clear any memory, it returns the memory that was allocated from the heap to the heap. No memory contents are necessaryily changed

This code will print out the same 2 values, delete does not change the contents of the variable holding the pointer, if you want it to change (to NULL say) then you need to do it yourself.
Expand|Select|Wrap|Line Numbers
  1. int *pint = new int;
  2.  
  3. cout << reinterpret_cast<unsigned long>(pint) << endl;
  4.  
  5. delete pint;
  6.  
  7. cout << reinterpret_cast<unsigned long>(pint) << endl;
  8.  
Oct 12 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Joseph | last post by:
I was doing my assignment,but encountered a problem at last step!!!!!! for easy reading, i ommited lots of other things //=====================code begin================================...
16
by: yuraukar | last post by:
I am trying to create a garbage collection class in C++ to collect instances of a specific class (so no general gc). The approach is to use smart pointers and a mark and a simple sweep gc. ...
3
by: Markus Dehmann | last post by:
I have a class "Data" and I store Data pointers in an STL set. But I have millions of inserts and many more lookups, and my profiler found that they cost a lot of runtime. Therefore, I want to...
17
by: Jon Slaughter | last post by:
I'm having a little trouble understanding what the slicing problem is. In B.S.'s C++ PL3rdEd he says "Becayse the Employee copy functions do not know anything about Managers, only the Employee...
5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
10
by: Bryce Calhoun | last post by:
Hello, First of all, this is a .NET 1.1 component I'm creating. SUMMARY ----------------------- This component that I'm creating is, for all intents and purposes, a document parser (I'm...
0
by: rokuingh | last post by:
ok, so i've been working on this one for quite a while, and the code is very big so i'm just going to give the relevant parts. this is a program that builds polymers (chemical structures of repeated...
11
by: Michael | last post by:
Hi, I am trying to get an idea of how function pointers work. I have the following: #include <stdio.h> void do_stuff(int*,int,void*); void getInt(int*); void showInt(int*);
22
by: sam_cit | last post by:
Hi Everyone, I have the following structure in my program struct sample { char *string; int string_len; };
9
by: David | last post by:
Hi all, I posted my question two days ago, and tried to solve this problem. but until now I didn't solve that. and I cut my codes so maybe this time it is more readable. ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.