I have one quick question about the passing of arrays in C++ which I don't understand.

Basically when you want to pass a array of type integer to another function you have to pass an address to that array instead of directly passing the whole block of contiguous memory. Exactly why is the case?

Also, why is that char arrays can directly be passed to another function in C++ without the need to pass an address instead??

I have tried looking for learning materials for this online (such as cplusplus.com) but I haven't managed to find and explanation for this.

Thanks for your time, Dan.

有帮助吗?

解决方案

As long as C++ is concerned, passing char arrays and int arrays are same.

There are 2 ways to pass arrays in c++.

Address is passed

int fn(int *arrays, int len);
int fn(int arrays[], int len); // Similar to above, still staying as sytax hangover from anci c

Array reference is passed

int fn(int (&array)[SIZE]); // Actual array passed as reference

You can templatized above function as

template<size_t SIZE>
int fn(int (&array)[SIZE]);

Above method allows you to pass array of anysize to this function. But beware, a different function is created from template for each size. If your function's side effect changes a local state (static variable for ex), this should be used with care.

If you don't want to change contents, use const with arguments.

If you want a copy of array in function argument, consider using stl container like std::array or std::vector or embed array in your class.

其他提示

It isn't entirely clear from your question exactly what you're trying and what problems you've had, but I'll try to give you useful answers anyway.

Firstly, what you're talking about is probably int[] or int* (or some other type), which isn't an array itself... its a pointer to a chunk of memory, which can be accessed as if it were an array. Because all you have is a pointer, the array has to be passed around by reference.

Secondly, passing around an array as a "whole block of contiguous memory" is rather inefficient... passing the point around might only involve moving a 32 or 64 bit value. Passing by reference is often a sensible thing with memory buffers, and you can explicitly use functions like memcpy to copy data if you needed to.

Thirdly, I don't understand what you mean about char arrays being "directly" passable, but other types of arrays cannot be. There's nothing magic about char arrays when it comes to passing or storing them... they're just arrays like any other. The principle difference is that compilers allow you to use string literals to create char arrays.

Lastly, if you're using C++11, you might want to consider the new std::array<T> class. It provides various handy facilities, including automatic memory management and keeping track of its own size. You can pass these by value, template<class T> void foo(std::array<T> bar) or by reference template<class T> void foo(std::array<T>& bar), as you like.

You can't pass any array by value. You can pass by value either a struct containing array or std::array from C++11.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top