Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
96 views
in Technique[技术] by (71.8m points)

VectorSP::getDoubleBuffer返回的buf在delete后有问题

请问一下,DolphinDB C++ API中,这个buf数组和VectorSP里数据是什么关系?我们发现如果在用完buf数组后delete buf;for循环中第二次跑 result_test = conn.run(script); 这句话就会有问题。
image.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

在dolphindb中,Vector有两种存储模式,一种是FastVector模式,数据存储在连续的内存块中;另一种是Big array模式,数据分段存储在多个不连续的内存中。一般而言,当Vector的大小超过1048576时,Vector会切换到Big array模式。

getDoubleBuffer方法一般情况下会直接返回内部地址,只有在区间[start, start + len)跨越Big array的内存交界处时,才会将数据拷贝至用户传入的buffer。而setDouble方法会判断传入的buffer地址是否为内部存储的地址,如果是则直接返回,否则进行内存拷贝。

因此,上面代码中delete getDoubleBuffer返回的buffer,就可能破坏Vector内部的存储。因此推荐的用法是:

// 获取buffer后批量更新
double buf[1024];
double* p = long_ret->getIntBuffer(0, 1024, buf);
// p[0] = ...
long_ret->setDouble(0, 1024, p);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...