无法正确分配内存,也无法在我的(ft_split)函数中释放它
我正在尝试创建一个应该:
用malloc分配,并返回通过使用字符“ c”拆分“ s”获得的一系列字符串 作为定界线。阵列必须以空指针结尾。
我已将所有代码与下面的代码示例以及调试器给我的错误之一的断点。
现在遇到问题:
- 当我输入字符串和末尾时,我添加了定界符,它给出了此错误为
字符串:
“是或不成为问题”
错误:
分割故障
- 当我输入此字符串时,
:
“你好”
它将给出正确的答案,然后给出此错误
你好
那里
分割故障
- 我只是无法弄清楚如何释放创建的内存分配,我认为我认为是正确的方法。
我的代码:
size_t ft_strlen(const char *s)
{
size_t i;
i = 0;
while (s[i] != '\0')
{
i++;
}
return (i);
}
char *ft_strcpy(char *dest, const char *src)
{
int i;
i = 0;
while (src[i] != '\0')
{
dest[i] = src[i];
i++;
}
dest[i] = '\0';
return (dest);
}
char *string_length(char const *str, char c, int i)
{
int j;
char *string;
j = 0;
// when i try the debugger with the first string in the main function it goes
// directly to this line and break.
string = (char *)malloc((ft_strlen(str)) * sizeof(*string));
if(!string)
return (NULL);
while (str[i] == c)
i++;
while (str[i])
{
*string = str[i];
if (str[i + 1] == c || str[i + 1] == '\0')
{
string++;
j++;
*string = '\0';
return (string - j);
}
string++;
j++;
i++;
}
return (0);
}
int count_strings(char const *str, char c)
{
int i;
int count;
int def;
i = 0;
count = 0;
def = 1;
while (str[i] == c)
i++;
while (str[i])
{
if (str[i] == c && def == 1)
{
i++;
def = 0;
}
if (str[i] != c && def == 0)
{
count++;
def = 1;
}
i++;
}
return (count);
}
int first_del(char const *str, char c, int i)
{
while (str[i] == c)
i++;
return (i);
}
char **ft_split(char const *s, char c)
{
int count;
int i;
int len;
int num_of_strings;
char **split;
char *string;
char **it;
i = 0;
count = 0;
num_of_strings = count_strings(s, c);
split = (char **)malloc((num_of_strings + 1) * sizeof(char *));
if (!split)
return (NULL);
split[num_of_strings + 1] = NULL;
while (i <= num_of_strings)
{
count = first_del(s, c, count);
string = string_length(s, c, count);
len = ft_strlen(string);
split[i] = (char *)malloc(len + 1);
ft_strcpy(split[i], string);
count += len;
i++;
free(string);
string = NULL;
}
return (split);
}
int main(void)
{
char s[] = " To be or not to be that is the question ";
char **split_strings = ft_split(s, ' ');
for (int i = 0; i < 10; i++)
printf("%s\n", split_strings[i]);
char s2[] = "Hello there";
char **split_strings2 = ft_split(s2, ' ');
for (int i = 0; i < 2; i++)
printf("%s\n", split_strings2[i]);
}
I am trying to create a ft_split
function which should:
Allocates with malloc and returns an array of strings obtained by splitting ’s’ using the character ’c’
as a delimiter. The array must end with a NULL pointer.
I have included all the code with the code sample below and the breakpoint where the debugger is giving me one of the errors.
now am having problems with:
- when I input a string and at the end of it i add the delimiter it gives this error
the string:
" To be or not to be that is the question "
the error:
Segmentation fault
- when I input this string
the string:
"Hello there"
it will give the right answer and then this error
Hello
there
Segmentation fault
- i just cant figure out how i should free the memory allocations created i have included what i think was the correct way commented.
my code:
size_t ft_strlen(const char *s)
{
size_t i;
i = 0;
while (s[i] != '\0')
{
i++;
}
return (i);
}
char *ft_strcpy(char *dest, const char *src)
{
int i;
i = 0;
while (src[i] != '\0')
{
dest[i] = src[i];
i++;
}
dest[i] = '\0';
return (dest);
}
char *string_length(char const *str, char c, int i)
{
int j;
char *string;
j = 0;
// when i try the debugger with the first string in the main function it goes
// directly to this line and break.
string = (char *)malloc((ft_strlen(str)) * sizeof(*string));
if(!string)
return (NULL);
while (str[i] == c)
i++;
while (str[i])
{
*string = str[i];
if (str[i + 1] == c || str[i + 1] == '\0')
{
string++;
j++;
*string = '\0';
return (string - j);
}
string++;
j++;
i++;
}
return (0);
}
int count_strings(char const *str, char c)
{
int i;
int count;
int def;
i = 0;
count = 0;
def = 1;
while (str[i] == c)
i++;
while (str[i])
{
if (str[i] == c && def == 1)
{
i++;
def = 0;
}
if (str[i] != c && def == 0)
{
count++;
def = 1;
}
i++;
}
return (count);
}
int first_del(char const *str, char c, int i)
{
while (str[i] == c)
i++;
return (i);
}
char **ft_split(char const *s, char c)
{
int count;
int i;
int len;
int num_of_strings;
char **split;
char *string;
char **it;
i = 0;
count = 0;
num_of_strings = count_strings(s, c);
split = (char **)malloc((num_of_strings + 1) * sizeof(char *));
if (!split)
return (NULL);
split[num_of_strings + 1] = NULL;
while (i <= num_of_strings)
{
count = first_del(s, c, count);
string = string_length(s, c, count);
len = ft_strlen(string);
split[i] = (char *)malloc(len + 1);
ft_strcpy(split[i], string);
count += len;
i++;
free(string);
string = NULL;
}
return (split);
}
int main(void)
{
char s[] = " To be or not to be that is the question ";
char **split_strings = ft_split(s, ' ');
for (int i = 0; i < 10; i++)
printf("%s\n", split_strings[i]);
char s2[] = "Hello there";
char **split_strings2 = ft_split(s2, ' ');
for (int i = 0; i < 2; i++)
printf("%s\n", split_strings2[i]);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了释放主要函数中的内存分配:
Xing的积分
to free the memory allocation in the main function:
credits to xing
首先,定义一个将给定字符串拆分为多个令牌的函数,由给定的分隔符隔开:
str
是要分配的字符串;sep
是分隔符/分界符;令牌
是将保持令牌的数组;maxtokens
是您的数组可以容纳的最大令牌(即其容量);ntokens
是str
中的代币的实际编号。返回:
true
如果字符串完全被标记化,false
如果达到令牌限制的最大数量(maxtokens
),并且字符串的结尾未达到。然后,定义一个函数,该函数计算给定字符串中的令牌数:
将两个函数放在一起:
输出:
注释:
&lt; string.h&gt; ,您可以用自己的版本替换
strncpy()
。malloc()
的内存。First, define a function that splits a given string into multiple tokens, separated by a given separator:
str
is the string to be splitted;sep
is the separator/delimiter;tokens
is the array that will hold your tokens;maxtokens
is the maximum number of tokens your array can hold (i.e. its capacity);ntokens
is the actual number of tokens instr
.Returns:
true
if the string has been tokenized totally,false
if the maximum number of tokens limit (maxtokens
) is reached and the end of string is not reached.Then, define a function that computes the number of tokens in a given string:
Putting the two together:
Output:
Notes:
<string.h>
, you can replacestrncpy()
with your own version.malloc()
as soon as you are done with the tokens.