Portál AbcLinuxu, 5. května 2025 18:50

Dotaz: pomoc s ladenim kodu v C

17.8.2009 17:37 ext3fs
pomoc s ladenim kodu v C
Přečteno: 302×
Odpovědět | Admin

Dobry den,

 chci poprosit s pomoci nalezeni chyby v nasledujicim kodu:

(komentuji radky kde valgrind hlasi chybu).

char *get_term(char *str, int pos)                                              
{                                                                               
int n;                                                                          
int word = 0;                                                                   
int w = 0, w_m = 0;                                                             
short in_word = 0;                                                              
short out_word = 0;                                                             
char **words;                                                                   

    for(n = 0; str[n] != '\0'; n++)                            
    {                                                          
                if (str[n] == '<' || str[n] == ' ' || str[n] == '>')
                {                                                   
                if (in_word)                                        
                {                                                   
                                if (w > w_m)                        
                                w_m = w;                            
                }                                                   
                out_word = 1;                                       
                in_word = 0;                                        
                w = 0;                                              
                }                                                   
                else if (!in_word)                                  
                {                                                   
                word++;                                             
                out_word = 0;                                       
                in_word = 1;                                        
                }                                                   
                if (in_word)                                        
                w++;                                                
    }                                                               

/*ZDE*/    if ((words = (char **)malloc(word*(sizeof(char *)))) == NULL)
        {                                                        
                fprintf(stderr, "chps : %s\n", strerror(errno));
                exit (1);                                        
        }                                                        
                                                                 
    for (n = 0; n < word; n++)                                   
        {                                                        
                if ((words[n] = (char *)malloc(w_m*sizeof(char))) == NULL)
                {                                                         
                        fprintf(stderr, "chps : %s\n", strerror(errno));  
                        exit (1);                                         
                }                                                         
        }                                                                 

    in_word = out_word = 0;
    w = w_m = word = 0;    
    for(n = 0; str[n] != '\0'; n++)
    {
                if (str[n] == '<' || str[n] == ' ' || str[n] == '>')
                {
                if (in_word)
                {
                                if (w > w_m)
                                {
                                w_m = w;
                                }
                                /*ZDE*/ words[word - 1][w] = '\0';
                }
                out_word = 1;
                in_word = 0;
                w = 0;
                }
                else if (!in_word)
                {
                word++;
                out_word = 0;
                in_word = 1;
                }
                if (in_word)
                words[word - 1][w++] = str[n];
    }

    if (pos <= word)
                return(words[pos - 1]);
    else
                return(" ");
}

Funkce se vola napr get_term("<NAME opt1 opt2 opt3>", 2); a

vrati opt1.

 

Dekuji.

Nástroje: Začni sledovat (0) ?Zašle upozornění na váš email při vložení nového komentáře.

Odpovědi

17.8.2009 19:12 pht | skóre: 48 | blog: pht
Rozbalit Rozbalit vše Re: pomoc s ladenim kodu v C
Odpovědět | | Sbalit | Link | Blokovat | Admin
když to chcípe na malloc, tak jste pravděpodobně poškodil haldu zapsáním mimo meze polí. při pohledu na kód se nedivím... není to trochu moc práce kvůli takové blbosti?

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

char *get_term(const char *str, int pos)
{
        char *p, *q, *r;
        size_t len;

        if ((pos < 1) || !str) {
                return NULL;
        }
        len = strlen(str);
        if ((*str != '<') || (str[len - 1] != '>')) {
                return NULL;
        }

        p = (char *)str + 1;
        while (--pos) {
                p = strchr(p, ' ');
                if (!p || !*p) {
                        return NULL;
                }
                p++;
        }
        q = strchr(p, ' ');
        if (!q) {
                q = (char *)str + len - 2;
        }
        if (p > q) {
                return NULL;
        }
        r = malloc(q - p + 2);
        if (!r) {
                return NULL;
        }
        memcpy(r, p, q - p + 1);
        r[q - p + 1] = '\0';
        return r;
}

int main()
{
        const char *s = "<a b c d>";
        int i;

        for (i = 1; i <= 5; i++) {
                char *p = get_term(s, i);

                printf("%d: %s\n", i, p ? p : "not found");
                if (p) {
                        free(p);
                }
        }
        return 0;
}

In Ada the typical infinite loop would normally be terminated by detonation.
17.8.2009 20:34 ext3fs
Rozbalit Rozbalit vše Re: pomoc s ladenim kodu v C

Dekuji za spravne nakopnuti. Kod jsem smazal a napsal znovu na polovinu radku a uz to funguje.

Založit nové vláknoNahoru

Tiskni Sdílej: Linkuj Jaggni to Vybrali.sme.sk Google Del.icio.us Facebook

ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.