70,037
社区成员
发帖
与我相关
我的任务
分享
void data_tok(char *buf)
{
char i = 0,j = 0,n = 0,fla = 0,x,y;
// char *_buf1[30][45];
char *buf2;
char *p = NULL,*q = NULL;
char *data[30];
char *out;
cJSON *root,*rot,*fmt;
p = strstr(buf,"|90|90|");
if(p != NULL)
{
while(q = strstr(p+1,"|90|90|") )
{
data[n] = (char *)malloc(255);
memcpy(data[n],p,q - p);
memset(data[n] + (q-p-1),0,1);
// printf("p:-----------:%s\n",data[n]);
buf2 = data[n];
j = 0;
_buf1[i][j] = (char *)malloc(30);
while((_buf1[i][j]=strtok(buf2,"|"))!=NULL)
{
buf2=NULL;
//printf("%s ",_buf1[i][j]);
_buf1[i][++j] = (char *)malloc(30);
}
p = q;
i++;
n++;
}
if(q == NULL)
{
q = buf + strlen(buf);
data[n] = (char *)malloc(512);
memcpy(data[n],p,q - p);
memset(data[n] + (q-p-1),0,1);
// printf("p:-----------:%s\n",data[n]);
buf2 = data[n];
j = 0;
_buf1[i][j] = (char *)malloc(30);
while((_buf1[i][j]=strtok(buf2,"|"))!=NULL)
{
buf2=NULL;
if(strcmp(_buf1[i][j],"ZS") == 0)
{
_buf1[i][j - 2] = NULL;
--j;
fla = 1;
break;
}
_buf1[i][++j] = (char *)malloc(30);
}
p = q;
i++;
n++;
}
}
printf("\n\n");
chinese(i-1);
/*
for(x = 0;x < i;x++)
{
for(y = 0;y < j;y++)
{
printf("%s ",_buf1[x][y]);
}
printf("\n");
}
*/
char *cont[42];
y = 0;
for(x = 0;x < 42;x++)
{
cont[x] = (char *)malloc(2);
sprintf(cont[x],"%d",x);
}
root=cJSON_CreateObject();
rot=cJSON_CreateArray();
cJSON_AddItemToObject(root, "list",rot);
for(x = 0;x < i;x++)
{
cJSON_AddItemToObject(rot, "a",fmt=cJSON_CreateObject());
if(fla == 0)
{
for(y = 0;y < j ;y++)
{
cJSON_AddStringToObject(fmt,cont[y],_buf1[x][y]);
}
}
else
{
for(y = 0;y < j - 1 ;y++)
{
cJSON_AddStringToObject(fmt,cont[y],_buf1[x][y]);
}
}
}
out=cJSON_Print(root); cJSON_Delete(root);
pubMessage(out,strlen(out));
printf("%s\n",out); free(out);
/*
for(y = 0;y < 42;y++)
{
free(cont[y]);
}
for(x = 0;x < i;x++)
{
free(data[x]);
for(y = 0;y < j;y++)
{
free(cont[y]);
free(_buf1[x][y]);
}
}*/
}
while((_buf1[i][j]=strtok(buf2,"|"))!=NULL)
{
buf2=NULL;
if(strcmp(_buf1[i][j],"ZS") == 0)
{
_buf1[i][j - 2] = NULL;
--j;
fla = 1;
break;
}
_buf1[i][++j] = (char *)malloc(30);
}
[/quote](j-2)置为NULL,是因为前面的两个字符串不要, 你再运行一下,看看崩溃在哪一行?
while((_buf1[i][j]=strtok(buf2,"|"))!=NULL)
{
buf2=NULL;
if(strcmp(_buf1[i][j],"ZS") == 0)
{
_buf1[i][j - 2] = NULL;
--j;
fla = 1;
break;
}
_buf1[i][++j] = (char *)malloc(30);
}
你再运行一下,看看崩溃在哪一行?
你再运行一下,看看崩溃在哪一行?

一个原则, 一次malloc对应一次free 就不会有问题
在用完的地方free就行,现在出了什么问题
for(y = 0;y < 42;y++)
721 {
722 free(cont[y]);
723 }
724 if(p_fla != NULL)
725 {
726 for(x = 0;x < i;x++)
727 {
728 if(fla == 0)
729 {
730 for(y = 0;y < j;y++)
731 {
732 free(_buf1[x][y]);
733 }
734 }
735 else
736 {
737 for(y = 0;y < j-1;y++)
738 {
739 free(_buf1[x][y]);
740 }
741 }
742 }
743 }
#include <time.h>
#include <stdlib.h>
#include <windows.h>
int main() {
int a,b[11];//本来是b[10],为判断哪句越界,故意声明为b[11]
srand((unsigned int)time(NULL));//按两次F11,等黄色右箭头指向本行时,调试、新建断点、新建数据断点,地址:&b[10],字节计数:4,确定。
while (1) {//按F5,会停在下面某句,此时a的值为10,b[10]已经被修改为对应0..4之一。
b[(a=rand()%11)]=0;
Sleep(100);
b[(a=rand()%11)]=1;
Sleep(100);
b[(a=rand()%11)]=2;
Sleep(100);
b[(a=rand()%11)]=3;
Sleep(100);
b[(a=rand()%11)]=4;
Sleep(100);
}
return 0;
}
[quote=引用 7 楼 dzc_woniu 的回复:] [quote=引用 5 楼 shihengzhen101 的回复:] 你再运行一下,看看崩溃在哪一行?
while((_buf1[i][j]=strtok(buf2,"|"))!=NULL)
{
buf2=NULL;
if(strcmp(_buf1[i][j],"ZS") == 0)
{
_buf1[i][j - 2] = NULL;
--j;
fla = 1;
break;
}
_buf1[i][++j] = (char *)malloc(30);
}
[/quote](j-2)置为NULL,是因为前面的两个字符串不要,[/quote]
不能理解啊,你这样不就内存泄露了吗?你还没有free,就把指针置为NULL