C 控制流,内存分配:中止陷阱 6,用字符串中的字符填充数组,嵌套 for 循环
C control flow, memory allocation: Abort trap 6, populate array with characters in string, nested for loops
这是一个关于控制流和可能的内存分配的问题。
给定一个 long long
数字,我正在执行以下操作:
- 将其转换为字符串
- 以规定的模式遍历此字符串的字符(
for
循环的第一层)
- 对每个选定的字符执行操作(
for
循环的第二层)
- 用处理后的数据填充数组
#include <stdio.h>
#include <string.h>
int main(void)
{
long long n = 12345678; // I am given a number
char str[8]; // initialize string of length 8
sprintf(str, "%2lld", n); // convert n to string
printf("The string is: %s\n", str); // check that n is converted to string
int arr[4]; // initialize array of length 4
for (int i = 6; i >= 0; i -= 2) // select every other char in string, starting from second-to-last char
{
for (int j = 0; j < 4; j++) // select position of array
{
arr[j] = (str[i] - '0') * 2; // convert char to int, multiply by 2, and assign to array position
printf("The digit is %c and the product is %d\n", str[i], arr[j]); // announce each entry to the array
}
}
for (int k = 0; k < 4; k++) // print contents of array
{
printf("The product at position %d is %d\n", k, arr[k]);
}
return 0;
}
这段代码有两个问题:
- 在 MacOS 终端上 运行 时生成
Abort trap: 6
错误。
- 当代码在实践 IDE 环境中成功执行时,会生成以下结果:
The string is: 12345678
The digit is 7 and the product is 14
The digit is 7 and the product is 14
The digit is 7 and the product is 14
The digit is 7 and the product is 14
The digit is 5 and the product is 10
The digit is 5 and the product is 10
The digit is 5 and the product is 10
The digit is 5 and the product is 10
The digit is 3 and the product is 6
The digit is 3 and the product is 6
The digit is 3 and the product is 6
The digit is 3 and the product is 6
The digit is 1 and the product is 2
The digit is 1 and the product is 2
The digit is 1 and the product is 2
The digit is 1 and the product is 2
The product at position 0 is 2
The product at position 1 is 2
The product at position 2 is 2
The product at position 3 is 2
我想要的是解决 Abort trap: 6
错误并得到以下结果:
The string is: 12345678
The digit is 7 and the product is 14
The digit is 5 and the product is 10
The digit is 3 and the product is 6
The digit is 1 and the product is 2
The product at position 0 is 14
The product at position 1 is 10
The product at position 2 is 6
The product at position 3 is 2
我应该在代码中更改什么以实现该目标?
我已经阅读了其他关于 Abort trap: 6
的帖子,但我不知道这段代码是如何在内存中犯错误的 allocation/usage。
您不需要 for
循环来递增 j
作为数组索引,只需在开始时设置为 0
并在每个循环中递增它。
for (int i = 6, j = 0; i >= 0; i -= 2, j++) // select every other char in string, starting from second-to-last char
{
arr[j] = (str[i] - '0') * 2; // convert char to int, multiply by 2, and assign to array position
printf("The digit is %c and the product is %d\n", str[i], arr[j]); // announce each entry to the array
}
正如@Keine Lust 在评论中提到的,您必须为空终止字符分配 space。
char str[8];
改为
char str[9];
您的 str
数组不够长:
char str[8]; // initialize string of length 8
要保存一个包含 8 个字符的字符串,您需要一个包含 9 个字节的数组:8 个用于相关字符,一个用于终止空字节。
因为您没有为字符串预留足够的 space,所以您最终写到了数组的末尾。这样做会调用 undefined behavior,在本例中表现为崩溃。
增加数组的大小,以免踩到无效内存:
char str[9];
至于您看到的输出,您在外循环的每次迭代中都覆盖了 arr[j]
的值。不是嵌套循环,而是在 i
和 j
.
上迭代的单个循环
这是一个关于控制流和可能的内存分配的问题。
给定一个 long long
数字,我正在执行以下操作:
- 将其转换为字符串
- 以规定的模式遍历此字符串的字符(
for
循环的第一层) - 对每个选定的字符执行操作(
for
循环的第二层) - 用处理后的数据填充数组
#include <stdio.h>
#include <string.h>
int main(void)
{
long long n = 12345678; // I am given a number
char str[8]; // initialize string of length 8
sprintf(str, "%2lld", n); // convert n to string
printf("The string is: %s\n", str); // check that n is converted to string
int arr[4]; // initialize array of length 4
for (int i = 6; i >= 0; i -= 2) // select every other char in string, starting from second-to-last char
{
for (int j = 0; j < 4; j++) // select position of array
{
arr[j] = (str[i] - '0') * 2; // convert char to int, multiply by 2, and assign to array position
printf("The digit is %c and the product is %d\n", str[i], arr[j]); // announce each entry to the array
}
}
for (int k = 0; k < 4; k++) // print contents of array
{
printf("The product at position %d is %d\n", k, arr[k]);
}
return 0;
}
这段代码有两个问题:
- 在 MacOS 终端上 运行 时生成
Abort trap: 6
错误。 - 当代码在实践 IDE 环境中成功执行时,会生成以下结果:
The string is: 12345678
The digit is 7 and the product is 14
The digit is 7 and the product is 14
The digit is 7 and the product is 14
The digit is 7 and the product is 14
The digit is 5 and the product is 10
The digit is 5 and the product is 10
The digit is 5 and the product is 10
The digit is 5 and the product is 10
The digit is 3 and the product is 6
The digit is 3 and the product is 6
The digit is 3 and the product is 6
The digit is 3 and the product is 6
The digit is 1 and the product is 2
The digit is 1 and the product is 2
The digit is 1 and the product is 2
The digit is 1 and the product is 2
The product at position 0 is 2
The product at position 1 is 2
The product at position 2 is 2
The product at position 3 is 2
我想要的是解决 Abort trap: 6
错误并得到以下结果:
The string is: 12345678
The digit is 7 and the product is 14
The digit is 5 and the product is 10
The digit is 3 and the product is 6
The digit is 1 and the product is 2
The product at position 0 is 14
The product at position 1 is 10
The product at position 2 is 6
The product at position 3 is 2
我应该在代码中更改什么以实现该目标?
我已经阅读了其他关于 Abort trap: 6
的帖子,但我不知道这段代码是如何在内存中犯错误的 allocation/usage。
您不需要 for
循环来递增 j
作为数组索引,只需在开始时设置为 0
并在每个循环中递增它。
for (int i = 6, j = 0; i >= 0; i -= 2, j++) // select every other char in string, starting from second-to-last char
{
arr[j] = (str[i] - '0') * 2; // convert char to int, multiply by 2, and assign to array position
printf("The digit is %c and the product is %d\n", str[i], arr[j]); // announce each entry to the array
}
正如@Keine Lust 在评论中提到的,您必须为空终止字符分配 space。
char str[8];
改为
char str[9];
您的 str
数组不够长:
char str[8]; // initialize string of length 8
要保存一个包含 8 个字符的字符串,您需要一个包含 9 个字节的数组:8 个用于相关字符,一个用于终止空字节。
因为您没有为字符串预留足够的 space,所以您最终写到了数组的末尾。这样做会调用 undefined behavior,在本例中表现为崩溃。
增加数组的大小,以免踩到无效内存:
char str[9];
至于您看到的输出,您在外循环的每次迭代中都覆盖了 arr[j]
的值。不是嵌套循环,而是在 i
和 j
.