我们应该在 return 分配内存指针的函数上使用 delete[] 还是 delete?
Should we use delete[] or delete on function that return allocated memory pointer?
对于下面的 Register() 示例,我应该使用 delete[] 还是 delete?我们遇到了一些内存泄漏问题和遗留代码,这些代码不允许更改函数的 return 类型和输入。我明白 new[] 应该使用 delete[]。 new应该用delete。但是对于下面的情况,哪种方法是正确的?
UINT Register(UINT regaddr, usb_dev_handle* current_handle)
{
unsigned int val= 0;
unsigned char* buffer = readregister(current_handle, char((regaddr >> 8) & 0x00FF), char(regaddr & 0x00FF));
raddrvaluefinal = buffer[2] << 8 | buffer[3];
delete buffer;
return val;
}
unsigned char* readregister(usb_dev_handle *current_handle, char one, char two)
{
unsigned char *buffer = new unsigned char[4];
char cmd[2];
cmd[0] = 'D';
cmd[1] = 'B';
int rv = 0;
unsigned char bin_size[2];
buffer[0] = one + 0x80;
buffer[1] = two;
buffer[2] = buffer[3] = 0;
usb_bulk_write(current_handle, 0x02, cmd, 2, 500);
bin_size[0] = (5 / 4) & 0x0FF;
bin_size[1] = ((5 / 4) >> 8) & 0x0FF;
rv = usb_bulk_write(current_handle, 0x01, (char*)bin_size, 2, 2000);
unsigned int byteswrite += usb_bulk_write(current_handle, 0x04, (char*)buffer, 4, 2000);
unsigned int bytesread += usb_bulk_read(current_handle, 0x86, (char*)buffer, 4, 2000);
buffer[0] = one;
buffer[1] = two;
return buffer;
}
I understand that new[] should use delete[]. new should use delete.
unsigned char *buffer = new unsigned char[4];
...
delete buffer;
程序有误,违反您的理解。当使用 new[]
动态分配动态数组时,必须使用 delete[]
来释放它。如果您使用 delete
,则程序的行为未定义。
首先你不应该使用拥有裸指针,在这种情况下似乎甚至没有必要使用动态数组。我建议改为:
std::array<unsigned char, 4>
readregister(usb_dev_handle *current_handle, char one, char two)
{
std::array<unsigned char, 4> buffer;
// ...
usb_bulk_write(current_handle, 0x04, reinterpret_cast<char*>(buffer.data()), 4, 2000);
旁注:“新建 -> 删除”、“新建[] -> 删除[]”有点过于简单化。如果使用new
创建数组,则必须使用delete[]
:
using T = int[4];
int* ptr = new T;
delete[] ptr; // correct
对于下面的 Register() 示例,我应该使用 delete[] 还是 delete?我们遇到了一些内存泄漏问题和遗留代码,这些代码不允许更改函数的 return 类型和输入。我明白 new[] 应该使用 delete[]。 new应该用delete。但是对于下面的情况,哪种方法是正确的?
UINT Register(UINT regaddr, usb_dev_handle* current_handle)
{
unsigned int val= 0;
unsigned char* buffer = readregister(current_handle, char((regaddr >> 8) & 0x00FF), char(regaddr & 0x00FF));
raddrvaluefinal = buffer[2] << 8 | buffer[3];
delete buffer;
return val;
}
unsigned char* readregister(usb_dev_handle *current_handle, char one, char two)
{
unsigned char *buffer = new unsigned char[4];
char cmd[2];
cmd[0] = 'D';
cmd[1] = 'B';
int rv = 0;
unsigned char bin_size[2];
buffer[0] = one + 0x80;
buffer[1] = two;
buffer[2] = buffer[3] = 0;
usb_bulk_write(current_handle, 0x02, cmd, 2, 500);
bin_size[0] = (5 / 4) & 0x0FF;
bin_size[1] = ((5 / 4) >> 8) & 0x0FF;
rv = usb_bulk_write(current_handle, 0x01, (char*)bin_size, 2, 2000);
unsigned int byteswrite += usb_bulk_write(current_handle, 0x04, (char*)buffer, 4, 2000);
unsigned int bytesread += usb_bulk_read(current_handle, 0x86, (char*)buffer, 4, 2000);
buffer[0] = one;
buffer[1] = two;
return buffer;
}
I understand that new[] should use delete[]. new should use delete.
unsigned char *buffer = new unsigned char[4]; ... delete buffer;
程序有误,违反您的理解。当使用 new[]
动态分配动态数组时,必须使用 delete[]
来释放它。如果您使用 delete
,则程序的行为未定义。
首先你不应该使用拥有裸指针,在这种情况下似乎甚至没有必要使用动态数组。我建议改为:
std::array<unsigned char, 4>
readregister(usb_dev_handle *current_handle, char one, char two)
{
std::array<unsigned char, 4> buffer;
// ...
usb_bulk_write(current_handle, 0x04, reinterpret_cast<char*>(buffer.data()), 4, 2000);
旁注:“新建 -> 删除”、“新建[] -> 删除[]”有点过于简单化。如果使用new
创建数组,则必须使用delete[]
:
using T = int[4];
int* ptr = new T;
delete[] ptr; // correct