C++ - 将数组传递给方法

发布于 2024-07-18 04:02:49 字数 160 浏览 4 评论 0原文

这是一个与我定义的函数类似的函数:

void Function( BYTE *data );

我想做的是这样的:

Function( new BYTE { 0x00, 0x00 } );

Here is a function similar to the one I've defined:

void Function( BYTE *data );

What I would like to do is something like this:

Function( new BYTE { 0x00, 0x00 } );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

断念 2024-07-25 04:02:49

您不能将数组初始值设定项语法与使用 new 动态分配的数组结合使用。 你可以这样做:

BYTE *ary=new BYTE[2];
ary[0] = 0;
ary[1] = 0;
Function(ary);
delete [] ary;

但是为什么在这里使用动态分配的内存呢? 该数组是否保留在当前函数的范围之外? 如果没有,您可以使用在堆栈上分配的数组:

BYTE ary[2] = {0};
Function(ary);

在 C++ 中,首选方法是使用 STL 类 std::vector ,它的作用类似于动态分配(但类型安全)的数组:

std::vector<BYTE> ary(2);
Function(&ary[0]);

You cannot use the array initialiser syntax with dynamically allocated arrays using new. You could do something like this:

BYTE *ary=new BYTE[2];
ary[0] = 0;
ary[1] = 0;
Function(ary);
delete [] ary;

But why are you using dynamically allocated memory here? Is the array held onto outside of the scope of the current function? If not, you can use an array allocated on the stack:

BYTE ary[2] = {0};
Function(ary);

In C++, a preferred method is to use the STL class std::vector which acts like a dynamically allocated (but type safe) array:

std::vector<BYTE> ary(2);
Function(&ary[0]);
我的影子我的梦 2024-07-25 04:02:49
BYTE foo[] = { 0x00, 0x00 };
Function( foo );

C++0x 将引入初始值设定项列表语法,该语法将允许更接近您上面想要的内容。

BYTE foo[] = { 0x00, 0x00 };
Function( foo );

C++0x will introduce initializer list syntax that will allow something closer to what you wanted above.

绝不放开 2024-07-25 04:02:49
#include <windows.h>
#include <iostream>
using namespace std;
void foo(BYTE *d) {
    cout << (int)d[ 0 ] << " " << (int)d[ 1 ] << endl;
}
int main(void) 
{
    foo(new BYTE[ 2 ]());
    return 0;
}

如果您只想将 BYTE 数组初始化为全零,则上述方法有效。 (我假设这是 Windows BYTE 类型。)但是,如上所述,这很容易泄漏,最好避免。

#include <windows.h>
#include <iostream>
using namespace std;
void foo(BYTE *d) {
    cout << (int)d[ 0 ] << " " << (int)d[ 1 ] << endl;
}
int main(void) 
{
    foo(new BYTE[ 2 ]());
    return 0;
}

The above works if all you ever wanted was to initialize the BYTE array to all zeroes. (I am assuming this is the Windows BYTE type.) However this is leak-prone as mentioned and is best avoided.

梦与时光遇 2024-07-25 04:02:49

或者您可以使用省略号来模拟数组的构造:

看一下: http://www.cplusplus.com/reference/clibrary/cstdarg/va_arg/

如果你真的想在 0x 到达之前摆弄,看看 此代码

Or you could use the ellipsis to mimic construction of your array:

take a look at this: http://www.cplusplus.com/reference/clibrary/cstdarg/va_arg/

And if you really want to fiddle before the 0x arrives, take a look at this code.

情栀口红 2024-07-25 04:02:49

gcc 有一个名为“复合文字”的扩展,它允许您编写:

Function((BYTE[]){1, 2, 3, 4});

请注意,它是在堆栈上分配的,因此它可能不适合您的目的。

gcc has an extension called "Compound literals", which allows you to write:

Function((BYTE[]){1, 2, 3, 4});

Note that it is allocated on the stack, so it might not be suitable for your purposes.

零時差 2024-07-25 04:02:49

好吧,如果 BYTE 是一个类,您可以有一个构造函数

BYTE::BYTE(char c1,char c2){
 //something here.
}

,然后调用

Function( new BYTE(0X00,0X00))

但是,这很容易泄漏。 您应该在退出 Function 之前删除该参数。 这并不总是可能的(例如,如果您没有 Function 的源代码)

Well, if BYTE is a Class, you could have a constructor

BYTE::BYTE(char c1,char c2){
 //something here.
}

and then call

Function( new BYTE(0X00,0X00))

However, this is leak-prone. You should delete the argument before exiting Function. And that's not always possible (e.g if you don't have Function's source)

归属感 2024-07-25 04:02:49

辅助功能;

  BYTE* makeNaryByteArray( int n, BYTE exemplar = 0 ) {
    BYTE* r = new BYTE[ n ];
    for( int i = 0 ; i < n ; ++i )
       n[i] = exemplar;
    return r;
  }

  //call them:
  BYTE* myByteArray;
  Function( myByteArray = makeNaryByteArray(2) );

  // done with array:
  delete[] myByteArray;

请记住,使用 new[] 创建的数组将使用 delete[] 删除;

Auxiliary function;

  BYTE* makeNaryByteArray( int n, BYTE exemplar = 0 ) {
    BYTE* r = new BYTE[ n ];
    for( int i = 0 ; i < n ; ++i )
       n[i] = exemplar;
    return r;
  }

  //call them:
  BYTE* myByteArray;
  Function( myByteArray = makeNaryByteArray(2) );

  // done with array:
  delete[] myByteArray;

Just remember, arrays created with new[] are deleted with delete[];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文