离开函数后,我的数组为空

时间:2011-05-02 15:35:55

标签: c++ arrays pointers

在上一个问题(changing value from function)中,我得到了帮助,这引出了我的下一个问题:

我使用字符串拆分然后使用简单的副本

int Crawl :: splitUrl(char ***arr, int max_length, char *url)
{
   int idx=0;
   char * p;
   int i;

  p = strtok (url,"/"); // call the strtok with str as 1st arg for the 1st time.
    while (p != NULL && idx < max_length)
    {
       for (i=0;i<maxUrlSize-1 && p[i] != '\0';i++)
          (*arr)[idx][i] = p[i];
       for ( ; i< maxUrlSize-1;i++)
          (*arr)[idx][i] = '\0';

        printf("tmp[idx[%d]] %s  %d addr: %x\n",idx,(*arr)[idx],strlen(p),(*arr)[idx]);

        idx++;
        p = strtok (NULL, "/");
    }

分配数组:

 split_url = new char * [ maxUrlSplit ];
 printf("split_url %x\n",split_url);
 for (i=0;i<maxUrlSplit;i++)
  {
     split_url[ i ] = new char [ maxUrlSize ];
     printf("split_url[ %d ] %x\n",i,split_url[i]);
  }

在函数之后我使用循环来获取arry中的所有元素。我打印它像

printf("add: %x %s %d\n",split_url[iarr], split_url[iarr], strlen(split_url[iarr]));

地址总是一样的,但在函数运行后没有条目?

我运行像

这样的功能
  int arr_size = crawl->splitUrl(&split_url,maxUrlSplit,url);

GDB和Valgrind没有说什么。

2 个答案:

答案 0 :(得分:0)

在我使代码可编辑后,它工作得很好,至少在它输出预期结果的意义上。我对风格或安全性没有评论:

const int maxUrlSplit = 10;
const int maxUrlSize = 64;


int splitUrl(char ***arr, int max_length, char *url)
{
   int idx=0;
   char * p;
   int i;

  p = strtok (url,"/"); // call the strtok with str as 1st arg for the 1st time.
    while (p != NULL && idx < max_length)
    {
       for (i=0;i<maxUrlSize-1 && p[i] != '\0';i++)
          (*arr)[idx][i] = p[i];
       for ( ; i< maxUrlSize-1;i++)
          (*arr)[idx][i] = '\0';

        printf("tmp[idx[%d]] %s  %d addr: %x\n",idx,(*arr)[idx],strlen(p),(*arr)[idx]);

        idx++;
        p = strtok (NULL, "/");
    }
    return idx;
}

int main()
{
    char url[] = "ant/bison/chimp";

    int i;
    char **split_url = new char * [ maxUrlSplit ];
    printf("split_url %x\n",split_url);
    for (i=0;i<maxUrlSplit;i++)
    {
        split_url[ i ] = new char [ maxUrlSize ];
        printf("split_url[ %d ] %x\n",i,split_url[i]);
    }
    int arr_size = splitUrl(&split_url,maxUrlSplit,url);

    for (i = 0; i < arr_size; i++)
    {
        printf("add: %x %s %d\n",split_url[i], split_url[i], strlen(split_url[i]));
    }
}

输出:

split_url 2f1490
split_url[ 0 ] 2f14e8
split_url[ 1 ] 2f1558
split_url[ 2 ] 2f15c8
split_url[ 3 ] 2f1638
split_url[ 4 ] 2f16a8
split_url[ 5 ] 2f1718
split_url[ 6 ] 2f1788
split_url[ 7 ] 2f17f8
split_url[ 8 ] 2f1868
split_url[ 9 ] 2f18d8
tmp[idx[0]] ant  3 addr: 2f14e8
tmp[idx[1]] bison  5 addr: 2f1558
tmp[idx[2]] chimp  5 addr: 2f15c8
*************
add: 2f14e8 ant 3
add: 2f1558 bison 5
add: 2f15c8 chimp 5

因此,您必须在代码的其他部分遇到其他问题。

答案 1 :(得分:0)

好的,明白了: 使用Boost Tokenizer或标准库,如another so question中详细说明的那样。

尝试修复那里的内容:

// pass in max_length as reference, or pointer, so you can change it.
// no three stars! You are passing in an adress, the extra indirection
// is not necessary (at least as far as I see it...

int Crawl :: splitUrl(char **arr, int & max_length, char *url)
{
   int idx=0;
   char * p;
   int i;

   p = strtok (url,"/"); // call the strtok with str as 1st arg for the 1st time.
   while (p != NULL && idx < max_length)
   {
     strcopy(arr[idx], p);
     // note you could also do if you absolutely KNOW that you won't deallocate url!
     // arr[idx] = p;
     idx++;
     p = strtok (NULL, "/");
   }
   max_length = idx; // no filling the rest with empty strings....
 }