简体   繁体   English

将数组传递给函数C ++

[英]Passing array to function C++

I have one quick question about the passing of arrays in C++ which I don't understand. 关于C ++中数组的传递,我有一个简单的问题,我不明白。

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?? 另外,为什么可以将char数组直接传递给C ++中的另一个函数而无需传递地址呢?

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. 我曾尝试在此网上寻找学习资料(例如cplusplus.com),但没有设法找到并解释。

Thanks for your time, Dan. 丹,谢谢您的时间。

As long as C++ is concerned, passing char arrays and int arrays are same. 只要考虑到C ++,传递char arraysint arrays是相同的。

There are 2 ways to pass arrays in c++. 有两种在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. 如果函数的副作用更改了局部状态(例如ex的静态变量),则应谨慎使用。

If you don't want to change contents, use const with arguments. 如果您不想更改内容,请在参数中使用const

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. 如果要在函数参数中复制数组,请考虑使用stl容器(如std::arraystd::vector或在您的类中嵌入数组。

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. 首先,您所讨论的可能是int[]int* (或其他类型),它本身不是数组……它是指向内存块的指针,可以像访问内存一样数组。 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. 其次,将数组作为“连续内存的整个块”传递是相当低效的……传递该点可能只涉及移动32或64位值。 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. 对于内存缓冲区,按引用传递通常是明智之举,如果需要,可以显式使用memcpy功能来复制数据。

Thirdly, I don't understand what you mean about char arrays being "directly" passable, but other types of arrays cannot be. 第三,我不理解您对char数组“直接”可传递的含义,但其他类型的数组则不能。 There's nothing magic about char arrays when it comes to passing or storing them... they're just arrays like any other. 当传递或存储char数组时,没有什么神奇的……它们就像其他数组一样。 The principle difference is that compilers allow you to use string literals to create char arrays. 原则上的区别在于,编译器允许您使用字符串文字来创建char数组。

Lastly, if you're using C++11, you might want to consider the new std::array<T> class. 最后,如果您使用的是C ++ 11,则可能需要考虑新的std::array<T>类。 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. 您可以根据需要通过template<class T> void foo(std::array<T> bar)或引用template<class T> void foo(std::array<T>& bar)传递这些值。

You can't pass any array by value. 您不能按值传递任何数组。 You can pass by value either a struct containing array or std::array from C++11. 您可以通过值传递包含C ++ 11的数组或std::array的结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM