2016-09-19 39 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; 
}