开发者

After leaving the function my array is empty

In a previous question (changing value from function) I got help, which lead me to the next problem:

I used a string split and then a simple copy

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, "/");
    }

The array is allocated:

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

and after the function i used a loop to get all elements in the arry. i printet it like

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

The address is always the same, but after the function runs there are no entries?

I run the function like

  int arr_size = crawl->splitUrl(&split_url,maxUrlSplit,url);

GDB and Valgrind don't say anything.


After I made your code compilable, it worked "fine", at least in the sense that it outputs the expected result. I make no comment on style or safety:

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]));
    }
}

Output:

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

Therefore, you must have some other issue in some other part of your code.


Ok, to get the obvious out of the way: Use Boost Tokenizer or the standard lib as beautifully explained in another so question.

Trying to fix what is there:

// 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....
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜