2009-07-14 49 views
0

自從我在C中寫下我的最後一行以來已經有很多年了,現在我正在嘗試再次開始創建一些函數並將其用作php的擴展。如何在c/gcc中返回腳本?

這是一個簡單的函數來創建「小網址」

讓說:

a0bg a0bf a0bh

我遇到的問題是,當我以「增量」的像ZZZ字符串,然後我得到:總線錯誤

否則,如果我增加ABR比如我得到的結果是:ABS

在某些時候我覺得我的問題這裏返回字符串結果

兩個代碼是全功能的,因爲我張貼他們在這裏。

編譯我使用:的gcc -o append_id_test.c append_id

我在OS X Leopard的

我這樣做的時候,它的工作原理:(請注意在電話會議上在功能append_id)

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

char* append_id(char*); 
int main(void) { 
    char *x; 
    char *incremented; 

    x = "aaab"; 

    printf("%s\n", append_id(x)); // should print 00000 (lenght 5) 


    incremented = (char *) malloc((strlen(x) + 2) * sizeof(char)); 
    incremented = append_id(x); 

    printf("---> %s\n", incremented); // should print 00000 (lenght 5) 

} 

char* append_id(char *id) { 

    int x; 
    char* new_id; 
    int id_size = strlen(id); 

    new_id = (char *) malloc((strlen(id) + 2) * sizeof(char)); 

    for (x = 0; x < id_size; x++) 
    { 
     new_id[x] = '0'; 
    } 

    strcat(new_id, "0"); 

    return new_id; 
} 

但整個代碼不工作(正如你所看到的調用append_id功能是在相同的方式進行上面的例子)

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


char* append_id(char*); 
char* increment_id(char*, int); 
char* get_next_id(char*); 


int main(int argc, char *argv[]) { 
    char *x; 
    int a; 

    x = "zz"; 

    printf("incrementando %s -> %s\n", "zz", get_next_id(x)); 

    return 0; 
} 


char * get_next_id(char *last_id) 
{ 
    int x, pos; 
    char *next_id; 
    char is_alnum = 1; 

    // if the last id is -1 (non-existant), start at the begining with 0 
    if (strlen(last_id) == 0) 
    { 
     next_id = "0"; 
    } 
    else 
    { 

     // check the input 
     for(x = 0; last_id[x]; x++) 
     { 
      if(!isalnum(last_id[x])) 
      { 
       is_alnum = 0; 
       break; 
      } 
     } 

     if (is_alnum == 0) 
     { 
      return ""; 
     } 


     // all chars to lowercase 
     for(x = 0; last_id[x]; x++) 
     { 
      last_id[x] = tolower(last_id[x]); 
     } 


     // loop through the id string until we find a character to increment 
     for (x = 1; x <= strlen(last_id); x++) 
     { 
      pos = strlen(last_id) - x; 

      if (last_id[pos] != 'z') 
      { 
       next_id = increment_id(last_id, pos); 
       break; // <- kill the for loop once we've found our char 
      } 
     } 

     // if every character was already at its max value (z), 
     // append another character to the string 
     if (strlen(next_id) == 0) 
     { 
      next_id = (char *) malloc((strlen(last_id) + 2) * sizeof(char)); 
      next_id = append_id(last_id); 
     } 

    } 


    return next_id; 
} 



char* append_id(char *id) { 

    int x; 
    char* new_id; 
    int id_size = strlen(id); 

    new_id = (char *) malloc((strlen(id) + 2) * sizeof(char)); 

    for (x = 0; x < id_size; x++) 
    { 
     new_id[x] = '0'; 
    } 

    strcat(new_id, "0"); 

    return new_id; 
} 



char* increment_id(char *id, int pos){ 
    char current, new_char; 
    char * new_id ; 
    int x; 

    new_id = (char *) malloc((strlen(id) + 1) * sizeof(char)); 


    current = id[pos]; 

    if (current >= 0x30 && current <= 0x39) 
    { 
     if (current < 0x39) 
     { 
      new_char = current + 1; 
     } 
     else // if we're at 9, it's time to move to the alphabet 
     { 
      new_char = 'a'; 
     } 
    } 
    else // move it up the alphabet 
    { 
     new_char = current + 1; 
    } 


    for (x = 0; x < strlen(id); x++) 
    { 
     if (x == pos) { 
      new_id[x] = new_char; 
     } 
     else { 
      new_id[x] = id[x]; 
     } 
    } 


    // set all characters after the one we're modifying to 0 
    if (pos != (strlen(new_id) - 1)) 
    { 
     for (x = (pos + 1); x < strlen(new_id); x++) 
     { 
      new_id[x] = '0'; 
     } 
    } 

    return new_id; 
} 
+2

我看到malloc的,但我沒有看到免費的... – fortran 2009-07-14 14:50:40

+1

說:「我的代碼的這一小部分工作,但與其他2個功能,是4倍大的其他部分不」,是不是很有幫助。 – dborba 2009-07-14 15:02:32

+0

我只是想向您展示問題的背景:我認爲(我不確定,是從第二部分的第74行開始的問題) – 2009-07-14 15:09:03

回答

2

請-Wall下一次編譯)

首先,使用的tolower和字符isalnum之前,

#include <ctype.h> 

其次,你爲x(主範圍)在點一個字符串,它是const。因此,如果您在打包時嘗試寫入內存,將會導致內存違規。嘗試初始化,比方說,

char x[] = "zz"; 

你也應該在get_next_id初始化NEXT_ID:

char *next_id = NULL; 

後來,不要運行在一個空指針strlen的:

// if every character was already at its max value (z), 
// append another character to the string 
if (!next_id || !strlen(next_id)) 
{ 
    next_id = append_id(last_id); 
} 

在append_id,在使用strcat之前,您需要空終止:

for (x = 0; x < id_size; x++) 
{ 
    new_id[x] = '0'; 
} 
new_id[id_size] = 0; 

這只是讓它工作。有一個很多的東西你的代碼錯了。你提到

已經多年,因爲我寫我的最後一行用C

我真的希望你年前就有的一個更好的把握,好像你忘記了很多基本的C內存,字符串和指針的概念。

0

我可能會檢查get_next_id中的for循環。您正在迭代指針而沒有限制。這到底在哪裏?

// check the input 
    for(x = 0; last_id[x]; x++) 
    { 
      if(!isalnum(last_id[x])) 
      { 
        is_alnum = 0; 
        break; 
      } 
    } 

對append_id做同樣的操作。獲取接收到的指針的大小(int id_size = strlen(id);),然後根據該長度迭代last_id指針。

我想會發生什麼事情是你的循環永遠不會結束。 last_id [x]將返回一個字符,而不是一個條件。所以它總是如此,除非當前字符爲零的情況。

1

除了忘記釋放你的記憶,你有兩個問題,上線12和72 第12行,你必須聲明「ZZ」作爲一個字符數組,而不是字符串字面:

char x[] = "zz"; 

在第72行,如果所有字符都處於最大值,則next_id將爲空。 strlen(null)會導致另一個段錯誤。

if (!next_id)