简体   繁体   English

如何删除此Void指针?

[英]How do I delete this Void Pointer?

#define ALIGNBUF(Length) Length % ALIGNSIZE ? \
              Length + ALIGNSIZE - (Length % ALIGNSIZE) : Length 

short NumCols;
long * ColLenArray, * OffsetArray;

ColLenArray = new long(NumCols * sizeof(long));
OffsetArray = new long(NumCols * sizeof(long));

// THIS CODE SHOULD NOT BE NEEDED TO UNDERSTAND THE PROBLEM
// BUT I HAVE INCLUDED IT JUST IN CASE
////////////////////////////////////////////////////////////
SQLColAttribute(hstmt, ((SQLUSMALLINT) i)+1, SQL_DESC_OCTET_LENGTH, NULL, 0, NULL, &ColLenArray[i]);
    ColLenArray[i] = ALIGNBUF(ColLenArray[i]);
    if (i)
        OffsetArray[i] = OffsetArray[i-1]+ColLenArray[i-1]+ALIGNBUF(sizeof(SQLINTEGER));
////////////////////////////////////////////////////////////

void **DataPtr = new void*[OffsetArray[NumCols - 1] + ColLenArray[NumCols - 1] + ALIGNBUF(sizeof(long))];

delete []DataPtr;

Don't think it can be done, have tried every way imaginable. 不要以为它可以做到,尝试过各种想象的方式。

This code works, as in the program runs, I just can't deallocate the memory. 这个代码工作,因为在程序运行中,我只是无法释放内存。 Every time this code is called(not all code included as it isn't relevant) the memory gets bigger. 每次调用此代码(不包括所有代码,因为它不相关)时,内存会变大。 I think that deletion is not happening properly and that the void * keeps growing. 我认为删除没有正确发生,并且void *不断增长。

I have also changed some of the code above based on recommendations here, but as this code is, the memory keeps growing. 我也根据这里的建议更改了上面的一些代码,但是正如这段代码所示,内存不断增长。

You can't invoke delete on a void * . 您无法在void *上调用delete

The solution is to not cast a pointer-to- void** (which is what new void*[...] will give you) to void* . 解决方案是不要将指向void**的指针void** (这是new void*[...]将给你的)转换为void* I don't really know what your code is supposed to be doing, but have you tried changing the type of DataPtr to void ** ? 我真的不知道您的代码应该做什么,但您是否尝试将DataPtr的类型更改为void **

More generally, avoid void* as far as possible in C++. 更一般地说,尽可能避免在C ++中使用void* There are better solutions. 有更好的解决方案。 If you edit your question to describe what you're trying to achieve, then we may be able to help suggest something. 如果你编辑你的问题来描述你想要实现的目标,那么我们可以帮助提出建议。

You should try avoid mixing void* and new . 你应该尝试避免混合void*new In C++ actually, new is meant to automatically determine the type of the pointer; 实际上,在C ++中, new意味着自动确定指针的类型; then why should not use it. 那为什么不应该使用它。 At least you can use char* , if you are simply dealing with raw bytes. 如果您只是处理原始字节,至少可以使用char*

Other point is new void*[SIZE] allocates void** . 其他点是new void*[SIZE]分配void** So you should change the declaration to void **DataPtr . 所以你应该将声明改为void **DataPtr Remove typecasting ahead of new . new之前删除类型转换。 You can now delete[] DataPtr; 你现在可以delete[] DataPtr; .

Edit: 编辑:

The code have some problems, the variables should be declared like below: 代码有一些问题,变量应该声明如下:

ColLenArray = new long[NumCols * sizeof(long)]; // declare as long[] (not long())
OffsetArray = new long[NumCols * sizeof(long)];

when you declare those variables as, new long() ; 当你将这些变量声明为new long() ; it will simply initialize the value and assign a pointer to single long . 它只是初始化值并指定一个指向单个 long的指针。

The memory corruption happens because, you are using ColLenArray[i] , which is accessing wrong memory. 发生内存损坏的原因是,您正在使用正在访问错误内存的ColLenArray[i] Since you are going to use above variables as arrays, it should be new long[] . 由于您要将上述变量用作数组,因此它应该是new long[] Then memory corruption will not happen. 然后内存损坏不会发生。 After usage, you should delete[] them. 使用后,您应该delete[]它们。

You just want a block of memory that you can pass to some database library routines? 您只需要一块可以传递给某些数据库库例程的内存块? Allocate thus: 因此分配:

char * buffer = new char[ len ];

len is the length of the buffer in bytes. len是缓冲区的长度,以字节为单位。 To delete, simply do: 要删除,只需执行:

delete [] buffer;

You want a void* to pass to a function? 你想要一个void *传递给一个函数?

void * DataPtr = static_cast< void* >( buffer );

For extra merit points, use boost to manage deletion: 要获得额外的优点,请使用boost来管理删除:

boost::scoped_array< char > buffer( new char[ len ] );

... then you don't have to worry about deletion. ...那么你不必担心删除。 To get the buffer here, you need: 要在此处获取缓冲区,您需要:

void * DataPtr = static_cast< void* >( buffer.get() );

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

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