为什么strdup和strcat组合在linux android ndk中不起作用

时间:2015-03-30 13:08:54

标签: android linux string visual-studio-2012 android-ndk

请考虑以下代码。手头的任务是连接"小学"和" _Secondary._tcp.local"进入" Primary._Secondary._tcp.local"

fqdn_global的值应为Primary._Secondary._tcp.local 我试过以下代码。但输出并不像预期的那样。

char *serviceName ;//= "_TestImpl._tcp.local";
char *fqdn_global ;//= "MyTestPc._TestImpl._tcp.local";
char *targetName  ;//= "MyTestPc.local";
jint Java_com_example_basicndk1_Ndk1Activity_sendAnnouncement(JNIEnv* env,
        jobject javaThis,jstring lServiceType,jstring lServiceName) {
    char *implName,*implNameWithDot;
    r = 0;
    serviceName = (*env)->GetStringUTFChars(env, lServiceType,NULL);
    LOGI("serviceName %s",serviceName);
    implName = (*env)->GetStringUTFChars(env, lServiceName,NULL);
    LOGI("implName %s",implName);
    implNameWithDot = strdup(implName);
    LOGI("implNameWithDot %s @%p",implNameWithDot,implNameWithDot);
    strcat(implNameWithDot,".");
    LOGI("implNameWithDot %s @%p",implNameWithDot,implNameWithDot);
    fqdn_global = strdup(implNameWithDot);
    LOGI("fqdn_global %s",fqdn_global);
    strcat(fqdn_global,serviceName);
    LOGI("fqdn_global %s",fqdn_global);
    targetName = strdup(implName);
    LOGI("fqdn_global %s",fqdn_global);
    strcat(targetName,".local");
//  LOGI("fqdn_global %s",fqdn_global);
    LOGI("targetName %s",targetName);
//  LOGI("fqdn_global %s",fqdn_global);
    LOGI("announcing \n%s\n%s\n%s",fqdn_global,serviceName,targetName);
    LOGI("mdns_resp_announce returned %d",r);
err:
    (*env)->ReleaseStringUTFChars(env, lServiceType, serviceName);
    (*env)->ReleaseStringUTFChars(env, lServiceName, implName);
}

它的输出是

03-30 18:30:10.494: I/compat.h(18392): serviceName _Sec._tcp.local
03-30 18:30:10.494: I/compat.h(18392): implName Pri
03-30 18:30:10.494: I/compat.h(18392): implNameWithDot Pri @0x588d8208
03-30 18:30:10.494: I/compat.h(18392): implNameWithDot Pri. @0x588d8208
03-30 18:30:10.494: I/compat.h(18392): fqdn_global Pri.
03-30 18:30:10.494: I/compat.h(18392): fqdn_global Pri._Sec._tcp.local
03-30 18:30:10.494: I/compat.h(18392): fqdn_global Pri._Sec._tc
03-30 18:30:10.494: I/compat.h(18392): targetName Pri.local
03-30 18:30:10.494: I/compat.h(18392): announcing 
03-30 18:30:10.494: I/compat.h(18392): Pri._Sec._tc
03-30 18:30:10.494: I/compat.h(18392): _Sec._tcp.local
03-30 18:30:10.494: I/compat.h(18392): Pri.local
03-30 18:30:10.494: I/compat.h(18392): mdns_resp_announce returned 0

类似地,我在Cygwin GNU C中尝试了同样的问题

#include <stdio.h>
#include <string.h>
//typedef char uint8_t;
#define LOGE printf
#define LOGI LOGE
int r;
char *serviceName ;//= "_TestImpl._tcp.local";
char *fqdn_global ;//= "MyTestPc._TestImpl._tcp.local";
char *targetName  ;//= "MyTestPc.local";
int main(int argc, char * argv[]){
    char *implName,*implNameWithDot;
    r = 0;
    serviceName = argv[1];//(*env)->GetStringUTFChars(env, lServiceType,NULL);
    LOGI("serviceName %s\n",serviceName);
    implName = argv[2];//(*env)->GetStringUTFChars(env, lServiceName,NULL);
    LOGI("implName %s\n",implName);
    implNameWithDot = strdup(implName);
    LOGI("implNameWithDot %s @%p\n",implNameWithDot,implNameWithDot);
    strcat(implNameWithDot,".");
    LOGI("implNameWithDot %s @%p\n",implNameWithDot,implNameWithDot);
    fqdn_global = strdup(implNameWithDot);
    LOGI("fqdn_global %s\n",fqdn_global);
    strcat(fqdn_global,serviceName);
    LOGI("fqdn_global %s\n",fqdn_global);
    targetName = strdup(implName);
    LOGI("fqdn_global %s\n",fqdn_global);
    strcat(targetName,".local");
//  LOGI("fqdn_global %s",fqdn_global);
    LOGI("targetName %s\n",targetName);
//  LOGI("fqdn_global %s",fqdn_global);
    LOGI("announcing \n%s\n%s\n%s",fqdn_global,serviceName,targetName);

}

输出

serviceName _Secondary._tcp.local
implName Primary
implNameWithDot Primary @0xf58270
implNameWithDot Primary. @0xf58270
fqdn_global Primary.
fqdn_global Primary._Secondary._tcp.local
fqdn_global Primary._Sec
targetName Primary.local
announcing 
Primary._Sec
_Secondary._tcp.local
Primary.local

但是上面的代码可以按预期在Visual Studio 2012编译器中运行。

0 个答案:

没有答案