mxDestroyArray 是否可以正确地释放重新分配的矩阵或大小已更改的矩阵?
Will mxDestroyArray free reallocated matrix or matrix with changed size correctly?
问题 1:
mxArray *data = mxCreateUninitNumericMatrix(1, 10, mxDOUBLE_CLASS, mxREAL);
mxSetN(data, 0);
mxDestroyArray(data);
mxDestroyArray 会释放 10 个元素还是 0 个元素?
问题 2:
mxArray *data = mxCreateUninitNumericMatrix(1, 10, mxDOUBLE_CLASS, mxREAL);
double *ptr = mxGetPr(data);
ptr = static_cast<double*>(mxRealloc(ptr, sizeof(double) * 20));
mxSetPr(data, ptr);
mxDestroyArray(data);
mxDestroyArray 会释放 10 个元素还是 20 个元素?
谢谢,
关于问题 1:至少有 10 个元素将被释放。 Say docs for mxSetN
:
You typically use mxSetN
to change the shape of an existing mxArray
. The mxSetN
function does not allocate or deallocate any space for the pr
, pi
, ir
, or jc
arrays. So, if your calls to mxSetN
and mxSetM
increase the number of elements
in the mxArray
, enlarge the pr
, pi
, ir
, and/or jc
arrays.
关于Q2:
在 docs for mxDestroyArray
中,它明确表示
mxDestroyArray
deallocates the memory occupied by the specified mxArray
including:
- Characteristics fields of the
mxArray
, such as size (m and n) and type.
- Associated data arrays, such as
pr
and pi
for complex arrays, and ir
and jc
for sparse arrays.
因此它将释放分配给 ptr
的所有 sizeof(double) * 20
字节。
@zeeMonkeez 是正确的,我只是想稍微扩展一下答案。
- 所有 10 个元素都被释放
- 所有 20 个元素都被释放
换句话说,两种情况下都没有内存泄漏。
无论存储在头中的大小(M 和 N)如何,当数组被销毁时,将其视为free
'ing 堆分配内存,它只知道数据的大小。
如果您将创建的数组返回给 MATLAB (plhs[0] = data;
) 而不是销毁它们,结果是您浪费了 space,因为您会告诉 MATLAB 这些数组具有比实际为数据分配的大小更小(在第一种情况下为 0 而不是 10,在第二种情况下为 10 而不是 20)。但是,当数组被销毁时,内存不会泄漏。只要矩阵在 MATLAB 的范围内,就会保留额外的内存但您无法访问它,只有当变量为 cleared/destroyed.
时才会回收它
问题 1:
mxArray *data = mxCreateUninitNumericMatrix(1, 10, mxDOUBLE_CLASS, mxREAL);
mxSetN(data, 0);
mxDestroyArray(data);
mxDestroyArray 会释放 10 个元素还是 0 个元素?
问题 2:
mxArray *data = mxCreateUninitNumericMatrix(1, 10, mxDOUBLE_CLASS, mxREAL);
double *ptr = mxGetPr(data);
ptr = static_cast<double*>(mxRealloc(ptr, sizeof(double) * 20));
mxSetPr(data, ptr);
mxDestroyArray(data);
mxDestroyArray 会释放 10 个元素还是 20 个元素?
谢谢,
关于问题 1:至少有 10 个元素将被释放。 Say docs for mxSetN
:
You typically use
mxSetN
to change the shape of an existingmxArray
. ThemxSetN
function does not allocate or deallocate any space for thepr
,pi
,ir
, orjc
arrays. So, if your calls tomxSetN
andmxSetM
increase the number of elements in themxArray
, enlarge thepr
,pi
,ir
, and/orjc
arrays.
关于Q2:
在 docs for mxDestroyArray
中,它明确表示
mxDestroyArray
deallocates the memory occupied by the specifiedmxArray
including:
- Characteristics fields of the
mxArray
, such as size (m and n) and type.- Associated data arrays, such as
pr
andpi
for complex arrays, andir
andjc
for sparse arrays.
因此它将释放分配给 ptr
的所有 sizeof(double) * 20
字节。
@zeeMonkeez 是正确的,我只是想稍微扩展一下答案。
- 所有 10 个元素都被释放
- 所有 20 个元素都被释放
换句话说,两种情况下都没有内存泄漏。
无论存储在头中的大小(M 和 N)如何,当数组被销毁时,将其视为free
'ing 堆分配内存,它只知道数据的大小。
如果您将创建的数组返回给 MATLAB (plhs[0] = data;
) 而不是销毁它们,结果是您浪费了 space,因为您会告诉 MATLAB 这些数组具有比实际为数据分配的大小更小(在第一种情况下为 0 而不是 10,在第二种情况下为 10 而不是 20)。但是,当数组被销毁时,内存不会泄漏。只要矩阵在 MATLAB 的范围内,就会保留额外的内存但您无法访问它,只有当变量为 cleared/destroyed.