简体   繁体   English

byte [] receiverByte = new byte [BUFFER];

[英]byte[] receiverByte = new byte[BUFFER];

I need some help with the following line of code. 我需要以下代码行的帮助。 I am writing my code in C and I need some replacement for following line of code in Java. 我在C中编写代码,我需要替换Java中的以下代码行。 So please help me and suggest some alternatives on how I can modify it for my C code. 所以,请帮助我,并提出一些替代方案,我可以为我的C代码修改它。 I am waiting for line of code. 我正在等待代码行。 Thanks.. 谢谢..

byte[] receiverByte = new byte[BUFFER];
byte[] receiverByte = new byte[BUFFER_SIZE ];

becomes this if using a C++ compiler: 如果使用C ++编译器就变成这样:

char* receiverByte = new char[BUFFER_SIZE ];

else in C: C中的其他内容:

char* receiverByte = malloc( BUFFER_SIZE );

Remember though that C is not memory managed like Java so you will need to call delete to free the memory when you are done: 请记住,虽然C不是像Java那样的内存管理,所以在完成后你需要调用delete来释放内存:

So for C++: 所以对于C ++:

delete[] receiverByte;

And for C: 对于C:

free( receiverByte );

Also you could create a fixed size array on the stack. 您还可以在堆栈上创建固定大小的数组。 The size BUFFER_SIZE must be specified at compile time and the vector size cannot change or grow: 必须在编译时指定BUFFER_SIZE大小,并且向量大小不能更改或增长:

char receiverByte[BUFFER_SIZE];

EDIT: If you are using a c++ compiler you also have access to the STL library which removes the need to directly use new[] and delete[]: 编辑:如果您使用的是c ++编译器,您还可以访问STL库,无需直接使用new []和delete []:

#include <vector>
...
std::vector<char> receiverByte;
receiverByte.resize( BUFFER_SIZE );

If BUFFER is defined as a macro: 如果将BUFFER定义为宏:

#define BUFFER 1234
unsigned char receiveBuffer[BUFFER];

or using a dynamic allocation with malloc() : 或使用malloc()的动态分配:

#include <stdlib.h>
unsigned char *receiveBuffer = malloc(BUFFER);

As mentioned by @mic_e you should multiply BUFFER with the size of a unsigned char to be more platform independent, the second example becomes: 正如@mic_e所提到的,你应该将BUFFERunsigned char的大小相乘以更加独立于平台,第二个例子变为:

unsigned char *receiveBuffer = malloc(BUFFER * sizeof(unsigned char));

the same goes for the first example. 第一个例子也是如此。

unsigned char receiverByte[BUFFER];

or 要么

unsigned char* receiverByte = malloc(BUFFER);
if (!receiverByte ) my_do_with_error(....);

And: 和:

double data[19][4];

But a dynamic array here will be more tricky (test for NULL) 但是这里的动态数组会比较棘手(测试NULL)

int M= 19, N= 4;
double **data   =malloc(sizeof( *data)*M);
         data[0]=malloc(sizeof(**data)*M*N);
for(int i=1; i<M; ++i) data[i] =data[0]+i*N;

..... .....

free(data[0]);
free(data);

EDIT: In 6.5.3.4 of the C standard we can find : 编辑: 在C标准的6.5.3.4中,我们可以找到

#include <stddef.h>
size_t fsize3(int n)
{
  char b[n+3];     // variable length array
  return sizeof b; // execution time sizeof
}
int main()
{
  size_t size;
  size = fsize3(10); // fsize3 returns 13
  return 0;
}

I would suggest to use unsigned char* as it resembles a byte in C. So it will be: 我建议使用unsigned char*因为它类似于C中的一个字节。所以它将是:

unsigned char* receiverByte = malloc(BUFFER * sizeof(unsigned char));

or 要么

unsigned char receiverByte[BUFFER];

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

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