c的字符串分割封装

l13615556434 2011-09-01 11:34:54
求大牛封装一下下面的字符分割函数啊,我写的有内存破坏的问题

typedef struct {

char ** command;
int length;

} command_split_data;

void t str_split(const char * src, const char * grep,
command_split_data * ret) {
int src_len = strlen(src);
char src_temp[src_len];
strcpy(src_temp, src);
int i = 0;
char * p;
ret->command = malloc(1);
printf("set\n");
p = strtok(src_temp, grep);
ret->command[0] = (char *) malloc(strlen(p));
ret->command[0] = p;
printf("set end\n");
i = 1;
while (p = strtok(NULL, grep)) {
ret->command[i] = (char *) malloc(strlen(p));
strcpy(ret->command[i], p);
printf("command %d : %s\n", i, ret->command[i]);
i++;
}
printf("return \n");
ret->length = i;
}
...全文
152 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
缺媳妇的郝 2011-09-01
  • 打赏
  • 举报
回复
学习了……
赵4老师 2011-09-01
  • 打赏
  • 举报
回复
VC调试(TC或BC用TD调试)时按Alt+8、Alt+6和Alt+5,打开汇编窗口、内存窗口和寄存器窗口看每句C对应的汇编、单步执行并观察相应内存和寄存器变化,这样过一遍不就啥都明白了吗。
(Linux或Unix下可以在用GDB调试时,看每句C对应的汇编并单步执行观察相应内存和寄存器变化。)
想要从本质上理解C指针,必须学习汇编以及C和汇编的对应关系。
从汇编的角度理解和学习C语言的指针,原本看似复杂的东西就会变得非常简单!
指针即地址。“地址又是啥?”“只能从汇编语言和计算机组成原理的角度去解释了。”

提醒:
“学习用汇编语言写程序”

“VC调试(TC或BC用TD调试)时按Alt+8、Alt+6和Alt+5,打开汇编窗口、内存窗口和寄存器窗口看每句C对应的汇编、单步执行并观察相应内存和寄存器变化,这样过一遍不就啥都明白了吗。
(Linux或Unix下可以在用GDB调试时,看每句C对应的汇编并单步执行观察相应内存和寄存器变化。)
想要从本质上理解C指针,必须学习C和汇编的对应关系。”
不是一回事!
wang_bird 2011-09-01
  • 打赏
  • 举报
回复
路过,字符分割成什么状态?
程序人生 2011-09-01
  • 打赏
  • 举报
回复
你的程序是想做什么?
l13615556434 2011-09-01
  • 打赏
  • 举报
回复
好的,接受大牛教诲!谢谢啊,新手,还不知道怎么给分呢!
赵4老师 2011-09-01
  • 打赏
  • 举报
回复
不要太崇拜动态分配,会产生内存碎片。
//使用动态分配
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
int i,L;
char *p;
void main() {
for (i=0;i<20000;i++) {
L=rand();
p=malloc(L);
if (NULL==p) {
printf("malloc error!\n");
continue;
}
memset(p,0,L);
free(p);
}
}
//不使用动态分配
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#define MAXLEN 30000
int i,L;
char buf[MAXLEN];
char *p;
void main() {
p=&buf[0];
for (i=0;i<20000;i++) {
L=rand();
if (L>MAXLEN) {
printf("L>MAXLEN==%d, ignore spilth.\n",MAXLEN);
L=MAXLEN;
}
memset(p,0,L);
}
}
l13615556434 2011-09-01
  • 打赏
  • 举报
回复
对的!一定要预先分配100给它吗?动态的分配能行得通么?大牛多指教啊!
赵4老师 2011-09-01
  • 打赏
  • 举报
回复
试试看吧。不保证对。
typedef struct {
char ** command;
int length;
}
command_split_data;

void str_split(const char * src, const char * grep,command_split_data * ret) {
int src_len = strlen(src);
assert(src_len<1000);
char src_temp[src_len];
strcpy(src_temp, src);
int i;
char * p;
ret->command =(char **)malloc(100*sizeof(char *));
assert(ret->command);
printf("set\n");
p = strtok(src_temp, grep);
assert(p);
ret->command[0] = (char *)malloc(strlen(p)+1);
strcpy(ret->command[0],p);
printf("command %d : %s\n", 0, ret->command[0]);
printf("set end\n");
i = 1;
while (p = strtok(NULL, grep)) {
ret->command[i] = (char *)malloc(strlen(p)+1);
strcpy(ret->command[i],p);
printf("command %d : %s\n", i, ret->command[i]);
i++;
if (i>=100) {
printf("up to 100 commands!\n");
break;
}
}
printf("return\n");
ret->length = i;
}

l13615556434 2011-09-01
  • 打赏
  • 举报
回复
好东西,这个可以收藏,但是我的问题还是没有解决啊。。。
hongwenjun 2011-09-01
  • 打赏
  • 举报
回复
#include <iostream>
#include <stdio.h>
#include <string.h>
const int SIZE = 1024 * 8;
char APPBUF[SIZE]; // 设置缓冲区 APPBUF
// 本函数完成从 分词里找最长的分词,使用缓冲区,避免了内存泄漏
char* search(const char* chbuf , const char* val);


int main()
{
const char* str = "我/爱/中珅/||我/爱珅/中文/||我爱1234567890/中/文/||我爱/中文/分/||我爱/中文/分词/";
char* pch = search(str , "|");
printf("%s\n", pch);
printf("%s\n", search("http://topic.csdn.net/u/20110822/20/e369cc10-5392-4763-b2cd-da4995b2cafb.html" , "/"));
printf("%s.png\n", search("http://hi.csdn.net/attachment/201108/22/3362235_1313993745S1YP.png" , "/."));
return 0;
}


char* search(const char* chbuf , const char* val)
{
char* ret = APPBUF; // 使用缓冲区,先清0
memset(ret, 0, SIZE);
char* pch = NULL;
char* funbuf = new char[strlen(chbuf) + 1];
strcpy(funbuf, chbuf);


pch = strtok(funbuf, val);
while (pch != NULL) {
if (strlen(ret) < strlen(pch)) { // 获取最长的字符串单元
strcpy(ret, pch);
}
pch = strtok(NULL, val);
}
delete[] funbuf;
return ret;
}


我这个是只返回 一个 C风格字符串的,使用了 一个 buf
hongwenjun 2011-09-01
  • 打赏
  • 举报
回复
用C就麻烦一些了
char str[][] 这样操作起来比较麻烦
C++ 的 容器string 就简单点
l13615556434 2011-09-01
  • 打赏
  • 举报
回复
strstr 是查找str在字符串中第一次出现的位置,可以配合strncpy来分割,但是我想获得的是一个字符串数组,然后在为其它的调用提供,是这样的。
hongwenjun 2011-09-01
  • 打赏
  • 举报
回复
2;3;4;5;6;7;8;

strstr 可以切割的

楼主详细问下
l13615556434 2011-09-01
  • 打赏
  • 举报
回复
是想实现字符串分割啊,比如2;3;4;5;6;7;8这样的字符串,以;分割开来形成一个字符串的数组,放到ret里面,返回出去。
ret是要返回的,这里只有指针p没有被释放,其他的应该都不用释放,在外面用完了之后统一释放ret指针的,初步想是这样想,大牛们帮帮忙啊,是linux c的
猫xiaowai 2011-09-01
  • 打赏
  • 举报
回复
问题1:有malloc,没有free

69,379

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧