Realloc问题
感谢您的关注,请忽略-各种各样的恶作剧正在发生,我正在尝试调试更多 ============================================== 有人能解释realloc的这种行为吗 输出:Realloc问题,c,realloc,C,Realloc,感谢您的关注,请忽略-各种各样的恶作剧正在发生,我正在尝试调试更多 ============================================== 有人能解释realloc的这种行为吗 输出: before realloc start: testing%20encryp before realloc app: ' ' realloc size: 27 after realloc: testing%20e strlen(newstr): 11 newstr: tes
before realloc start: testing%20encryp
before realloc app: ' '
realloc size: 27
after realloc: testing%20e
strlen(newstr): 11
newstr: testing%20e
代码:
它在realloc之后从start中删除“ncryp”;但这不应该发生
编辑:更多代码,更多输出
char * urlEncode(char * c)
{
#ifdef EBUG
printf("urlEncode: Encoding '%s'\n", c);
#endif
int len = strlen(c)+1;
char * ret = malloc(sizeof(char) * len);
memset(ret, 0, len);
int z=0;
char * escapee = malloc(sizeof(char) * 4);
escapee[0] = '%'; escapee[3] = '\0';
for(int i=0;i<strlen(c);i++)
{
printf("z = %i len = %i ret = %s\n", z, len, ret);
if(z >= len)
{
ret = strAppend(ret, " ");
len += strlen(" ");
}
printf("z = %i len = %i ret = %s\n", z, len, ret);
if ( (48 <= c[i] && c[i] <= 57) ||//0-9
(65 <= c[i] && c[i] <= 90) ||//abc...xyz
(97 <= c[i] && c[i] <= 122) || //ABC...XYZ
(c[i]=='~' || c[i]=='!' || c[i]=='*' || c[i]=='(' || c[i]==')' || c[i]=='\'')
)
{
ret[z++] = c[i];
}
else
{
char2hex(c[i], escapee);
ret = strAppend(ret, escapee);
z += 3;
}
}
ret[z] = '\0';
free(escapee);
#ifdef EBUG
printf("urlEncode: Encoded string to '%s'\n", c);
#endif
return ret;
}
urlEncode: Encoding 'testing encrypt'
z = 0 len = 16 ret =
z = 0 len = 16 ret =
z = 1 len = 16 ret = t
z = 1 len = 16 ret = t
z = 2 len = 16 ret = te
z = 2 len = 16 ret = te
z = 3 len = 16 ret = tes
z = 3 len = 16 ret = tes
z = 4 len = 16 ret = test
z = 4 len = 16 ret = test
z = 5 len = 16 ret = testi
z = 5 len = 16 ret = testi
z = 6 len = 16 ret = testin
z = 6 len = 16 ret = testin
z = 7 len = 16 ret = testing
z = 7 len = 16 ret = testing
before realloc start: testing
before realloc app: '%20'
realloc size: 11
after realloc: testing
strlen(newstr): 10
newstr: testing%20
z = 10 len = 16 ret = testing%20
z = 10 len = 16 ret = testing%20
z = 11 len = 16 ret = testing%20e
z = 11 len = 16 ret = testing%20e
z = 12 len = 16 ret = testing%20en
z = 12 len = 16 ret = testing%20en
z = 13 len = 16 ret = testing%20enc
z = 13 len = 16 ret = testing%20enc
z = 14 len = 16 ret = testing%20encr
z = 14 len = 16 ret = testing%20encr
z = 15 len = 16 ret = testing%20encry
z = 15 len = 16 ret = testing%20encry
z = 16 len = 16 ret = testing%20encryp
before realloc start: testing%20encryp
before realloc app: ' '
realloc size: 27
after realloc: testing%20encryp
strlen(newstr): 26
newstr: testing%20encryp
z = 16 len = 26 ret = testing%20encryp
char*urlEncode(char*c)
{
#ifdef EBUG
printf(“urlEncode:编码“%s”\n”,c);
#恩迪夫
int len=strlen(c)+1;
char*ret=malloc(sizeof(char)*len);
memset(ret,0,len);
int z=0;
char*escapee=malloc(sizeof(char)*4);
逃犯[0]='%';逃犯[3]='\0';
for(int i=0;i=len)
{
ret=带端(ret,“”);
len+=strlen(“”);
}
printf(“z=%i len=%i ret=%s\n”,z,len,ret);
if((48realloc
的第一个参数必须是以前由malloc
、calloc
或realloc
返回的指针,而不是随后的free
d
如果不是这样,任何事情都可能发生,包括你所看到的
从哪里开始
来自哪里
编辑:发布您的编辑后,您似乎没有遇到realloc问题!我认为您试图做的是包含两个字符串start和app,如果是这种情况,您最好使用strncat函数
#include <cstring>
char *strncat( char *str1, const char *str2, size_t count );
#包括
char*strncat(char*str1,const char*str2,size\u t count);
既然您原来的realloc问题似乎无法重现,我敢发布一个稍微经过重构的代码来满足您的要求。它在字符串上传递了两次,但在我看来,就执行的内存分配调用数量而言,它应该具有更可预测的性能—仅一次。它也是一个bi它不比你的短
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int is_printable(char c) {
return (48 <= c && c <= 57) ||//0-9
(65 <= c && c <= 90) ||//abc...xyz
(97 <= c && c <= 122) || //ABC...XYZ
c == '~' || c =='!' || c== '*' ||
c == '(' || c== ')' || c== '\'';
}
char *urlEncode(char *s) {
char *ret, *c, *ct;
int i, len;
printf("urlEncode: Encoding '%s'\n", s);
/* First pass - figure out how long the target string should be */
len = 0;
for(c=s; *c; c++) {
if(is_printable(*c)) len++; else len += 3;
}
/* Don't forget we need to store terminating zero too */
len++;
printf("Current len: %d, target len: %d\n", strlen(s)+1, len);
ct = ret = malloc(len);
/* Second pass - copy/encode */
for(c=s; *c; c++) {
if(is_printable(*c)) {
*ct++ = *c;
} else {
snprintf(ct, 4, "%%%02x", *c);
ct += 3;
}
}
*ct = 0; /* null-terminate the string */
printf("Encoded string: %s\n", ret);
return ret;
}
int main(int argc, char *argv[])
{
urlEncode("testing encrypt");
exit(1);
}
#包括
#包括
#包括
int可打印(字符c){
返回(48
在我的系统中,分配的字符串newstr的地址等于start,不管“start”的分配大小是多少,很可能当它增长时,系统正在重新分配相同的内存位置你能发布调用代码吗?特别是,start从何而来?IIRC sizeof(char)始终为1。我希望您通过执行realloc(开始…)
,start可能会变成无效指针。您的新输出完全不同,并且似乎没有遇到您之前发布的问题?@Charles:是的。我无法解释。我正在尝试找出更多的东西。这很奇怪,我没有更改任何我不认为的内容……对,但我想处理内存管理和在函数内部增加str1,而不是在我调用strncatI之前。strncat不明白你的意思,strncat会为你增加字符串。strncat不会增加底层内存区域。它只会在第一个字符串后附加第二个字符串,并依赖调用方为其提供适当的限制以防止任何缓冲区溢出。“destination-指向目标数组的指针,该数组应该包含一个C字符串,并且足够大,可以包含连接的结果字符串,包括额外的空字符。”谢谢,这样更好。但是有一个小错误:它似乎翻转了编码值:“encoded string:testing%02encrypt”“不是%20,谢谢。格式字符串是错误的-它没有翻转部件,它总是打印%02:)“man snprintf”是我的朋友…不完全正确。第一个参数可能是NULL
。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int is_printable(char c) {
return (48 <= c && c <= 57) ||//0-9
(65 <= c && c <= 90) ||//abc...xyz
(97 <= c && c <= 122) || //ABC...XYZ
c == '~' || c =='!' || c== '*' ||
c == '(' || c== ')' || c== '\'';
}
char *urlEncode(char *s) {
char *ret, *c, *ct;
int i, len;
printf("urlEncode: Encoding '%s'\n", s);
/* First pass - figure out how long the target string should be */
len = 0;
for(c=s; *c; c++) {
if(is_printable(*c)) len++; else len += 3;
}
/* Don't forget we need to store terminating zero too */
len++;
printf("Current len: %d, target len: %d\n", strlen(s)+1, len);
ct = ret = malloc(len);
/* Second pass - copy/encode */
for(c=s; *c; c++) {
if(is_printable(*c)) {
*ct++ = *c;
} else {
snprintf(ct, 4, "%%%02x", *c);
ct += 3;
}
}
*ct = 0; /* null-terminate the string */
printf("Encoded string: %s\n", ret);
return ret;
}
int main(int argc, char *argv[])
{
urlEncode("testing encrypt");
exit(1);
}
char * strAppend(char * start, char * app)
{
int i=strlen(start);
int j=0;
printf("before realloc start: %s\n", start);
printf("before realloc app: '%s'\n", app);
printf("realloc size: %i\n", i+strlen(app)+1);
char * newstr =(void*) realloc(start, sizeof(char) * (i + strlen(app) + 1));
memset(newstr,'z',sizeof(char) * (i + strlen(app) + 1));
printf("addres %x\n",newstr);
printf("after realloc: %s\n", newstr);
while(app[j] != '\0')
newstr[i++] = app[j++];
//missing null terminating character
newstr[i]=0;
printf("strlen(newstr): %i\n", strlen(newstr));
printf("newstr: %s\n", newstr);
return newstr;
}