Thread: passing array elements as an index to another array

  1. #1
    Registered User
    Join Date
    Aug 2019
    Posts
    3

    passing array elements as an index to another array

    I have two matrices path[max][2] and input_pair2[max][max] where max is 10 and path_count is dynamic and less than max (let's say 6). I want to use elements of path matrix as an index of input_pair2, i am doing this as in the following code. This works fine with c compiler but giving error in the framework i am using the code in. The error indicates problem of assigning values 0 and 1 to the input_pair2 matrix.

    Code:
    for (int p = 0; p < path_count; p++)
        {
            if (input_pair2[path[p] [0]] [path[p][1]] == 1)
                    input_pair2[path[p][0]] [path[p][1]] = 0;
            else
                    input_pair2[path[p][0]] [array[p][1]] = 1;
          
        }
    Is there any other way, i can do the same in C?

  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
    That is C.

    > This works fine with c compiler but giving error in the framework i am using the code in.
    Yeah, copy/paste the ACTUAL error message.

    Also, what "framework"?
    Names, URLs, whatever where we can know more about what's on your screen is going to be helpful.
    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
    Aug 2019
    Posts
    3
    Error is,
    terminate called after throwing an instance of 'std::runtime_error'
    what(): Unknown literal: 14100. Did you forget to return a value or assign a value to a OUTPUT variable?
    Makefile:8: recipe for target 'output.gate.txt' failed
    make: *** [output.gate.txt] Aborted (core dumped)

    I am using CBMC-GC framework for converting functions to circuits. When i comment the above given code, error vanishes and circuits are generated.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Did you forget to return a value or assign a value to a OUTPUT variable?
    Well since you seem to have some usage of = in your code, perhaps your inappropriately named 'input_pair2' isn't being recognised as an output variable (as per the error message).

    Does the function actually return a value (and you have a return <expression>;) ?
    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.

  5. #5
    Registered User
    Join Date
    Aug 2019
    Posts
    3
    Code:
    typedef struct
    {
        int input_pair1[max][max];
        int input_pair2[max][max];
    }Pair;
    
    typedef struct
    {
       int array[max][2];
       int count;
    }paths;
    Pair augment_path(paths path, Pair pair )
    {
      
    for (int p = 0; p < path.count; p++)
    
        {
    
            if (pair.input_pair2[path.array[p] [0]] [path.array[p][1]] == 1)
    
                    pair.input_pair2[path.array[p][0]] [path.array[p][1]] = 0;
    
            else
    
                    pair.input_pair2[path.array[p][0]] [path.array[p][1]] = 1;
    
           
    
        }
    
    
    
        return pair;
    }
    Yes, it is returning a value.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    And do you use that value?

    As in
    Code:
    Pair result = augment_path(path, pair);
    > where max is 10
    Code:
    typedef struct
    {
        int input_pair1[max][max];
        int input_pair2[max][max];
    }Pair;
    Do you have any idea what sizeof(Pair) is?

    Both your function arguments, and function return are passed "by value".
    Which means you're copying back and forth 100's of bytes at a time.

    Even more so, since you don't seem to use half of your pair structure.


    > I am using CBMC-GC framework for converting functions to circuits.
    I can see your code generating an awful lot of circuits.
    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.

  7. #7
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338

    Question

    Code:
    Pair augment_path(paths path, Pair pair ) {
       for (int p = 0; p < path.count; p++) {
        if (pair.input_pair2[path.array[p] [0]] [path.array[p][1]] == 1) {
            pair.input_pair2[path.array[p][0]] [path.array[p][1]] = 0;
        }  else {
            pair.input_pair2[path.array[p][0]] [path.array[p][1]] = 1;       
        };
       }; 
       return pair;
    };
    
    int main() {
    
     return 0;
    };
    
    


    Identify what is causing (segfaults) ...

    Memory segmentation - Wikipedia
    Last edited by Structure; 08-06-2019 at 07:07 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing char array index as pointer
    By a.mlw.walker in forum C Programming
    Replies: 1
    Last Post: 12-23-2013, 06:46 AM
  2. Sorting elements in 2d array by their index
    By heheho in forum C Programming
    Replies: 5
    Last Post: 06-17-2013, 11:40 AM
  3. Replies: 2
    Last Post: 04-22-2013, 08:17 AM
  4. Replies: 8
    Last Post: 04-04-2012, 09:03 PM
  5. accessing user entered array elements by index?
    By richdb in forum C Programming
    Replies: 10
    Last Post: 04-08-2006, 11:10 AM

Tags for this Thread