为什么我的程序会跳过 for 循环? C
Why my program skips a for cicle? C
我正在尝试编写一个将字符串拆分为多个字符串的函数,我知道我有很多已分配的 space 未释放,我只是在测试这一点,但 valgrind 显示了我
Conditional jump or move depends on uninitialised value(s)
==25613== at 0x4C2DB3C: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==25613== by 0x40090C: split (strutil.c:32)
==25613== by 0x400A00: main (strutil.c:45)
==25613== Uninitialised value was created by a stack allocation
==25613== at 0x400720: split (strutil.c:9)
一些类似的错误,然后 sigsem 并关闭。我担心的是,当我 运行 它与 gdb 在那个应该计算 ',' 的圆圈时,它按预期循环直到它达到值 ',' 然后跳过整个 cicle 但 i++ 并继续前进。为什么要这样做?我观察了 gdb,所有参数(str[i],sep)在条件之前的时刻都有正确的值。
#include "strutil.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
char** split(const char* str, char sep){
size_t cant = 2;
size_t i;
for(i = 0; i < strlen(str); i++){//this is line 9
if(str[i] == sep)
cant ++;
i++;
}
size_t corte[cant];
i = 0;
corte[0] = 0;
size_t j = 1;
size_t cant_corte[cant];
for(i = 0; i < strlen(str); i++){
if(str[i] == sep){
corte[j] = i + 1;
cant_corte[j - 1] = corte[j] - corte[j - 1];
//printf("pasa\n");
j++;
}
printf("pasa\n");
i++;
}
char** strv = malloc(sizeof(char*) * cant);
if (strv == NULL)return NULL;
for(i=0; i < cant; i++){
strv[i] = malloc(sizeof(char) * cant_corte[i]);
if (strv[i] == NULL)return NULL;
strncpy(strv[i], str + corte[i], cant_corte[i-1]);
strcat(strv[i], "[=11=]");
}
strv[cant + 2] = NULL;
return strv;
}
int main(){
char* eje = "abc,defg";
printf("%s\n", eje);
char r = ',';
char** prueba = split(eje, r);
printf("%s\n", prueba[0]);
getchar();
return 0;
}
如果您不希望 i
变量继续递增,请确保在您的 for 循环中您不递增 i
,而是依靠 for 循环来执行它的'工作。 (每次条件为真时增加 i )。
我正在尝试编写一个将字符串拆分为多个字符串的函数,我知道我有很多已分配的 space 未释放,我只是在测试这一点,但 valgrind 显示了我
Conditional jump or move depends on uninitialised value(s)
==25613== at 0x4C2DB3C: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==25613== by 0x40090C: split (strutil.c:32)
==25613== by 0x400A00: main (strutil.c:45)
==25613== Uninitialised value was created by a stack allocation
==25613== at 0x400720: split (strutil.c:9)
一些类似的错误,然后 sigsem 并关闭。我担心的是,当我 运行 它与 gdb 在那个应该计算 ',' 的圆圈时,它按预期循环直到它达到值 ',' 然后跳过整个 cicle 但 i++ 并继续前进。为什么要这样做?我观察了 gdb,所有参数(str[i],sep)在条件之前的时刻都有正确的值。
#include "strutil.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
char** split(const char* str, char sep){
size_t cant = 2;
size_t i;
for(i = 0; i < strlen(str); i++){//this is line 9
if(str[i] == sep)
cant ++;
i++;
}
size_t corte[cant];
i = 0;
corte[0] = 0;
size_t j = 1;
size_t cant_corte[cant];
for(i = 0; i < strlen(str); i++){
if(str[i] == sep){
corte[j] = i + 1;
cant_corte[j - 1] = corte[j] - corte[j - 1];
//printf("pasa\n");
j++;
}
printf("pasa\n");
i++;
}
char** strv = malloc(sizeof(char*) * cant);
if (strv == NULL)return NULL;
for(i=0; i < cant; i++){
strv[i] = malloc(sizeof(char) * cant_corte[i]);
if (strv[i] == NULL)return NULL;
strncpy(strv[i], str + corte[i], cant_corte[i-1]);
strcat(strv[i], "[=11=]");
}
strv[cant + 2] = NULL;
return strv;
}
int main(){
char* eje = "abc,defg";
printf("%s\n", eje);
char r = ',';
char** prueba = split(eje, r);
printf("%s\n", prueba[0]);
getchar();
return 0;
}
如果您不希望 i
变量继续递增,请确保在您的 for 循环中您不递增 i
,而是依靠 for 循环来执行它的'工作。 (每次条件为真时增加 i )。