From: Robert on 28 Mar 2010 14:03 Hi. This should be easy, but I'm still getting used to mex memory management. I have a mex function in which a vector will grow dynamically to some unknown length. The resulting vector is my LHS. I'd like to use the mex memory management tools to allocate/free the memory without leaks. As a toy example, how would I implement this loop: i=0; while(rand()<C){ x[i]=foo(i); i++; } where x (of type *double, say) is a LHS variable for the mex function? To speed things up, I may want to allocate a large block for x, then run the loop until I run out of space, then allocate another block, etc, until the loop terminates (instead of allocating two bytes in each iteration of the loop). That would require chopping the leftover from the last block after the loop terminates. I'm not sure how to do that with mex memory allocation either.
From: James Tursa on 28 Mar 2010 14:48 "Robert " <llmmnnoopp(a)hotmail.com> wrote in message <hoo5kp$26e$1(a)fred.mathworks.com>... > Hi. This should be easy, but I'm still getting used to mex memory management. > > I have a mex function in which a vector will grow dynamically to some unknown length. The resulting vector is my LHS. I'd like to use the mex memory management tools to allocate/free the memory without leaks. > > As a toy example, how would I implement this loop: > i=0; > while(rand()<C){ > x[i]=foo(i); > i++; > } > where x (of type *double, say) is a LHS variable for the mex function? > > To speed things up, I may want to allocate a large block for x, then run the loop until I run out of space, then allocate another block, etc, until the loop terminates (instead of allocating two bytes in each iteration of the loop). That would require chopping the leftover from the last block after the loop terminates. I'm not sure how to do that with mex memory allocation either. You chop it off with the same function that you use to do the incremental expanding ... with the mxRealloc function. James Tursa
|
Pages: 1 Prev: how to convert a string to hexa decimal value Next: Accelerator |