-3

In C++ the size of an array must be determined at the compile-time. I want to write a simple code in C++, say, to do a naive matrix multiplication with itself (for a matrix that is square in size) and I want to do it for any matrix size. However, I do not know how I can get the matrix sizes or the matrices of various sizes at the run-time in C++.

I have read that there are two types of memory, stack and heap. It seems the size of arrays on stack must be determined at the compile-time while the size of an array on heap can be determined at rune-time.

I also know that Numpy can get the size of an array from the user in Python and create an array in C/C++, so, the way Numpy do this might be the solution.

Does Numpy do compilation after getting the size of an array from the user? or are Numpy arrays created on the heap in C? Basically, how does Numpy avoid this issue?

Edit I changed the question to put the focus on how Numpy resolves the issue I had.

10
  • 1
    You want std::vector rather than a raw array, it can have its size set at runtime. (Uses new[] under the hood to achieve that.) It would also be a nice idea to use something like std::mdspan as your function parameter to be able to accept sub-matrices without copying them into new storage, but it's not supported in GCC yet. Commented May 12, 2024 at 11:39
  • 1
    "Does Numpy do compilation after getting the size of an array from the user?...." Python is almost always runtime. Commented May 12, 2024 at 11:40
  • 2
    @MOON They either use malloc() or new[] (which are closely related). The point is that you (usually) have to allocate on the heap to be able to set the size at runtime. Commented May 12, 2024 at 11:45
  • 1
    "you mention that C++ does not know the difference" If you're talking about this "Also the c++ standard has no notion of stack or heap", this is mostly just a bit of (not very useful) trivia. Yes, the standard doesn't use those words, but it doesn't really matter, because everyone else does, and we're not talking specifically about the standard. The standard just calls them differently. Commented May 12, 2024 at 11:50
  • 2
    "People say that heap memory is slower than stack memory." Allocating/deallocating on heap is likely slower, but that's the tradeoff you pay for being able to set size at runtime. There are likely some tricks to reduce the cost, but you shouldn't worry about them until the performance actually becomes a problem. Commented May 12, 2024 at 11:52

1 Answer 1

-1

Does Numpy do compilation after getting the size of an array from the user?

No, python and its different features/aspects almost always are runtime determined.

Do you happen to know what Numpy does? Does it also use std::vector?

Numpy is written/implemented using C and python, so it can't use std::vector as std::vector is provided by C++ standard library.

Instead they use malloc, calloc etc.

Memory management in NumPy

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

2 Comments

I am curious to know how Numpy overcome this issue and hopefully through that I know how these things are handled in compiled languages such as C or C++. Could you please mention how Numpy does it in C? So far I learned in C++ we need to use std::vector and that in C++ there is no difference between heap and stack.
@MOON They use malloc etc as mentioned in the answer. I've also provided a link to the reference where it explains that in more details.

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.