memset段错误的问题~~~

a_sum_of_money 2011-08-19 02:34:55
写了一个函数 目标是
在串中截取指定的内容:
初步写的代码如下:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

static int look_for_keyword(char **pStandbyData, int *npStandbyDataLen, char *keyword);

int main(int argc, const char *argv[])
{
char *source="content-Disposition: form-data; name=\"subType\"\
\
1\
-----------------------------7db2221ba0d2e";

int length = strlen(source);
printf("%d\n", length);

look_for_keyword(&source, &length, "subType"); //查找关键字subType后的内容

printf("%s\n", source);

return 0;
}

static int look_for_keyword(char **pStandbyData, int *npStandbyDataLen, char *middle_keyword)
{
int length = 0; //记录full_keyword的长度

char *p = *pStandbyData; //指针p指向串头
char *content = NULL; //content保存截取的内容
content = (char *)malloc(sizeof(char) * 1024); //分配内存
memset(content, '\0', 1024);
char *q = content; //指针q指向content串头

char *head_keyword = "name=\""; //查找关键字头

char *full_keyword = NULL; //需要查找的关键字
full_keyword = (char *)malloc(sizeof(char) * 80); //分配内存
memset(full_keyword, '\0', 80); //初始化
strcpy(full_keyword, head_keyword);
strcat(full_keyword, middle_keyword);

length = strlen(full_keyword);

p = strstr(*pStandbyData, full_keyword); //p指向查找的关键字头
p += length; //p指向要截取的内容头

while(*p != '-') {
*q = *p;
p++;
q++;
}
*q= '\0';

memset(*pStandbyData, '\0', *npStandbyDataLen);
*npStandbyDataLen = a;

strcpy(*pStandbyData, content);

free(content);
free(full_keyword);

return 0;
}



用gdb调试了下:
程序总是在memset(*pStandbyData, '\0', *npStandbyDataLen);的地方报段错误~~~
段错误不是说可能是操作了地址错误吗??? *pStandbyData有什么问题不
我把memset(*pStandbyData, '\0', *npStandbyDataLen)注释后
又在strcpy(*pStandbyData, content);下报了错
没搞懂啊~~~~
求解~~~
我的环境是ubuntu 10.10 gcc编译滴
...全文
470 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
li3208 2011-08-19
  • 打赏
  • 举报
回复
mark
赵4老师 2011-08-19
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>
int main() {
char *p;
char *source="content-Disposition: form-data; name=\"subType\"1 -----------------------------7db2221ba0d2e";

p=strstr(source,"subType");
if (p) printf("%s\n",p+7);//subType后的内容即"1 -----------------------------7db2221ba0d2e
return 0;
}
a_sum_of_money 2011-08-19
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 wyfwx 的回复:]

引用 9 楼 a_sum_of_money 的回复:
引用 4 楼 shi3590 的回复:

char *source=“XXX”;
改为char source[]="XXX";

给个解释呗
按这种方法编译时出现了
look_for_keyword.c: In function ‘main’:
look_for_keyword.c:17: warning: passing argum……
[/Quote]
wyfwx 2011-08-19
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 a_sum_of_money 的回复:]
引用 4 楼 shi3590 的回复:

char *source=“XXX”;
改为char source[]="XXX";


按这种方法编译时出现了
look_for_keyword.c: In function ‘main’:
look_for_keyword.c:17: warning: passing argument 1 of ‘look_for_keyword’ f……
[/Quote]

char *p = source;

look_for_keyword(&p, &length, "subType");
wyfwx 2011-08-19
  • 打赏
  • 举报
回复
"content-Disposition: form-data; name=\"subType\"\
\
1\
-----------------------------7db2221ba0d2e";
不让你修改。

char source[]="content-Disposition: form-data; name=\"subType\"\
\
1\
-----------------------------7db2221ba0d2e";
a_sum_of_money 2011-08-19
  • 打赏
  • 举报
回复
没人来了???
顶起啊 这好像就是数组与指针的差异了吧

高手们讲讲
a_sum_of_money 2011-08-19
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 shi3590 的回复:]

char *source=“XXX”;
改为char source[]="XXX";
[/Quote]

按这种方法编译时出现了
look_for_keyword.c: In function ‘main’:
look_for_keyword.c:17: warning: passing argument 1 of ‘look_for_keyword’ from incompatible pointer type
look_for_keyword.c:5: note: expected ‘char **’ but argument is of type ‘char (*)[92]’

的情况,,,,,,,,,,,,,,,,,,,,,,,,,,,不知原因
a_sum_of_money 2011-08-19
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 tianmo2010 的回复:]

*npStandbyDataLen = a;

只是不知道这个a,LZ怎么用的。。。
[/Quote]

忘了注释掉了~~呵
newfinder 2011-08-19
  • 打赏
  • 举报
回复
1、malloc分配内存完后,先检查一下分配是否成功:if ( full_keyword != NULL ) { …… }
2、*npStandbyDataLen = a; a在哪定义的。????
3、第二个memset()可以去掉了,因为你接着strcpy(),
4、strcpy和strcat等函数运行前,先检查一下源字符串的长度.windows下有strcpy_s和strcat_s专门来防缓冲区溢出;linux貌似更严格。
tianmo2010 2011-08-19
  • 打赏
  • 举报
回复
*npStandbyDataLen = a;

只是不知道这个a,LZ怎么用的。。。

tianmo2010 2011-08-19
  • 打赏
  • 举报
回复
因为memset函数的第一个参数为地址,而LZ用的*pStandbyData表示取地址里面的值。。。
shi3590 2011-08-19
  • 打赏
  • 举报
回复
char *source=“XXX”;
改为char source[]="XXX";
tianmo2010 2011-08-19
  • 打赏
  • 举报
回复
memset(*pStandbyData, '\0', *npStandbyDataLen);

把红色的改为:pStandbyData 就可以了。

即这样子:memset(pStandbyData, '\0', *npStandbyDataLen);

yarpee 2011-08-19
  • 打赏
  • 举报
回复
*pStandbyData指向的不是字符串常量?常量!
luciferisnotsatan 2011-08-19
  • 打赏
  • 举报
回复
char *source="content-Disposition: form-data; name=\"
source指向字符串字面量,在只读区里。

69,336

社区成员

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

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