Two arrays are sent to a function and:
step1_ segments of each of them is used for producing new arrays, and then
step2_ initial arrays are completely replaced by the new arrays.
Here's the code:
Code:
#include "pcg_basic.h"
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void swapChromoSegments(int cutpoint, int chromoSize, int *array1, int *array2);
int main()
{
int myArr1[5] = { 4, 8, 2, 4, 3 };
int myArr2[5] = { 5, 0, 1, 5, 6 };
for (int i = 0; i < 5; i++) {
printf("%d ", myArr1[i]);
}
printf("%c", '\n');
for (int i = 0; i < 5; i++) {
printf("%d ", myArr2[i]);
}
printf("%c", '\n');
pcg32_random_t rng;
pcg32_srandom_r(&rng, time(NULL), (intptr_t) & rng);
int cutpoint = pcg32_boundedrand_r(&rng, 5);
swapChromoSegments(cutpoint, 5, myArr1, myArr2);
for (int i = 0; i < 5; i++) {
printf("%d ", myArr1[i]);
}
printf("%c", '\n');
for (int i = 0; i < 5; i++) {
printf("%d ", myArr2[i]);
}
printf("%c", '\n');
return 0;
}
void swapChromoSegments(int cutpoint, int chromoSize, int *array1, int *array2)
{
int *temp1 = malloc(sizeof(int) * chromoSize);
int *temp2 = malloc(sizeof(int) * chromoSize);
int i = 0;
for (; i < cutpoint; i++) {
temp1[i] = array1[i];
}
for (; i < chromoSize; i++) {
temp1[i] = array2[i];
}
i = 0;
for (; i < cutpoint; i++) {
temp2[i] = array2[i];
}
for (; i < chromoSize; i++) {
temp2[i] = array1[i];
}
array1 = temp1;
array2 = temp2;
free(temp1);
free(temp2);
return;
}
The problem is it obviously changes array1 and array2 "locally" since (according to my guess) they are local pointers in the "swapChromoSegments" function. So myArr1 and myArr2 in main do not change as I expect.
(There's also another problems with this code that know how to solve(using malloc) but I've left it for now as is since it's not my main problem: The address of an array isn't really a pointer so I should not assign a pointer to it as I am doing done above (array1=temp1).)
So in a second attemt I rewrote it like this:
Code:
#include "pcg_basic.h"
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void swapChromoSegments(int cutpoint, int chromoSize, int *array1[],
int *array2[]);
int main()
{
int myArr1[5] = { 4, 8, 2, 4, 3 };
int myArr2[5] = { 5, 0, 1, 5, 6 };
for (int i = 0; i < 5; i++) {
printf("%d ", myArr1[i]);
}
printf("%c", '\n');
for (int i = 0; i < 5; i++) {
printf("%d ", myArr2[i]);
}
printf("%c", '\n');
pcg32_random_t rng;
pcg32_srandom_r(&rng, time(NULL), (intptr_t) & rng);
int cutpoint = pcg32_boundedrand_r(&rng, 5);
swapChromoSegments(cutpoint, 5, &myArr1, &myArr2);
for (int i = 0; i < 5; i++) {
printf("%d ", myArr1[i]);
}
printf("%c", '\n');
for (int i = 0; i < 5; i++) {
printf("%d ", myArr2[i]);
}
printf("%c", '\n');
return 0;
}
void swapChromoSegments(int cutpoint, int chromoSize, int **array1,
int **array2)
{
int *temp1 = malloc(sizeof(int) * chromoSize);
int *temp2 = malloc(sizeof(int) * chromoSize);
int i = 0;
for (; i < cutpoint; i++) {
temp1[i] = *array1[i];
}
for (; i < chromoSize; i++) {
temp1[i] = *array2[i];
}
i = 0;
for (; i < cutpoint; i++) {
temp2[i] = *array2[i];
}
for (; i < chromoSize; i++) {
temp2[i] = *array1[i];
}
*array1 = temp1;
*array2 = temp2;
free(temp1);
free(temp2);
return;
}
But now it's giving a segmentation fault error, I don't know where I'm accessing an illegal memory. The exact GDB error is at this line:
temp1[i]=*array1[i];
Program received signal SIGSEGV, Segmentation fault. ... at test.c:44
Edit:Here's above code with malloc for arrays in main(I feed the input using redirection):
Code:
#include "pcg_basic.h"
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void swapChromoSegments(int cutpoint, int chromoSize, int* array1, int* array2);
int main(){
int* myArr1 = malloc(sizeof(int)*5);
int* myArr2 = malloc(sizeof(int)*5);
//int* myArrHolder[2] = {myArr1,myArr2};
for(int i=0; i<5; i++){
scanf("%d",&myArr1[i]);
}
for(int i=0; i<5; i++){
scanf("%d",&myArr2[i]);
}
for(int i=0; i<5; i++){
printf("%d ",myArr1[i]);
}
printf("%c",'\n');
for(int i=0; i<5; i++){
printf("%d ",myArr2[i]);
}
printf("%c",'\n');
pcg32_random_t rng;
pcg32_srandom_r(&rng, time(NULL), (intptr_t)&rng);
int cutpoint = pcg32_boundedrand_r(&rng,5);
swapChromoSegments(cutpoint, 5, myArr1, myArr2);
for(int i=0; i<5; i++){
printf("%d ",myArr1[i]);
}
printf("%c",'\n');
for(int i=0; i<5; i++){
printf("%d ",myArr2[i]);
}
printf("%c",'\n');
return 0;
}
void swapChromoSegments(int cutpoint, int chromoSize, int* array1, int* array2){
int* temp1 = malloc(sizeof(int)*chromoSize);
int* temp2 = malloc(sizeof(int)*chromoSize);
int i=0;
for(; i<cutpoint; i++){
temp1[i] = array1[i];
}
for(; i<chromoSize; i++){
temp1[i] = array2[i];
}
i=0;
for(; i<cutpoint; i++){
temp2[i] = array2[i];
}
for(; i<chromoSize; i++){
temp2[i] = array1[i];
}
free(array1);
free(array2);
array1 = temp1;
array2 = temp2;
free(temp1);
free(temp2);
return;
}
Now it has the same problem as the snippet#1 in my question. array1 and array2 are changed locally in the "swapChromoSegments" function. So here's a sample input/output I get:
Code:
input:
4 8 2 4 3
5 0 1 5 6
output:
4 8 2 4 3
5 0 1 5 6
0 0 2 4 3
-807234976 32767 1 5 6