下面的c++版本new std::byte[sizeof(data)]
在纯C中的等效是什么?
我试图在纯C (Windows 10)中创建一个系统字节缓冲区,以便我可以通过IOCTL传递一个void指针,以便与内核驱动程序通信。
问题是我看的例子是用c++写的,我正试图弄清楚如何用普通C写它。
下面是我试图转换的c++代码的一个例子:
std::byte* buffer;
try {
buffer = new std::byte[sizeof(data)];
}
catch (const std::exception& e) {
throw gcnew InteropException(e);
}
// Fill the buffer here
try {
write(buffer); // Buffer is passed in as a void* aka PVOID
delete[] buffer;
}
catch (const std::exception& e) {
delete[] buffer;
throw gcnew InteropException(e);
}
下面的c++版本
new std::byte[sizeof(data)]
在纯C中的等效是什么?
使用malloc()
/free()
,例如:
unsigned char *buffer = malloc(sizeof(data));
if (buffer == NULL) {
// handle error as needed...
return;
}
// Fill the buffer here
write(buffer);
// error handling as needed...
free(buffer);
std::byte
:
enum class byte : unsigned char {} ; // (since C++17)
,
typedef unsigned char byte;
或
typedef enum byte : unsigned char {} byte; // (since C23)