拆分字符串分隔符用C - 段错误,无效免费 [英] Split string with delimiter in C - segmentation faults, invalid free
本文介绍了拆分字符串分隔符用C - 段错误,无效免费的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写了一个简单的code在C与分隔符分割字符串。当我删除我所有的FreeS,code的伟大工程,但给人的内存泄漏。当我不去除游离,它不显示内存泄漏,但给分段故障。什么是拧,如何解决呢?
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;unsigned int类型countWords(字符* stringLine)
{
无符号整型数= 0;
字符* TMP = stringLine;
字符*最后= 0;
为const char DELIM ='/'; 而(* TMP)
{
如果(DELIM == * TMP)
{
算上++;
最后= tmp目录;
}
TMP ++;
}
返回计数;
}焦炭** getWordsFromString(字符* stringLine)
{
焦炭** sizeNames = 0;
无符号整型数= 0;
为const char * DELIM =/; 数= countWords(stringLine); sizeNames =的malloc(sizeof的(字符*)*计);
如果(sizeNames == NULL)
{
返回NULL;
} 如果(sizeNames)
{
为size_t IDX = 0;
字符*令牌= strtok的(stringLine,DELIM);
而(令牌)
{
如果(IDX>计数)
{
出口(-1);
}
*(sizeNames + IDX +)=的strdup(令牌);
令牌= strtok的(0,DELIM);
}
如果(IDX ==计数 - 1)
{
出口(-1);
}
*(sizeNames + IDX)= 0;
} 返回sizeNames;
}无效showWords(字符* stringLine)
{
unsigned int类型的大小= countWords(stringLine),I = 0;
焦炭** sizeNames = getWordsFromString(stringLine); 对于(i = 0; *(sizeNames + I);我++)
{
的printf(二字= [%S] \\ n,*(sizeNames + I));
免费(*(sizeNames + I));
}
的printf(\\ n);
免费(sizeNames);
}诠释的main()
{
CHAR字[] =你好!/世界/ /的/我/; showWords(字);
返回0;
}
解决方案
变量 sizeNames
是一个指针数组,而不是字符串(字符数组),您需要用空字符终止。
所以删除此:
*(sizeNames + IDX)= 0;
和更改此:
为(i = 0; *(sizeNames + I);我++)
要这样:
为(i = 0; I<大小;我++)
I wrote a simple code to split string in C with delimiter. When I remove all my frees, code works great but gives memory leaks. When I dont remove free, it does not show memory leaks but gives segmentation fault .. What is wring and how to solve it?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
unsigned int countWords(char *stringLine)
{
unsigned int count = 0;
char* tmp = stringLine;
char* last = 0;
const char delim = '/';
while (*tmp)
{
if (delim == *tmp)
{
count++;
last = tmp;
}
tmp++;
}
return count;
}
char **getWordsFromString(char *stringLine)
{
char** sizeNames = 0;
unsigned int count = 0;
const char *delim = "/";
count = countWords(stringLine);
sizeNames = malloc(sizeof(char*) * count);
if(sizeNames == NULL)
{
return NULL;
}
if (sizeNames)
{
size_t idx = 0;
char* token = strtok(stringLine, delim);
while (token)
{
if(idx > count)
{
exit(-1);
}
*(sizeNames + idx++) = strdup(token);
token = strtok(0, delim);
}
if(idx == count - 1)
{
exit(-1);
}
*(sizeNames + idx) = 0;
}
return sizeNames;
}
void showWords(char *stringLine)
{
unsigned int size = countWords(stringLine), i = 0;
char** sizeNames = getWordsFromString(stringLine);
for (i = 0; *(sizeNames + i); i++)
{
printf("word=[%s]\n", *(sizeNames + i));
free(*(sizeNames + i));
}
printf("\n");
free(sizeNames);
}
int main()
{
char words[] = "hello/world/!/its/me/";
showWords(words);
return 0;
}
解决方案
Variable sizeNames
is an array of pointers, not a string (array of characters) that you need to terminate with a null-character.
So remove this:
*(sizeNames + idx) = 0;
And change this:
for (i=0; *(sizeNames+i); i++)
To this:
for (i=0; i<size; i++)
这篇关于拆分字符串分隔符用C - 段错误,无效免费的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文