|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
在练习写根据逗号分割字符串问题的时候运用二级指针在被调函数的中分配了内存,主调函数中调用过后释放内存的时候调试失败,
求有经验的哥哥们帮忙解答,谢谢!!
代码如下:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
void freeMem(char ***freePointer, int num);
//查询字符串中某个字符出现的个数
//并返回每一个相邻字符ch之间其他字符的数目,通过数组返回
int strSerchCount(char *psource, int *_ncount,char ch,int scount[]){
int ncount = 0;
int i=0, j=0, k=0;
char *p = psource;
char *pb = psource;
//int *sc=NULL;
if (p == NULL||scount==NULL){
return -1;
}
//sc = scount;
while (*p != '\0'){
if (*p == ch){
ncount++;
}
p++;
}
//sc = (int **)malloc(sizeof(int*)*(ncount + 1));//内存分配
//*sc = (int*)malloc(sizeof(int)*(ncount + 1));
//for (k = 0; k < ncount + 1; k++){
// sc[k] = (int *)malloc(sizeof(int)*4);
// if (sc[k] == NULL){
//printf("erro\n");
//}
//}
//printf("%d", sizeof(*sc));
p = psource;
while (*p != '\0'){
if (*p == ch){
scount[i]= (p-pb)/sizeof(char);
pb = p + 1;
//j = 0;
i++;
}
p++;
//j++;
}
if (*p == '\0'){
scount[i] = (p - pb) / sizeof(char);
}
*_ncount = ncount+1;
//scount = sc;
return 0;
}
//内存分配函数
int getMem(char ***_pMem, int *secMemCount, int firstMemCount){
int i = 0;
int ret = 0;
char **mem = NULL;
if (_pMem == NULL || secMemCount == NULL){
ret = -1;
goto END;
}
//分配一级指针内存
mem = (char **)malloc(sizeof(char *)*firstMemCount);
if (mem == NULL){
ret = -2;
goto END;
}
//分配二级指针内存
for (i = 0; i <firstMemCount; i++){
mem[i] = (char *)malloc(sizeof(char)*secMemCount[i]);
if (mem[i] == NULL){
ret = -3;
goto END;
}
}
END:
if (ret != 0){
freeMem(&mem, firstMemCount);
}
else
{
*_pMem = mem;
}
return ret;
}
//内存释放
void freeMem(char ***freePointer, int num){
int i = 0;
char **p = NULL;
if (freePointer == NULL){
return;
}
p = *freePointer;
for (i = 0; i < num; i++){
if (p[i] != NULL){
free(p[i]);
p[i] == NULL;
}
}
if (p != NULL){
free(p);
freePointer == NULL;
}
}
//字符串查找
// int strSearch(char *_psource, char **_pt1){
// char *p1 = NULL;
// //char *p2 = NULL;
// if (_psource == NULL || _pt1==NULL/*||_pt2==NULL*/){
// return -1;
// }
// //p1 = _psource;
// //p2 = _psource;
// //while ()
//
//
// }
//
int memDataArray(char *psource, char ***_prl, int _firstcount, int *secount){
int ret = 0;
char *pt = NULL;
char **prl = NULL;
int i = 0;
if (psource == NULL || _prl == NULL || secount == NULL){
ret = -1;
return ret;
}
pt = psource;
prl = *_prl;
for (i = 0; i < _firstcount; i++){
strncpy(prl[i], pt, secount[i]);
prl[i][secount[i]] = '\0';
pt = pt +secount[i]+1;
}
//strSearch(psource, &pt);
return ret;
}
//字符串分割
int spiltString(char *p, char***_pr, int *_ncout){
int ncount = 0;
int ret = 0;
int eachMemLen[25]={0};
char **prt = NULL;
if (p == NULL || _pr == NULL || _ncout == NULL){
return -1;
}
//第一步查询字符串中分隔符“,”的个数
strSerchCount(p, &ncount, ',',eachMemLen);
//第二步根据逗号的数目分配内存
ret=getMem(&prt, eachMemLen, ncount);
if (ret != 0){
printf("fuc err:%d getMem(char ***_pMem, int *secMemCount, int firstMemCount)\n", ret);
return ret;
}
//将字符串按照每一个字符分割存入二维数组中
ret=memDataArray(p, &prt, ncount, eachMemLen);
if (ret != 0){
printf("fuc err: %d memDataArray(char *psource, char ***_prl, int _firstcount, int *secount)\n", ret);
return ret;
}
*_pr = prt;
*_ncout = ncount;
return ret;
}
int main(){
//测试用字符串
char *p = "abcdef,acccd,eeeee,aaaaa,e3eeeeeee, sssssss";
char **pr = NULL;//输出结果
int ncount = 0;
int i = 0;
spiltString(p, &pr, &ncount);
for (i = 0; i < ncount; i++){
printf("%s\n", pr[i]);
}
freeMem(&pr, ncount);
return 0;
}
你的程序有两个问题,第一个是二级指针分配内存时,你是根据字符串长度分配的,这并没有错
mem[i] = (char *)malloc(sizeof(char)*secMemCount[i]);
但是后边这一句 prl[i][secount[i]] = '\0';
就会导致的时候释放溢出了,因为你的字符串“abcdef”长度为6,你前边分配了6个char长度,但是你最后加上'\0'多一个赋值,相当于要长度7,而你实际长度只有6,对于其他类型你的做法是对的,但是字符串在内存中都是比实际字符串长度大1用于存储结束符
另一个小问题 p[i] == NULL;释放内存的时候=写成==了
|
-
调试到释放内存的时候就报错了
|