gpt4 book ai didi

c++ - 如何正确删除那些指针?

转载 作者:太空宇宙 更新时间:2023-11-04 14:37:48 24 4
gpt4 key购买 nike

这是我的分配:

for (int i = 0; i < numCols; i++)
{
columnNameLen = new SQLSMALLINT *[numCols];
columnDataType = new SQLSMALLINT *[numCols];
columnDataSize = new SQLULEN *[numCols];
columnDataDigits = new SQLSMALLINT *[numCols];
columnDataNullable = new SQLSMALLINT *[numCols];
columnData = new SQLWCHAR *[numCols];
columnDataLen = new SQLLEN *[numCols];
columnName = new SQLWCHAR *[numCols];
}

for (int i = 0; i < numCols; i++)
{
columnNameLen[i] = new SQLSMALLINT;
columnDataType[i] = new SQLSMALLINT;
columnDataSize[i] = new SQLULEN;
columnDataDigits[i] = new SQLSMALLINT;
columnDataNullable[i] = new SQLSMALLINT;
columnDataLen[i] = new SQLLEN;
columnName[i] = new SQLWCHAR[256];
}

这是我的删除:

for (int i = 0; i < numCols; i++)
{
delete columnNameLen[i];
columnNameLen[i] = NULL;
delete columnDataType[i];
columnDataType[i] = NULL;
delete columnDataSize[i];
columnDataSize[i] = NULL;
delete columnDataDigits[i];
columnDataDigits[i] = NULL;
delete columnDataNullable[i];
columnDataNullable[i] = NULL;
delete columnData[i];
columnData[i] = NULL;
delete columnDataLen[i];
columnDataLen[i] = NULL;
delete[] columnName[i];
columnName[i] = NULL;
}

for (int i = 0; i < numCols; i++)
{
delete[] columnNameLen;
columnNameLen = NULL;
delete[] columnDataType;
columnDataType = NULL;
delete[] columnDataSize;
columnDataSize = NULL;
delete[] columnDataDigits;
columnDataDigits = NULL;
delete[] columnDataNullable;
columnDataNullable = NULL;
delete[] columnData;
columnData = NULL;
delete[] columnDataLen;
columnDataLen = NULL;
delete[] columnName;
columnName = NULL;
}

上面的代码可以吗?我可能应该只使用智能指针,但我不知道使用它们后代码会是什么样子?因此,如果您输入智能指针代码,我们将不胜感激。这些指针在线程函数中本地使用,不与任何东西共享。

我担心第二个循环只需要 delete 而不是 delete[]

TIA!

最佳答案

你不应该在这里使用指针根本

using SQLSTRING = std::basic_string<SQLWCHAR>;

struct Column
{
SQLSTRING Name;
SQLSTRING Data;
SQLSMALLINT DataType;
SQLULEN DataSize;
SQLSMALLINT DataDigits;
SQLSMALLINT DataNullable;
}

std::vector<Column> columns(numCols);

关于c++ - 如何正确删除那些指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52730198/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com