{"record":{"id":"158872fe87fc72b7","repo":"Yalantis/uCrop","slug":"cimg-appname-math-parser-cimg-s-function-158872","errorCode":null,"errorMessage":"[\" cimg_appname \"_math_parser] CImg<%s>: Function '%s()': Specified image #%u of size (%d,%d,%d,%d) cannot be used as dynamic array%s.","messagePattern":"\\[\" cimg_appname \"_math_parser\\] CImg<(.+?)>: Function '(.+?)\\(\\)': Specified image #%u of size \\((.+?),(.+?),(.+?),(.+?)\\) cannot be used as dynamic array(.+?)\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":26263,"sourceCode":"      }\n\n      static double mp_cut(_cimg_math_parser& mp) {\n        double val = _mp_arg(2), cmin = _mp_arg(3), cmax = _mp_arg(4);\n        return cimg::cut(val,cmin,cmax);\n      }\n\n      static double mp_da_back_or_pop(_cimg_math_parser& mp) {\n        const bool is_pop_heap = mp.opcode[4]==2, is_pop = (bool)mp.opcode[4];\n        const char *const s_op = is_pop_heap?\"da_pop_heap\":is_pop?\"da_pop\":\"da_back\";\n        mp_check_list(mp,s_op);\n        const unsigned int\n          dim = (unsigned int)mp.opcode[2],\n          ind = (unsigned int)cimg::mod((int)_mp_arg(3),mp.imglist.width());\n        double *const ptrd = &_mp_arg(1) + (dim>1?1:0);\n        CImg<T> &img = mp.imglist[ind];\n        int siz = img?(int)cimg::float2uint((float)img[img._height - 1]):0;\n        if (img && (img._width!=1 || img._depth!=1 || siz<0 || siz>img.height() - 1))\n          throw CImgArgumentException(\"[\" cimg_appname \"_math_parser] CImg<%s>: Function '%s()': \"\n                                      \"Specified image #%u of size (%d,%d,%d,%d) cannot be used as dynamic array%s.\",\n                                      mp.imgout.pixel_type(),s_op,ind,\n                                      img.width(),img.height(),img.depth(),img.spectrum(),\n                                      img._width==1 && img._depth==1?\"\":\" (contains invalid element counter)\");\n        if (!siz)\n          throw CImgArgumentException(\"[\" cimg_appname \"_math_parser] CImg<%s>: Function '%s()': \"\n                                      \"Specified dynamic array #%u contains no elements.\",\n                                      mp.imgout.pixel_type(),s_op,ind);\n        const int siz1 = siz - 1;\n        if (is_pop_heap) { // Heapify-down\n          if (dim==1) cimg::swap(img[0],img[siz1]);\n          else {\n            T *ptr0 = img.data(), *ptr1 = img.data(0,siz1);\n            cimg_forC(img,c) { cimg::swap(*ptr0,*ptr1); ptr0+=img._height; ptr1+=img._height; }\n          }\n          int index = 0;\n          while (true) {\n            const int child_left = 2*index + 1, child_right = child_left + 1;","sourceCodeStart":26245,"sourceCodeEnd":26281,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L26245-L26281","documentation":"Dynamic-array MP functions treat a listed image as a dynamic array: it must be a single-column image (width==1, depth==1) whose last row element stores a valid element counter (0 <= siz <= height-1). When the image shape or its counter is invalid, this error is thrown, appending '(contains invalid element counter)' when the shape is fine but the counter is wrong.","triggerScenarios":"Calling heap functions like 'da_pop'/'minha'-style MP opcodes on an image that is not 1 x H x 1 x S, or whose last-row counter value exceeds height-1 or is negative.","commonSituations":"Using a normal 2D image as a dynamic array without initializing its counter; corrupted array images after bad resize; forgetting to create the array with da_init.","solutions":["Initialize the dynamic array first, e.g. fill(\"da_init(h,dim)\") to set proper shape and counter.","Ensure the image is a single-column (width 1, depth 1) image before array operations.","Reset the element counter stored in the last row to a value <= height-1.","Re-create the array image instead of reusing arbitrary images."],"exampleFix":"// before\nCImg<float> arr(10,10,1,3,0); arr.fill(\"da_pop(0)\");\n// after\nCImg<float> arr(1,11,1,3,0); arr.fill(\"da_init(11,3)\"); arr.fill(\"da_pop(0)\");","handlingStrategy":"validation","validationCode":"bool validDynArray = arr.width()==1 && arr.depth()==1;\nfloat counter = arr ? arr(arr.width()-1, arr.height()-1) : 0.f;\nif (!validDynArray || counter < 0 || counter > arr.height()-1)\n  throw std::runtime_error(\"image is not a valid dynamic array\");","typeGuard":"bool isDynamicArray(const CImg<T>& a) {\n  if (a.is_empty() || a.width()!=1 || a.depth()!=1) return false;\n  double siz = a(a.width()-1, a.height()-1);\n  return siz >= 0 && siz <= a.height()-1;\n}","tryCatchPattern":"try { img.fill(expr); } catch (const cimg_library::CImgArgumentException& e) { /* re-init array with da_init */ }","preventionTips":["Create arrays exclusively via da_init","Never write to the counter row directly","Check width/depth==1 before array ops"],"tags":["cimg","math-parser","dynamic-array","shape-mismatch"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}