0

I was going through the examples for std::transform() function for c++ under STL, from this link , what I understood is , it is used to add two arrays and saves them in a result (res here). So I tried to limit the size of res by size 1 (res[0]) , and I was expecting some compile time error as in below for loop it is trying to extract i < n , (n = 3 here, which would be index out of range).

But i has incremented 8 times in my program. Could someone please explain me the reason , one more thing, is transform is similar to python's res = [x+y for x,y in zip(arr1,arr2)] , thanks in Advance :).

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
  int arr1[]= {1,2,3};
  int arr2[]= {4,5,6,7}; // should be greater than arr1 size

  int n = sizeof(arr1)/sizeof(arr1[0]);
  cout<<"n = "<<n<<endl;
  int res[0];
  cout<<"res[0] = "<<res[0]<<endl;

  transform(arr1,arr1+n,arr2, res, plus<int>());

  for(int i= 0; i < n; i++) {
  cout<<"i = "<<i<<" res[i] = "<<res[i]<<endl;
  }
  cout<<endl;
  return 0;
}

enter image description here

4
  • I'm surprised that code actually builds. Considering that res is a zero sized array which is not allowed. All indexing of it will be out of bounds and lead to undefined behavior. Commented Feb 14, 2018 at 4:30
  • @Someprogrammerdude i tried with res[1] as well, got ` anupam  ~  C++  $  g++ -std=c++11 transform.cpp -o transform.o anupam  ~  C++  $  ./transform.o n = 3 res[0] = 1 res[1] = 134515106 i = 0 res[i] = 5 i = 1 res[i] = 1 i = 2 res[i] = 9 i = 3 res[i] = 1 i = 4 res[i] = 2 i = 5 res[i] = 3 i = 6 res[i] = 4 i = 7 res[i] = 5 i = 8 res[i] = 6 ` Commented Feb 14, 2018 at 4:45
  • 1
    On a totally unrelated note: The suffix .o is for object files, not for executable files. Executable files in a POSIX environment (like Linux) traditionally doesn't have a suffix. Commented Feb 14, 2018 at 5:11
  • thanks @Someprogrammerdude :) , will keep it in mind Commented Feb 14, 2018 at 5:28

1 Answer 1

1

Even after you set the size of the array res to one element you will have undefined behavior.

The problem is that you attempt to use n elements of the array, and you create an empty (which is not allowed) array. Even if you create an array of one element it's to small to fit all values.

When you go out of bounds you will have undefined behavior.

Either make n a compile-time constant and use that as the size for res. Or if n is really variable and could be input from the user then you should use a std::vector.


What really happens with the code you show and the loop, is that the std::transform call will go out of bounds writing to res, and will overwrite the value of n.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.