是什么让这两个指针不同?

时间:2015-02-12 04:32:00

标签: c pointers

我有一个C程序

我想要做的是将pApList复制到pApListCp

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错了?

1 个答案:

答案 0 :(得分:1)

问题可能是由于free(temp);造成的。你还没有在这里分配任何内存,为什么要免费?

作为本地数组的

temp将存储在堆栈空间中,编译器在函数退出后重用堆栈。