[英]malloc Incorrect checksum error when trying to parse input as tokens
我正在嘗試創建一個 shell,但我一直遇到錯誤,
malloc:已釋放對象的校驗和不正確
在我的代碼以及測試時的分段錯誤中,是否有可能的解決方法? 我試過調試,但在代碼中找不到任何異常,有人可以指出我正確的方向嗎?
char **getArguments(char line[])
{
/* Pointer to char pointer for storing arguments, initial size is 1 */
char **args = malloc(sizeof(char *));
/* Error handling */
if (args == NULL)
{
fprintf(stderr, "Error: cannot split line.");
exit(EXIT_FAILURE);
}
int count = 0;
/* Try to parse first argument */
char *temp = strtok(line, " \t\n\r\a");
while (temp != NULL)
{
args[count] = temp;
/* Reallocate more space for next argument */
count++;
char **reallocated = realloc(args, count * sizeof(char *));
/* Error handling */
if (reallocated == NULL)
{
fprintf(stderr, "Error: cannot split line.");
exit(EXIT_FAILURE);
}
else
{
args = reallocated;
}
/* Move to next token */
temp = strtok(NULL, " \t\n\r\a");
}
/* NULL terminate the array so that we know where's the end */
args[count] = NULL;
return args;
}
count
初始化為 0,然后使用args[count]
賦值似乎是問題所在。
getArguments()
函數最初為參數創建一個單項數組,但隨后分配count = 0
- 這應該是1
,因為存在第一個元素。
接下來,代碼使用count
的全長進行賦值。 顯然,在 C 中,數組的索引從0
到length-1
,因此my_array[ length ]
永遠不會正確。
只需將count
初始化為 1 並將數組索引固定為 0-offset 即可解決問題。
/* Splits the command into arguments */
char **getArguments(char line[])
{
/* Pointer to char pointer for storing arguments, initial size is 1 */
char **args = malloc(sizeof(char *));
/* Error handling */
if (args == NULL)
{
fprintf(stderr, "Error: cannot split line.");
exit(EXIT_FAILURE);
}
int count = 1; // <-- HERE
/* Try to parse first argument */
char *temp = strtok(line, " \t\n\r\a");
while (temp != NULL)
{
args[count-1] = temp; // <-- HERE
/* Reallocate more space for next argument */
count++;
char **reallocated = realloc(args, count * sizeof(char *));
/* Error handling */
if (reallocated == NULL)
{
fprintf(stderr, "Error: cannot split line.");
exit(EXIT_FAILURE);
}
else
{
args = reallocated;
}
/* Move to next token */
temp = strtok(NULL, " \t\n\r\a");
}
/* NULL terminate the array so that we know where's the end */
args[count-1] = NULL; // <-- HERE
return args;
}
示例輸出(通過 Valgrind)
[user@machine]> valgrind ./run_cmds
==8173== Memcheck, a memory error detector
==8173== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==8173== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==8173== Command: ./run_cmds
==8173==
# /bin/echo foo
foo
# /bin/echo 1 2 3 4 5 6 7 8 9 10 11 12 13 14
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# ==8173==
==8173== HEAP SUMMARY:
==8173== in use at exit: 120 bytes in 1 blocks
==8173== total heap usage: 24 allocs, 23 frees, 3,544 bytes allocated
==8173==
==8173== LEAK SUMMARY:
==8173== definitely lost: 0 bytes in 0 blocks
==8173== indirectly lost: 0 bytes in 0 blocks
==8173== possibly lost: 0 bytes in 0 blocks
==8173== still reachable: 120 bytes in 1 blocks
==8173== suppressed: 0 bytes in 0 blocks
==8173== Rerun with --leak-check=full to see details of leaked memory
==8173==
==8173== For lists of detected and suppressed errors, rerun with: -s
==8173== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
也許您的代碼被指示使用realloc()
作為賦值或類似的東西,但更簡單的方法是簡單地計算參數的數量,將args
分配到正確的大小(完成一次),從而不進行循環解析重新分配。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.