Delphi/C中更快的参数传递

kil*_*ode 3 c delphi parameters parameter-passing

我有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
Run Code Online (Sandbox Code Playgroud)
  1. 我想通过c中的指针传递参数,我有一个char数组和一个函数,我不想通过整个数组,切掉它的第一部分并通过其余部分

    void testfunction(char **Data)
    {
        printf("Data = %d\n", *Data);
        return;
    }
    
    int main()
    {
        char Data[] = "TEST FUNCTION";
        testfunction(&&Data[4]);        // Error
        return 0;
    }
    
    Run Code Online (Sandbox Code Playgroud)

谢谢

Dav*_*nan 6

Delphi字符串驻留在堆上,并始终通过指针传递.您的第一个按值传递的示例不正确.不会复制按值传递的字符串.仅复制参考.这对于字符串是可行的,因为它们具有魔术写时复制行为.将复制按值传递的动态数组.

传递字符串时使用const具有最佳性能,因为编译器可以优化引用计数代码.

您的C代码有点混淆.你不想要char**,而是你想要的char*.请记住,C字符串a char*只是指向以null结尾的内存块的指针.您不需要指向C字符串,因为它已经是指针.

而你的意思%s并非%d确定.从第5个字符开始传递C字符串Data+4.

 void testfunction(char *Data) {
     printf("Data = %s\n", Data);
 }

int main() {
    char Data[] = "TEST FUNCTION";
    testfunction(Data+4);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)