Turecko-americká vycházející matematická hvězda Levent Alpöge ze společnosti Anthropic vyvolal pozdvižení oznámením (PDF), že s pomocí superinteligence Claude od Antropicu dokázal vyřešit jeden ze sedmi největších matematických problémů tisíciletí, slavnou Riemannovu hypotézu (prvočísla se mezi čísly objevují zdánlivě nepravidelně, hypotéza tvrdí, že za jejich rozmístěním je určitý řád). Svůj důkaz uvolnil jako open-source
… více »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.
#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;
}
Tiskni
Sdílej: