开发者

Why SWIG requires helper functions to deal with array?

开发者 https://www.devze.com 2023-03-16 03:11 出处:网络
Quoted from here: Ideally, a user might want to pass Perl arrays as arguments as follows: @a = (10,20,30,40);

Quoted from here:

Ideally, a user might want to pass Perl arrays as arguments as follows:

@a = (10,20,30,40);
@b = (50,70,60,200);
gd::plotpts($im,\@a,\@b,4,1); # Error!

However, this script generates a type error instead of acting as one might expect. While such behavior may seem restrictive or bizarre, SWIG has been deliberately designed to operate in this manner. In fact, there are even benefits to this approach. If Perl arrays were to be used as C arrays, a copy would be made, verified for type-correctness, and deallocated every time an array was passed to a C function. For large arrays, this would introduce a substantial performance overhead. Space requirements are also a concern for some C programs. For example, a numerical application might manipulate arrays with millions of elements. Converting such arrays to and from a Perl representation would clearly introduce substantial memory and performance overhead. In contrast, manipulating pointers to such arrays is easy and efficient.

But I don't quite understand why it's wrong开发者_开发百科,can someone illustrate in more detail!?


C arrays are very different from Perl arrays. A Perl array is really a C structure combined with some malloced memory. You cannot just simple use a Perl array as a C array. So something needs to convert the Perl array into a C array and possible back again. This is not necessarily straight forward. Therefore swig forces you to write the code to do it. That is what the helper functions do.

0

精彩评论

暂无评论...
验证码 换一张
取 消