Thread: Change both array elements and the array address in a function

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    42

    Change both array elements and the array address in a function

    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
    Last edited by Salem; 01-05-2019 at 12:23 AM. Reason: removed crayola

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The basic problem is things like
    array1 = temp1;
    array2 = temp2;
    or
    *array1 = temp1;
    *array2 = temp2;

    do NOT copy an entire array from one place to another.

    You need to use a for loop to copy each temp[i] to array[i]
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2018
    Posts
    42
    Salem, your replies are very helpful, thanks. I did that because I thought having 5 loops inside a function is scary... . I fixed it now and it's working fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 11-15-2016, 03:01 AM
  2. Replies: 2
    Last Post: 07-25-2015, 01:11 AM
  3. Replies: 3
    Last Post: 03-16-2012, 04:42 AM
  4. Redeclaring An Array To Change The Number Of Elements
    By Rajin in forum C++ Programming
    Replies: 10
    Last Post: 02-24-2005, 07:28 PM
  5. grow function to change size of array class
    By Santos7772002 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 04:31 AM

Tags for this Thread