Returning crossover child integer array issue

Here i am trying to implement Genetic Algorithm Crossover. However I face an issue at the
 
return child_str;


the error that is shown in the compiler is : identifier "child_str" is undefined

did i not define it properly?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  #include<cstring>
#include<cstdio>
#include<iostream>
#include<ctime>
#include<cstdlib>
#include <string>
#include <vector>
#include <algorithm> 

#define CROSSOVERRATE 0.7  //crossover rate set to 0.7
#define SIZEOFSET 8 //set sizes is set to 8
using namespace std;


int* Crossover(int parent1[], int parent2[]);

int main(void)
{
	int parent1[SIZEOFSET] = { 0, 1, 2, 3, 4, 5, 6, 7 };
	int parent2[SIZEOFSET] = { 3, 2, 7, 6, 0, 1, 4, 5 };

	int* Child = Crossover(parent1, parent2);

	return 0;
}


int* Crossover(int parent1[], int parent2[]) {

	srand(time(NULL));
	int crossover_prob = rand() % 100 + 1;  //randomize probability of crossover
	
	int child_str[SIZEOFSET];
	int count;
	int pos_child = 0;

	if (crossover_prob < CROSSOVERRATE)  // if crossover probability is less than the crossover rate then crossover is not performed
	{
		srand(SIZEOFSET);
		int cross_start_pos = rand() - 1;    //random num generated from 0 to hundred for position in set

		int max_substr_size = SIZEOFSET - cross_start_pos;  //find the maximum length of the range of string

		srand(max_substr_size);
		int cross_len = rand();   // determine length of strand
		
		std::string parent1_str = "";

		for (int y = 0; y < SIZEOFSET; y++)
		{
			parent1_str += std::to_string(parent1[y]);
		}

		std::string temp_trim = parent1_str.substr(cross_start_pos, cross_len);

		std::vector<int> substring;
		substring.reserve(temp_trim.size()); //to save on memory reallocations
		std::transform(std::begin(temp_trim), std::end(temp_trim), std::back_inserter(substring),
			[](char c) {
			return c - '0';
		}
		);

		

		for (int i = 0; i < cross_start_pos; i++)
		{

			count = 0;

			for (int j = 0; j < cross_len - 1; j++)
			{
				if (parent2[i] != substring[j])
				{
					count++;
				}
			}

			if (count == cross_len)
			{
				child_str[pos_child] = parent2[i];
				pos_child++;
			}
		}

		for (int i = cross_start_pos + cross_len; i < SIZEOFSET - 2; i++)
		{
			if (pos_child == cross_start_pos)
			{
				for (int k = 0; k < substring.size() - 1; k++)
				{
					child_str[pos_child] = substring[k];
					pos_child++;
				}
			}
			count = 0;

			for (int j = 0; j < cross_len - 1; j++)
			{
				if (parent2[i] != substring[j])
				{
					count++;
				}
			}

			if (count == cross_len)
			{
				child_str[pos_child] = parent2[i];
				pos_child++;
			}
		}
	}
	
	child_str[SIZEOFSET-1]= parent2[SIZEOFSET-1];

	return child_str;
}


What would be the solution to solve this issue.
Well the error I get is

$ g++ -std=c++11 foo.cpp
foo.cpp: In function ‘int* Crossover(int*, int*)’:
foo.cpp:33:6: warning: address of local variable ‘child_str’ returned [-Wreturn-local-addr]
  int child_str[SIZEOFSET];
      ^

Your local array goes out of scope at the point of the return, so the pointer you end up with in main is invalid.

You're using C++, so the solution would be to store things in std::vectors instead.

Also, calling srand() more than once in a program adds no value.

1
2
srand(SIZEOFSET);
int cross_start_pos = rand() - 1;    //random num generated from 0 to hundred for position in set 

Nor does srand() with a small value limit the range of numbers generated by rand().


Since you're using C++11, better random number generators are available to you.
http://www.cplusplus.com/reference/random/
Topic archived. No new replies allowed.