2016-09-19 64 views
0

我试图编程一个拆分,它需要一个字符数组与多个单词并将每个单词分隔成自己的较小的字符数组。所有较小char数组的指针都保存在一个指针数组中,因此我可以返回一个双指针。C - 拆分减慢我的电脑

你可以看看我的代码,看看你是否看到任何错误。当我尝试运行我的程序时,我的电脑逐渐变慢,3-4秒后我无法移动我的鼠标或alt + f4我的编辑器。所以有些事情必须严重错误!

另外我完全不熟悉C编程,所以我肯定会在那里犯一个愚蠢的错误。

char **split(char *s) { 

char **result; 
int wrd_cnt = 2; //I'm adding NULL at the end of the pointer-array. 


//Counts the number of words to allocate memory for the pointer-array. 
for(int i = 0; i < strlen(s); i++) { 
    if(s[i] == ' ') { 
     wrd_cnt++; 
    } 
} 
result = malloc(wrd_cnt * sizeof(char*)); 

//Counts letters in each word to allocate memory for every single small char-array with malloc. 
for(int i = 0; i < strlen(s); i++) { 
    for(int j = 0; j < (wrd_cnt); j++) { 
     int char_cnt = 0; 
     for(int k = 0; s[i] != ' ' || s[i] != '\0'; k++, i++) { 
      char_cnt++; 
      result[j] = malloc(char_cnt * sizeof(char)); 
     } 
    } 
} 

//Puts each word into their own place in the pointer array. 
for(int i = 0; i < strlen(s); i++) { 
    for(int j = 0; j < (wrd_cnt); j++) { 
     for(int k = 0; s[i] != ' ' || s[i] != '\0'; k++, i++) { 
      result[j][k] = s[i]; 
     } 
    } 
} 

result[wrd_cnt-1] = NULL; 
return result; 
} 
+1

's [i]!=''|| s [i]!='\ 0''应该是's [i]!=''&& s [i]!='\ 0'',否则你会超出字符串边界。 –

+0

\ t和\ v应该被认为是 – rak007

+0

@AndreaBiondo这不是说s [i]的条件是''和'\ 0'满足条件吗?这是不可能的? – Magnarok

回答

1

在这种情况下使用J和K的循环可以被删除,而不是增量和复位i,j和char_cnt基于i循环作为S数组处理,类似于你已经在wrd_cnt做了什么第一圈

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 

char **split(char *s); 

int main (void) { 
    char **output = NULL; 
    int each = 0; 

    char line[99] = " string to parse for words "; 
    output = split (line); 

    each = 0; 
    while (output[each]) { 
     printf ("%s\n", output[each]); 
     each++; 
    } 
    each = 0; 
    while (output[each]) { 
     free (output[each]); 
     each++; 
    } 
    free (output); 

    exit (0); 
} 

char **split(char *s) { 

    char **result; 
    int wrd_cnt = 2; //I'm adding NULL at the end of the pointer-array. 
    int char_cnt = 0; 
    int i = 0; 
    int j = 0; 
    int k = 0; 

    //Counts the number of words to allocate memory for the pointer-array. 
    for(i = 0; i < strlen(s); i++) { 
     if(s[i] == ' ') { 
      wrd_cnt++; 
     } 
    } 
    if ((result = malloc(wrd_cnt * sizeof(char*))) == NULL) { 
     fprintf (stderr, "malloc failure\n"); 
     exit (1); 
    } 
    //Counts letters in each word to allocate memory for every single small char-array with malloc. 
    char_cnt = 1; 
    j = 0; 
    for(i = 0; i < strlen(s); i++) { 
     if (s[i] == ' ') { 
      if ((result[j] = malloc(char_cnt * sizeof(char))) == NULL) { 
       fprintf (stderr, "malloc failure\n"); 
       exit (1); 
      } 
      j++; 
      char_cnt = 1; 
      continue; 
     } 
     char_cnt++; 
    } 
    if (j == wrd_cnt - 2) { 
     //allocate for last word 
     if ((result[j] = malloc(char_cnt * sizeof(char))) == NULL) { 
      fprintf (stderr, "malloc failure\n"); 
      exit (1); 
     } 
     j++; 
     result[j] = NULL; 
    } 
    result[wrd_cnt - 1] = NULL;//just to make sure the last pointer is null 

    //Puts each word into their own place in the pointer array. 
    j = 0; 
    k = 0; 
    for(i = 0; i < strlen(s); i++) { 
     if (s[i] == ' ') { 
      result[j][k] = '\0';//for space only so [j][0] is '\0' 
      k = 0; 
      j++; 
      continue; 
     } 
     result[j][k] = s[i]; 
     k++; 
     result[j][k] = '\0';//for last word if there is no final space in s[] 
    } 
    return result; 
} 
0
从您的代码上面的注释

除了让我害怕,因为所有的malloc()调用你做什么,一个每个字的。这意味着你还必须释放每个单词。这会让程序面临内存泄漏。由于这是允许大量铸造的C,所以可以使用单个malloc来保存(char *)指针数组和实际单词。

 
char **split(char const *s) { 

    char **result; // 
    char *target; // where in result chars stored 
    size_t s_strlen = strlen(s); // length of s 
    int  wrd_cnt = 2; //I'm adding NULL at the end of the pointer-array. 
    { 
    char const *sx; 
    for (sx = s; sx = strpbrk(sx, " \t\n\r"); sx++) 
    { 
     wrd_cnt++; 
    } 
    } 

    result = malloc((wrd_cnt * sizeof(char *)) + s_strlen + 2); 
             /* allow for \0 and possible ' ' */ 

    target = (char *)(result + wrd_cnt); /* where to save words */ 
    strcpy(target, s);  /* copy to target known to be big enough */ 
    if (s_strlen > 0 && target[s_strlen-1] != ' ') 
     strcat(target + s_strlen, " "); /* assure ends in space */ 

    { 
    char *tx, *tnext; 
    int  n; 
    n = 0; 
    for (tx = target; tnext = strpbrk(tx, " \t\n\r"); tx = tnext + 1) 
    { 
     result[n++] = tx;  /* remember pointer */ 
     *tnext = '\0';   /* terminate word */ 
    } 
    result[n] = NULL;   /* null termination */ 
    } 

    return result; 
}