0

Edit: I've edited my code to add the 'extern"C"' part (not sure if that makes a difference)

So I have this small code:

extern "C"{
   void foo(int* nR,int* mR,float* x,float* out){       //&& 
       const int n=*nR,m=*mR;
       other_foo(n,m,x,out);
   }
}

that works fine. But now I want to copy the n-array of float x before passing it to the function other_foo (since other_foo will be altering x and I want to keep a copy).

if I do like so, all works fine:

extern "C"{
   void foo(int* nR,int* mR,float* x,float* out){       //&& 
       const int n=*nR,m=*mR;
       float y[n];
       for(int i=0;i<n;i++) y[i]=x[i];
       other_foo(n,m,x,out);
   }
}

but if I do like so:

extern "C"{
   void foo(int* nR,int* mR,float* x,float* out){       //&& 
       const int n=*nR,m=*mR;
       float y[n];
       std::copy(x,x+n,y);
       other_foo(n,m,x,out);
   }
}

all hell breaks lose: the output of other_foo is not the same anymore!

My question is, of course, why?

1 Answer 1

1

There is no difference between these two code snippets relative to the result of copying.

void foo(int* nR,int* mR,float* x,float* out){      //&& 
    const int n=*nR,m=*mR;
    float y[n];
    for(int i=0;i<n;i++)    y[i]=x[i];
    other_foo(n,m,x,out);
}


void foo(int* nR,int* mR,float* x,float* out){      //&& 
    const int n=*nR,m=*mR;
    float y[n];
    std::copy(x,x+n,y);
    other_foo(n,m,x,out);
}

The both copy n elements from x to y.

However this code is not C++ compliant. The size of an array shall be a constant expression known at compile time. So it would be more correctly to allocate the array dynamically.

For example

void foo(int* nR,int* mR,float* x,float* out){      //&& 
    int n = *nR, m = *mR;
    float *y = new float[n];
    std::copy( x, x+n, y );
    other_foo( n, m, x, out );
    // other code  maybe including delete [] y
}

I think that the problem is not in this function. It seems that the problem is in called function other_foo

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

10 Comments

Many thanks! So this one (your second proposal) works and I will use it.
@user189035 You should check function other_foo. Maybe it has undefined behaviour.
here is the declaration block of other_foo: void other_foo(const int& n,const int& m,float x[],float ox[]). Do you think that could cause the problem?
@user189035 The problem is not in the declaration of the function. The problem can be in the definition of the function. Also if you use unqualified name of algorithm copy as for example copy( x, x + n, y ) then it is possible that you call some C function instead of the algorithm.
thanks for the hints. It's really the float *y = new float[n]; line. When I substitute it by 'float y[n];' (and don't use the loop), it stops working (the output of other_foo becomes non-sense). I've edited my question (the code is warped in an 'extern"C"' do you think that could cause this?
|

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.