c - realloc():strtok 操作中的下一个大小无效
问题描述
在我程序的这一部分中,我试图模仿 ftp 服务器中的 CDUP 操作。首先,我得到客户端所在的当前目录 (ptr2
和buf3
),然后我尝试剥离最后一个由分隔的字符串/
,以确定如果我们上一个目录的路径是什么。但是,只有当我进入另一个目录、回来并再次尝试 CDUP(即从根目录向上)时,我才会收到 realloc 错误
char *ptr2;
char buf3[255];
// Get current directory of application, store in buf3
if ((ptr2 = getcwd(buf3, sizeof(buf3))) != NULL) {
printf("Current working dir CDUP: %s\n", buf3);
} else {
perror("getcwd() error");
}
// Strip current directory off buf3
char *cdupSplit = strtok(buf3, "/");
char *cdupAccumulator = NULL;
char *newStr = calloc(1, strlen(cdupSplit));
while (cdupSplit != NULL) {
if (cdupAccumulator != NULL) {
newStr = realloc(newStr, strlen("/"));
newStr = realloc(newStr, strlen(cdupAccumulator));
strcat(newStr, "/");
strcat(newStr, cdupAccumulator);
}
cdupAccumulator = cdupSplit;
cdupSplit = strtok(NULL, "/");
}
...
free(newStr);
错误说realloc(): invalid next size: 0x0000000001ac0a20 ***
我不确定我哪里出错了,因为我正在释放newStr
变量并且它没有被传递到命令的下一个问题中。
解决方案
这部分没有多大意义:
if (cdupAccumulator != NULL) {
newStr = realloc(newStr, strlen("/"));
newStr = realloc(newStr, strlen(cdupAccumulator));
strcat(newStr, "/");
strcat(newStr, cdupAccumulator);
}
您不断向字符串添加新内容,但一次又一次地调整它的大小以仅保留最后一部分。
You seem to assume that realloc
increases the size be the given parameter. This is not the case. (Even then there is no space for terminating \0)
You need to keep track of the current size and add to this size accordingly.
For example like this:
if (cdupAccumulator != NULL) {
newStr = realloc(newStr, strlen(newStr) + strlen(cdupAccumulator) + 2);
strcat(newStr, "/");
strcat(newStr, cdupAccumulator);
}
推荐阅读
- java - 使用用户输入字符串而不是 HashMap 名称访问 HashMap 方法
- java - 如何每行打印越来越多的数组元素
- php - 正则表达式匹配打开和关闭的自定义标签
- sql - 如何编写正确的字段标准以消除“输入参数值”错误?
- python - 如何在 matplotlib 中绘制 3d 点数
- parse-platform - 为什么即使我已将解析导入 HTML 文件的头部,浏览器仍会告诉我解析未定义?
- dart - Using setState in separate BottomNotifcationBar class back to the main class
- ios - 如何找出 UITableView 中的哪个特定行收到了强制触摸事件?
- python - Why python not show in path icon?
- powershell - 窗口:命令获取当前磁盘速度输入/输出