#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define CHLEN 10
void strcatf(char **, char *);
int main()
{
char * s = (char *)malloc(CHLEN);
s = "now is the time";
char * t = "now is the time";
strcatf(&s, t);
printf("%s\n", s);
printf("%s\n", t);
free(s);
return 0;
}
/* 将t志向的数据拼接到s指向的数据的结尾 */
void strcatf(char ** s, char * t)
{
int slen = 0;
while (**s++)
{
*s++;
slen++;
}
int tlen = 0;
while (*t++)
{
tlen++;
}
if (slen + tlen > CHLEN)
{
*s = (char *)realloc(*s, slen + tlen);
}
while (**s++ = *t++);
}
|
|
|
在unbuntu中用gcc编译运行后提示核心已转存错误,请各位指教
|
|
| 8分 |
。。。。。
char * s = (char *)malloc(CHLEN);//申请一块内存 s = “now is the time”; //指针指向常字符串“now is the time”(上面指针没有释放,已经内存泄漏) char * t = “now is the time”; strcatf(&s, t); //想把字符串 拷贝到 常字符串“now is the time” 的内存处,出错 printf(“%s\n”, s); |
|
s = “now is the time”;
改成 |
|
|
我将s = “now is the time”; 这句代码改了后,这里是不报错了。但strcatf函数里报错了,能看下strcatf函数里有什么错误吗。
|
|
|
看不太懂
判断结尾不是==””\ 0’么,while() 判断的是什么不懂 最后 两个指针都指向结尾了,为什么还要再赋值 |
|
|
\0就是代表0,0在while就是false。最后的代码是将t指向的数据逐个逐个加到s指向的数据后面
|
|
|
现在报错的地方是44行*s = (char *)realloc(*s, slen + tlen);报的错误时“无效的旧尺寸”。
|
|
| 12分 |
保存”now is the time”需要16个字节:
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#define CHLEN 16
void strcatf(char **, char *);
int main()
{
char * s = (char *)malloc(CHLEN);
strcpy(s,"now is the time");
char * t = "now is the time";
strcatf(&s, t);
printf("%s\n", s);
printf("%s\n", t);
free(s);
return 0;
}
/* 将指向的数据拼接到s指向的数据的结尾 */
void strcatf(char ** s, char * t)
{
int slen = 0;
while (**s++)
{
*s++;
slen++;
}
int tlen = 0;
while (*t++)
{
tlen++;
}
if (slen + tlen > CHLEN)
{
*s = (char *)realloc(*s, slen + tlen + 1);
}
while (**s++ = *t++);
}
|
|
其实电脑开机后物理内存的每个字节都是可读写的,从来不会因为所谓的new、delete或malloc、free而被创建、销毁。区别仅在于操作系统内存管理模块在你读写时是否能发现并是否采取相应动作而已。操作系统管理内存的粒度不是字节而是页,一页通常为4KB。
|
|
|
终于弄好了,这是最后我想要都的不报错的代码
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#define CHLEN 20
void strcatf(char **, char *);
int main()
{
char * s = (char *)malloc(CHLEN);
strcpy(s, "now is the time");
char * t = "now is the time";
strcatf(&s, t);
printf("%s\n", s);
printf("%s\n", t);
free(s);
return 0;
}
/* 将t志向的数据拼接到s指向的数据的结尾 */
void strcatf(char ** s, char * t)
{
char * j = *s;
int slen = 0;
while (*j++)
{
slen++;
}
char * i = t;
int tlen = 0;
while (*i++)
{
tlen++;
}
if (slen + tlen > CHLEN)
{
*s = (char *)realloc(*s, slen + tlen + 1);
}
while (*((*s) + (slen++)) = *t++);
}
|
|
|
这个我没看懂什么意思,有更详细的文档吗 |
|
|
我的意思就是你malloc了10个字节,非要往里放16个字节,操作系统不一定会报错。 #include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#define CHLEN 20
void strcatf(char **, char *);
int main()
{
char * s = (char *)malloc(CHLEN);
if (s==NULL) return 1;
strncpy(s, "now is the time",CHLEN-1);s[CHLEN-1]=0;
char * t = "now is the time";
strcatf(&s, t);
printf("%s\n", s);
printf("%s\n", t);
free(s);
return 0;
}
/* 将t志向的数据拼接到s指向的数据的结尾 */
void strcatf(char ** s, char * t)
{
char * j = *s;
int slen = 0;
while (*j++)
{
slen++;
}
char * i = t;
int tlen = 0;
while (*i++)
{
tlen++;
}
if (slen + tlen > CHLEN)
{
char *p=(char *)realloc(*s, slen + tlen + 1);
if (p==NULL) exit(1);
*s = p;
}
while (*((*s) + (slen++)) = *t++);
}
小心驶得万年船! |
|
|
非常感谢
|
|