这两个指针有什么不同?

问题描述 投票:0回答:1

我有一个C语言的程序

我想做的是复制 pApListpApListCp

ScanResult *pApListCp;
void ApListCallback(ScanResult *pApList)
{
    ap_num = pApList->ApNum;
    printf("ApListCallback,ap_num=%i\r\n",ap_num);
    for(i=0;i<ap_num;i++){
        char * ssidc = pApList->ApList[i].ssid;
        char temp[500] = "";
        strcpy(temp, ssidc);
        printf("ap cb,ssid=%s\r\n",temp);
        free(temp);
    }
    //memcpy(&hugebuf[sizeof(mxchip_cmd_head_t)-1], pApList->ApList, ap_num*sizeof(ApList_str));
    pApListCp=pApList;
    startScan = 0;
}

我也打印了两次。

case CMD_SCAN:

        printf("4 scan \r\n");
        mxchipStartScan();
        startScan = 1;

        while(startScan == 1){
          mxchipTick();
        }

        ap_num = pApListCp->ApNum;
        printf("\r\ncmd scan,ap_num=%i\r\n",ap_num);
        for(i=0;i<ap_num;i++){
            char * ssidc = pApListCp->ApList[i].ssid;
            char temp[500] = "";
            strcpy(temp, ssidc);
            printf("cmd scan,ssid=%s\r\n",temp);
            free(temp);
        }
        memcpy(p_reply->data, pApListCp->ApList, ap_num*sizeof(ApList_str));
            //memcpy((char * ) & p_reply->data[CONFIG_DATA_SIZE], pApListCp->ApList, ap_num*sizeof(ApList_str));

        p_reply->datalen = ap_num*sizeof(ApList_str);
        break;

但似乎两个结果有差别。

现在的结果看起来像。

4 scan 
ApListCallback,ap_num=16
ap cb,ssid=Modim fashion
ap cb,ssid=timelessgz2
ap cb,ssid=
ap cb,ssid=timelessgz1
ap cb,ssid=sweet baby
ap cb,ssid=sweet baby 2
ap cb,ssid=JIMMY 3G
ap cb,ssid=ringierguest
ap cb,ssid=wudiuandiuan
ap cb,ssid=ChinaNet-GPi6
ap cb,ssid=Bert-Co
ap cb,ssid=TP-LINK_CBD04C
ap cb,ssid=ChinaNet-Ul6
ap cb,ssid=ZOC-TC5
ap cb,ssid=VAIO-VAIO_Network
ap cb,ssid=blue1

cmd scan,ap_num=16
cmd scan,ssid=屨 m fashion
cmd scan,ssid=timelessgz2
cmd scan,ssid=
cmd scan,ssid=timelessgz1
cmd scan,ssid=sweet baby
cmd scan,ssid=sweet baby 2
cmd scan,ssid=JIMMY 3G
cmd scan,ssid=ringierguest
cmd scan,ssid=wudiuandiuan
cmd scan,ssid=ChinaNet-GPi6
cmd scan,ssid=Bert-Co
cmd scan,ssid=TP-LINK_CBD04C
cmd scan,ssid=ChinaNet-Ul6
cmd scan,ssid=ZOC-TC5
cmd scan,ssid=VAIO-VAIO_Network
cmd scan,ssid=blue1

是什么让第一项 Modim fashion 错了吗?

c pointers
1个回答
1
投票

这个问题可能是由于以下原因造成的 free(temp);.你在这里没有分配任何内存,那为什么要释放?

temp 作为一个本地数组,它将存储在堆栈空间中,在函数退出后,编译器将重新使用堆栈。

© www.soinside.com 2019 - 2024. All rights reserved.