gpt4 book ai didi

c++ - mxDestroyArray 是否可以正确地释放重新分配的矩阵或大小已更改的矩阵?

转载 作者:太空狗 更新时间:2023-10-29 21:16:07 32 4
gpt4 key购买 nike

问题一:

mxArray *data = mxCreateUninitNumericMatrix(1, 10, mxDOUBLE_CLASS, mxREAL);
mxSetN(data, 0);
mxDestroyArray(data);

mxDestroyArray 会释放 10 个元素还是 0 个元素?

问题二:

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 个元素?

谢谢,

最佳答案

关于Q1:至少有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.

关于第二季度:在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 字节。

关于c++ - mxDestroyArray 是否可以正确地释放重新分配的矩阵或大小已更改的矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36026076/

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