2

I have a function that takes in a couple of large multi-dimension float arrays as input. As a result, I crash the stack.

I'm a beginner to C/C++ so I will apologize in advance if this question is dumb. Anyways, after looking around the net, this problem is not particularly new and the general solution is either make it as a global variable or use vector (instead of array).

However, this piece of C++ code was intended to be used as a shared library and takes input from a Python script and returns (or modifies) the value (also in arrays). Therefore I don't think it would be possible to declare as global (correct me if I am wrong).

So, apart from using vector, is there any other way to do so? The reason that put me off from using C++ vector is there is no equivalent data type in Python. I'm using ctypes for communication between Python and C++ if that matters.

1
  • 1
    What abour passing a pointer to the array. Also if you interface with other languages a vector might not be correct. (Better use a pointer and do your indexing with the correct stride manually). Generally if you interface with a lib tze calling convention is C, so with extern "C" vector wouldnt be available anyway Commented Feb 27, 2020 at 11:36

1 Answer 1

1

There is no way in C/C++ to pass an array as a function argument by value. You can pass a pointer to the array and the size of the array. In C++, you also can pass a reference to the array and the size of the array (in template parameters). In either case, the array is not copied and the called function accesses the array in-place, where the caller allocated it.

Note that the array can be wrapped in a structure, in which case it can be passed by value. In C++, std::array is an example of such. But the caller has to initialize that structure instead of the raw array. std::vector is similar in that it is a structure and it automatically copies elements when the vector is copied, but unlike C arrays and std::array it dynamically allocates memory for the elements.

However, if you're integrating with C API, you are most likely limited to a pointer+size or pointer+size wrapped in a C structure solutions. The rules of working with the array (e.g. who allocates and frees the array, are writes to the array allowed, etc.) are specific to the particular API you're working with. Often, there is a dedicated set of functions for working with arrays provided by the library. You should read the API documentation about conventions taken in Python.

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

2 Comments

Now you mention it, I realise that I might have misunderstood my problem. I did pass pointer to my (Python) array to C++. However, right after C++ function returns, it detects a stack crashing. Maybe it is the Python side that is causing trouble.
sorry I meant Stack Smashing, not stack crashing

Your Answer

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