user-0 提问: C语言内存问题

回复:
user: user-1
content: free(token);错误          token并没有使用malloc分配内存给它 strtok 错误            strtok会对原先字符串造成破坏

user: user-2
content: 貌似不需要free(token);

user: user-3
content: 看上去怪怪的

user: user-4
content: strtok, 这是linux中的吗, 真是怪, 竟然第二次不用源串???????????????????????一点都不符合普通vc语言习惯

user: user-5
content: 试试这个 #include<io.h> #include<stdio.h> #include<conio.h> #include<malloc.h> #include<stdlib.h> #include<string.h>  void localSearch(); void allMusicList(struct Music *local); void playMusicList(struct Music *play, struct Music *local);//导入播放列表   int  count = 0;//本地歌曲数目 int temp = 0;   struct Music { 	int order; 	char *name; 	char *singer; };//歌曲信息   int main() {  	localSearch();//计算 本地音乐数量 	struct Music *allSong = (struct Music*) malloc(sizeof(struct Music) * count);//建立全部歌曲列表 	memset(allSong, NULL, sizeof(struct Music) * count);  	if (NULL == allSong) 	{ 		printf("内存分配失败"); 		return -1; 	}  	allMusicList(allSong);//导入全部歌曲列表  	free(allSong);//释放全部歌曲内存  	system("pause");  	return 0; }   void localSearch() { 	long Handle; 	struct _finddata_t FileInfo; 	if ((Handle = _findfirst("D:\\CloudMusic\\*.MP3", &FileInfo)) == -1L)//存放歌曲的路径 	{ 		printf("本地没有歌曲可以播放\n"); 	} 	else 	{ 		printf("localSearch()---%s\n", FileInfo.name);  		count = 1; 		while (_findnext(Handle, &FileInfo) == 0) 		{ 			printf("localSearch()---%s\n", FileInfo.name); 			count++; 		} 		_findclose(Handle); 		printf("localSearch()---共找到%d首音乐\n", count); 	} }   void allMusicList(struct Music *local) { 	long Handle; 	char *buf = NULL; 	struct _finddata_t FileInfo; 	Handle = _findfirst("D:\\CloudMusic\\*.MP3", &FileInfo); 	int address = 0; 	char str[100];  	char *token;  	token = strtok_s(FileInfo.name, "-", &buf);  	local[address].singer = (char *)malloc(strlen(token) + 1); 	strcpy_s(local[address].singer, strlen(token) + 1, token); 	while (token != NULL) 	{ 		token = strtok_s(NULL, "-", &buf); 		if (token != NULL) 		{ 			local[address].name = (char *)malloc(strlen(token) + 1); 			strcpy_s(local[address].name, strlen(token) + 1, token); 		} 	} 	local[address].order = address + 1; 	printf("allMusicList()---%d. %s  \t%s\n", local[address].order, local[address].name, local[address].singer); 	address++;  	//分割第一首歌的歌手与歌名并加序号   	while (_findnext(Handle, &FileInfo) == 0) 	{ 		token = strtok_s(FileInfo.name, "-", &buf);  		local[address].singer = (char *)malloc(strlen(token) + 1); 		strcpy_s(local[address].singer, strlen(token) + 1, token); 		while (token != NULL) 		{ 			token = strtok_s(NULL, "-", &buf); 			if (token != NULL) 			{ 				local[address].name = (char *)malloc(strlen(token) + 1); 				strcpy_s(local[address].name, strlen(token) + 1, token); 			} 		} 		local[address].order = address + 1; 		printf("allMusicList()---%d. %s  \t%s\n", local[address].order, local[address].name, local[address].singer);//输出结果与下面输出不一致 		address++; 	} 	for (int i = 0; i < count; i++) 	{ 		printf("allMusicList()---%d. %s  \t%s\n", local[i].order, local[i].name, local[i].singer);//输出结果与上面不一致 	}   	_findclose(Handle); 	//分割全部歌曲的歌手与歌名并加序号  }

user: user-6
content: 仅供参考：#include <stdio.h> #include <string.h> char string[80]; char seps1[3]; char seps2[3]; char *token; char *zzstrtok (     char *string,     const char *control1,//连续出现时视为中间夹空token     const char *control2 //连续出现时视为中间无空token     ) {     unsigned char *str;     const unsigned char *ctrl1 = (const unsigned char *)control1;     const unsigned char *ctrl2 = (const unsigned char *)control2;     unsigned char map1[32],map2[32];     static char *nextoken;     static char flag=0;     unsigned char c;     int L;      memset(map1,0,32);     memset(map2,0,32);     do {         map1[*ctrl1 >> 3] |= (1 << (*ctrl1 & 7));     } while (*ctrl1++);     do {         map2[*ctrl2 >> 3] |= (1 << (*ctrl2 & 7));     } while (*ctrl2++);      if (string) {         if (control2[0]) {             L=strlen(string);             while (1) {                 c=string[L-1];                 if (map2[c >> 3] & (1 << (c & 7))) {                     L--;                     string[L]=0;                 } else break;             }         }         if (control1[0]) {             L=strlen(string);             c=string[L-1];             if (map1[c >> 3] & (1 << (c & 7))) {                 string[L]=control1[0];                 string[L+1]=0;             }         }         str=(unsigned char *)string;     }     else str=(unsigned char *)nextoken;      string=(char *)str;     while (1) {         if (0==flag) {             if (!*str) break;             if (map1[*str >> 3] & (1 << (*str & 7))) {                 *str=0;                 str++;                 break;             } else if (map2[*str >> 3] & (1 << (*str & 7))) {                 string++;                 str++;             } else {                 flag=1;                 str++;             }         } else if (1==flag) {             if (!*str) break;             if (map1[*str >> 3] & (1 << (*str & 7))) {                 *str=0;                 str++;                 flag=0;                 break;             } else if (map2[*str >> 3] & (1 << (*str & 7))) {                 *str=0;                 str++;                 flag=2;                 break;             } else str++;         } else {//2==flag             if (!*str) return NULL;             if (map1[*str >> 3] & (1 << (*str & 7))) {                 str++;                 string=(char *)str;                 flag=0;             } else if (map2[*str >> 3] & (1 << (*str & 7))) {                 str++;                 string=(char *)str;             } else {                 string=(char *)str;                 str++;                 flag=1;             }         }     }     nextoken=(char *)str;      if (string==(char *)str) return NULL;     else             return string; } void main() {    strcpy(string,"A \tstring\t\tof ,,tokens\n\nand some  more tokens, ");    strcpy(seps1,",\n");strcpy(seps2," \t");    printf("\n[%s]\nTokens:\n",string);    token=zzstrtok(string,seps1,seps2);    while (token!=NULL) {       printf(" <%s>",token);       token=zzstrtok(NULL,seps1,seps2);    }     strcpy(string,"1234| LIYI|China | 010 |201110260000|OK");    strcpy(seps1,"|");strcpy(seps2," ");    printf("\n[%s]\nTokens:\n",string);    token=zzstrtok(string,seps1,seps2);    while (token!=NULL) {       printf(" <%s>",token);       token=zzstrtok(NULL,seps1,seps2);    }     strcpy(string,"1234|LIYI||010|201110260000|OK");    strcpy(seps1,"");strcpy(seps2,"|");    printf("\n[%s]\nTokens:\n",string);    token=zzstrtok(string,seps1,seps2);    while (token!=NULL) {       printf(" <%s>",token);       token=zzstrtok(NULL,seps1,seps2);    }     strcpy(string,"1234|LIYI||010|201110260000|OK");    strcpy(seps1,"|");strcpy(seps2,"");    printf("\n[%s]\nTokens:\n",string);    token=zzstrtok(string,seps1,seps2);    while (token!=NULL) {       printf(" <%s>",token);       token=zzstrtok(NULL,seps1,seps2);    }     strcpy(string,"a");    strcpy(seps1,",");strcpy(seps2,"");    printf("\n[%s]\nTokens:\n",string);    token=zzstrtok(string,seps1,seps2);    while (token!=NULL) {       printf(" <%s>",token);       token=zzstrtok(NULL,seps1,seps2);    }     strcpy(string,"a,b");    strcpy(seps1,",");strcpy(seps2,"");    printf("\n[%s]\nTokens:\n",string);    token=zzstrtok(string,seps1,seps2);    while (token!=NULL) {       printf(" <%s>",token);       token=zzstrtok(NULL,seps1,seps2);    }     strcpy(string,"a,,b");    strcpy(seps1,",");strcpy(seps2,"");    printf("\n[%s]\nTokens:\n",string);    token=zzstrtok(string,seps1,seps2);    while (token!=NULL) {       printf(" <%s>",token);       token=zzstrtok(NULL,seps1,seps2);    }     strcpy(string,",a");    strcpy(seps1,",");strcpy(seps2,"");    printf("\n[%s]\nTokens:\n",string);    token=zzstrtok(string,seps1,seps2);    while (token!=NULL) {       printf(" <%s>",token);       token=zzstrtok(NULL,seps1,seps2);    }     strcpy(string,"a,");    strcpy(seps1,",");strcpy(seps2,"");    printf("\n[%s]\nTokens:\n",string);    token=zzstrtok(string,seps1,seps2);    while (token!=NULL) {       printf(" <%s>",token);       token=zzstrtok(NULL,seps1,seps2);    }     strcpy(string,",a,,b");    strcpy(seps1,",");strcpy(seps2,"");    printf("\n[%s]\nTokens:\n",string);    token=zzstrtok(string,seps1,seps2);    while (token!=NULL) {       printf(" <%s>",token);       token=zzstrtok(NULL,seps1,seps2);    }     strcpy(string,",,a,,b,,");    strcpy(seps1,",");strcpy(seps2,"");    printf("\n[%s]\nTokens:\n",string);    token=zzstrtok(string,seps1,seps2);    while (token!=NULL) {       printf(" <%s>",token);       token=zzstrtok(NULL,seps1,seps2);    }     strcpy(string,",");    strcpy(seps1,",");strcpy(seps2,"");    printf("\n[%s]\nTokens:\n",string);    token=zzstrtok(string,seps1,seps2);    while (token!=NULL) {       printf(" <%s>",token);       token=zzstrtok(NULL,seps1,seps2);    }     strcpy(string,",,");    strcpy(seps1,",");strcpy(seps2,"");    printf("\n[%s]\nTokens:\n",string);    token=zzstrtok(string,seps1,seps2);    while (token!=NULL) {       printf(" <%s>",token);       token=zzstrtok(NULL,seps1,seps2);    }     strcpy(string,",,,");    strcpy(seps1,",");strcpy(seps2," ");    printf("\n[%s]\nTokens:\n",string);    token=zzstrtok(string,seps1,seps2);    while (token!=NULL) {       printf(" <%s>",token);       token=zzstrtok(NULL,seps1,seps2);    } } // //[A      string          of ,,tokens // //and some  more tokens,] //Tokens: // <A>, <string>, <of>, <>, <tokens>, <>, <and>, <some>, <more>, <tokens>, <>, //[1234| LIYI|China | 010 |201110260000|OK] //Tokens: // <1234>, <LIYI>, <China>, <010>, <201110260000>, <OK>, //[1234|LIYI||010|201110260000|OK] //Tokens: // <1234>, <LIYI>, <010>, <201110260000>, <OK>, //[1234|LIYI||010|201110260000|OK] //Tokens: // <1234>, <LIYI>, <>, <010>, <201110260000>, <OK>, //[a] //Tokens: // <a>, //[a,b] //Tokens: // <a>, <b>, //[a,,b] //Tokens: // <a>, <>, <b>, //[,a] //Tokens: // <>, <a>, //[a,] //Tokens: // <a>, <>, //[,a,,b] //Tokens: // <>, <a>, <>, <b>, //[,,a,,b,,] //Tokens: // <>, <>, <a>, <>, <b>, <>, <>, //[,] //Tokens: // <>, <>, //[,,] //Tokens: // <>, <>, <>, //[,,,] //Tokens: // <>, <>, <>, <>,