You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

寻求MATLAB结构体数组a、b的MEX等价实现方案(含内存差异重现)

Replicating Memory-Different Identical Structs in MEX

Got it, let's break this down. First, the key reason two MATLAB structs with identical content can have massive memory differences is almost always shared underlying data vs. fully copied data. MATLAB uses copy-on-write by default, but if you explicitly duplicate data (or trigger a copy via modification), you end up with two separate memory blocks. Let's build a MEX function that creates both types of struct and returns them to your workspace.

First, Understand the MATLAB Context

In MATLAB, this scenario might look like this:

% Create a large array
big_data = rand(1e6, 1);

% Struct a: shares memory with big_data (shallow reference)
a.data = big_data;

% Struct b: forces a full copy of big_data
b.data = big_data;
b.data(1) = b.data(1); % Triggers copy-on-write

Running whos a b would show a using minimal extra memory (since it just references big_data), while b uses ~8MB more (the size of the copied array).

MEX Implementation to Replicate This

Here's a complete MEX function that creates both structs—one with shared data, one with a full copy:

#include "mex.h"

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    // 1. Create a large underlying array (1 million double elements)
    const mwSize num_elements = 1000000;
    mxArray *large_array = mxCreateDoubleMatrix(num_elements, 1, mxREAL);
    double *array_ptr = mxGetPr(large_array);
    
    // Fill the array with dummy values (so it has meaningful content)
    for (mwSize i = 0; i < num_elements; i++) {
        array_ptr[i] = (double)i / 1000;
    }
    
    // Define struct field name
    const char *field_names[] = {"data"};
    
    // 2. Create struct A: uses a shallow reference to the original array
    mxArray *struct_a = mxCreateStructMatrix(1, 1, 1, field_names);
    mxSetField(struct_a, 0, "data", large_array); 
    // Note: mxSetField transfers ownership of large_array to struct_a—no copy here
    
    // 3. Create struct B: makes a full deep copy of the large array
    mxArray *array_copy = mxDuplicateArray(large_array);
    mxArray *struct_b = mxCreateStructMatrix(1, 1, 1, field_names);
    mxSetField(struct_b, 0, "data", array_copy);
    
    // 4. Return both structs to MATLAB workspace
    plhs[0] = struct_a;
    plhs[1] = struct_b;
    
    // No manual memory cleanup needed: MATLAB manages struct-owned arrays
}

How It Works

  • Shallow Reference (Struct A): When we use mxSetField to attach large_array directly to struct_a, we're just passing a pointer to the existing memory block. No new memory is allocated for the data field—struct A only stores a reference to the original array.
  • Full Copy (Struct B): mxDuplicateArray creates an exact copy of large_array in a new memory block. Attaching this copy to struct_b means it has its own independent data, leading to the large memory difference.

Test It in MATLAB

  1. Save the code as createMemDiffStructs.c
  2. Compile it with mex createMemDiffStructs.c
  3. Run:
    [a, b] = createMemDiffStructs;
    whos a b
    

You'll see output like this (the exact numbers depend on your system):

Name      Size            Bytes  Class     Attributes

  a         1x1              112  struct              
  b         1x1            8000112  struct              

Notice b uses ~8MB more memory—exactly the size of the copied 1M-element double array.

Bonus: What If It's Packed vs. Unpacked Structs?

If your original MATLAB structs use different packing (e.g., struct vs. packedstruct for C-compatible memory layout), you can adjust the MEX code to create packed structs with mxCreateStructMatrixWithTag using the 'Packed' tag. But the shared/copied data scenario is far more likely to cause the massive memory difference you're seeing.

内容的提问来源于stack exchange,提问作者brneuro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:02:38