课后写的一个挖字符函数,但是内存释放出现了问题,一释放就报错
int spitString(const char *mytmp, char c, char ***mybuf, int *num){
int cls = 0;
if (mytmp == NULL || num == NULL)
{
cls = -1;
printf("func spitString() err:%d (mytmp == NULL || num == NULL)\n", cls);
goto END;
}
char *p1 = mytmp;
int count = 0;
while (p1 = strchr(p1, ','))
{
count++;
p1++;
}
char **pp = NULL;
pp = (char **)malloc(count * sizeof(char *));
if (pp == NULL)
{
cls = -2;
printf("func spitString() err:%d count * sizeof(char *)\n", cls);
goto END;
}
char *p = NULL, *ptmp = NULL;
count = 0;
p = mytmp;
ptmp = mytmp;
int len = p - ptmp + 1;
do
{
p = strchr(p, c);
if (p != NULL)
{
if (p - ptmp > 0)
{
pp = (char *)malloc(len * sizeof(char));
if (pp == NULL)
{
cls = -3;
printf("func spitString() err:%d (char *)malloc(len * sizeof(char))", cls);
goto END;
}
strncpy(pp, ptmp, p - ptmp);
pp = '\0';
count++;
ptmp = p = p + 1;
}
}
else
{
break;
}
} while (*p!='\0');
END:
if (cls != 0)
{
if (pp == NULL)
{
return -4;
}
Free_String(&pp, count);
}
else
{
*num = count;
*mybuf = pp;
}
return cls;
}
void Free_String(char ***mybuf, int num)
{
if (mybuf == NULL)
{
return;
}
int i;
char **tmp = NULL;
tmp = *mybuf;
if (tmp == NULL)
{
return;
}
for (i = 0; i < num; i++)
{
if (tmp != NULL)
{
free(tmp);
}
}
if (tmp != NULL)
{
free(tmp);
}
*mybuf = NULL;
}
int main()
{
int cls = 0;
char tmp = { "abcdef,acccd,eeee,aaaa,e3eeee,ssss," };
char c = ',';
int num = 0, i;
char **buf = NULL;
cls = spitString(tmp, c, &buf, &num);
if (cls != 0)
{
printf("fucn spitString() err:%d \n", cls);
return cls;
}
for (i = 0; i < num; i++)
printf("%s \n", buf);
printf("%d \n", num);
for (i = 0; i < num; i++)
{
if (buf != NULL)
{
free(buf);
buf = NULL;
}
}
if (buf != NULL)
{
free(buf);
buf = NULL;
}
//Free_String(&buf, num);
return cls;
} 找了很久都没找到错误出在什么地方,好打击信心啊{:9_220:} 改了好多地方
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void Free_String(char **mybuf, int num)
{
if(mybuf == NULL)
return;
for(int i = 0; i < num; ++i) {
free(mybuf);
}
free(mybuf);
}
int spitString(const char *mytmp, char c, char ***mybuf, int *num)
{
if(mytmp == NULL || num == NULL)
return -1;
const char *p1 = mytmp;
int count = 0;
while(p1 = strchr(p1, ','))
{
count++;
p1++;
}
++count;
char **pp = NULL;
pp = malloc(count * sizeof(char *));
if(pp == NULL)
{
*mybuf = pp;
return -2;
}
const char *p = mytmp, *q;
for(int i = 0; i < count; ++i)
pp = NULL;
for(int i = 0; i < count; ++i) {
q = strchr(p, c);
if(!q) q = p + strlen(p);
pp = malloc(q - p + 1);
if(!pp) return -3;
strncpy(pp, p, q - p);
pp = '\0';
p = q + 1;
}
*mybuf = pp;
*num = count;
return 0;
}
int main(void)
{
int cls = 0;
char tmp = {"abcdef,acccd,eeee,aaaa,e3eeee,ssssa,"};
char c = ',';
int num = 0, i;
char **buf = NULL;
cls = spitString(tmp, c, &buf, &num);
if(cls != 0)
{
printf("fucn spitString() err: %d\n", cls);
return cls;
}
for(i = 0; i < num; i++)
printf("%s\n", buf);
printf("%d\n", num);
Free_String(buf, num);
return cls;
}
谢谢大神,不过我编译器用的VS2013,直接malloc会报错,我将你的答案又处理了下,内存完美释放,感谢{:9_228:} 臭屁含 发表于 2020-3-24 22:17
谢谢大神,不过我编译器用的VS2013,直接malloc会报错,我将你的答案又处理了下,内存完美释放,感谢{:9_22 ...
后缀是 .cpp? 人造人 发表于 2020-3-24 22:55
后缀是 .cpp?
是的呀,我感觉.cpp跟.c没啥区别就懒得改了{:9_229:} 臭屁含 发表于 2020-3-24 23:42
是的呀,我感觉.cpp跟.c没啥区别就懒得改了
刚才我仔细对照了下我之前写的代码,终于发现了问题,函数内定义的那个int len值是错的,造成malloc内存出错,不能释放内存,我将int len放到了指针p赋值之后,问题解决了 臭屁含 发表于 2020-3-24 23:42
是的呀,我感觉.cpp跟.c没啥区别就懒得改了
就用我的代码,不要修改
你换成 .c 看看能不能编译过
臭屁含 发表于 2020-3-24 23:46
刚才我仔细对照了下我之前写的代码,终于发现了问题,函数内定义的那个int len值是错的,造成malloc内存 ...
嗯
页:
[1]