Delphi / C中更快的参数传递
发布时间:2020-12-15 09:08:29 所属栏目:大数据 来源:网络整理
导读:我有2个部分的应用程序,Delphi和C,我有2个问题 ?1.将参数传递给Delphi的最快方法是什么? procedure name(Data: string); // Data is Copied to another location before passing so its slowprocedure name(var Data: string); // Data is Passed by pointe
我有2个部分的应用程序,Delphi和C,我有2个问题
?1.将参数传递给Delphi的最快方法是什么? procedure name(Data: string); // Data is Copied to another location before passing so its slow procedure name(var Data: string); // Data is Passed by pointer,Faster procedure name(const Data: string); // unknown >我想通过c中的指针传递参数,我有一个char数组和一个函数,我不想通过整个数组,切掉它的第一部分并通过其余部分 void testfunction(char **Data) { printf("Data = %dn",*Data); return; } int main() { char Data[] = "TEST FUNCTION"; testfunction(&&Data[4]); // Error return 0; } 谢谢 解决方法
Delphi字符串驻留在堆上,并始终通过指针传递.您的第一个按值传递的示例不正确.不会复制按值传递的字符串.仅复制参考.这对于字符串是可行的,因为它们具有魔术写时复制行为.将复制按值传递的动态数组.
传递字符串时使用const具有最佳性能,因为编译器可以优化引用计数代码. 您的C代码有点混淆.你不想要char **,而是你想要char *.请记住,C字符串char *只是指向以null结尾的内存块的指针.您不需要指向C字符串,因为它已经是指针. 你的意思是%s而不是%d肯定.要传递从第5个字符开始的C字符串,请写入数据4. void testfunction(char *Data) { printf("Data = %sn",Data); } int main() { char Data[] = "TEST FUNCTION"; testfunction(Data+4); return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |