segmentation fault

9 отговора 272 прегледа
#1

segmentation fault

...
void test(char *buf)
{
char *s;
s = strtok(buf," ");
return ;
}

...
test("ala bala nitza...");

po si4ki pravila tva trqbva da ba4ka, da da ama ne...
nqkakva sveja ideq???!!! ne6to mnogo tapo e ama az kat ne se se6tam

Отговор
в отговор на linuxfen #2

Re: segmentation fault

# man strtok
---
BUGS
Never use this function. This function modifies its first argument. The iden<AD>
tity of the delimiting character is lost. This function cannot be used on con<AD>
stant strings.

---

ПП
Така бачка:

void test(char *buf)
{
char *s = buf;
strtok(s, " ");
printf("%s", s);
return;
}

...
char* str = "ala bala nitza...";
char* buf = (char *)malloc(strlen(str) + 1);
strcpy(buf, str);
test(buf);
free(buf);

Редактирано от mn_t на

Отговор
в отговор на mn_t #3

Re: segmentation fault

Но не както трябва:
test(buf);
printf("%s", buf); //Изкарва само "ala"
free(buf);

По-добре използвай strsep.

Отговор
в отговор на mn_t #4

strsep

man strsep:
...

BUGS

This function suffers from the same problems as strtok(). In particular, it modifies the original string. Avoid it.

....

Язък

Отговор
в отговор на Masklin #5

Re: strsep

xaxaxa ami i az stignah do tuka ama po trudniqt na4in:) radost za o4ite mi... i strstr ima nqkakav podoben problem:)

P.S. za6to da e prosto kato moje da e slojno...

Отговор
в отговор на linuxfen #6

Това пък защо

strtok и strsep имат общ проблем - трябва да могат да се викат последователно много пъти и значи някъде трябва да се записва докъде са стигнали. Не че нещо пречи това място да е част от интерфейса и да се изисква от потребителя да дава този storage, ама хората са взели решение да пипат стринга. Лошо няма (има де, ясно), ама пък на strstr защо му е да променя входния низ?

Отговор
в отговор на Masklin #7

Re: strsep

Хъх.... явно man страницата ми е стара

SYNOPSIS
#include <string.h>

char *strsep(char **stringp, const char *delim);

DESCRIPTION
The strsep() function returns the next token from the string stringp which is
delimited by delim. The token is terminated with a `\0' character and stringp
is updated to point past the token.

RETURN VALUE
The strsep() function returns a pointer to the token, or NULL if delim is not
found in stringp.

NOTES
The strsep() function was introduced as a replacement for strtok(), since the
latter cannot handle empty fields. (However, strtok() conforms to ANSI-C and
hence is more portable.)

CONFORMING TO
BSD 4.4

SEE ALSO
index(3), memchr(3), rindex(3), strchr(3), strpbrk(3), strspn(3), strstr(3),
strtok(3)

GNU April 12, 1993

Отговор
в отговор на mn_t #8

Re: strsep

STRSEP(3) Linux Programmer's Manual STRSEP(3)



NAME
strsep - extract token from string

SYNOPSIS
#include <string.h>

char *strsep(char **stringp, const char *delim);

DESCRIPTION
If *stringp is NULL, the strsep() function returns NULL
and does nothing else. Otherwise, this function finds the
first token in the string *stringp, where tokens are
delimited by symbols in the string delim. This token is
terminated with a `\0' character (by overwriting the
delimiter) and *stringp is updated to point past the
token. In case no delimiter was found, the token is taken
to be the entire string *stringp, and *stringp is made
NULL.

RETURN VALUE
The strsep() function returns a pointer to the token, that
is, it returns the original value of *stringp.

NOTES
The strsep() function was introduced as a replacement for
strtok(), since the latter cannot handle empty fields.
However, strtok() conforms to ANSI-C and hence is more
portable.

BUGS
This function suffers from the same problems as strtok().
In particular, it modifies the original string. Avoid it.

CONFORMING TO
BSD 4.4

SEE ALSO
index(3), memchr(3), rindex(3), strchr(3), strpbrk(3),
strspn(3), strstr(3), strtok(3)



GNU 1993-04-12 STRSEP(3)

Отговор
в отговор на Masklin #9

Re: strsep

Мдамммм, за пореден път готовото sux.

Отговор
в отговор на mn_t #10

Не,

просто лош дизайн. Най-трудното в програмирането. Нещото, в което Линус е гениален, всъщност . Очевидно posix не е негов продукт

Отговор