使用 memcpy 获取分段错误
Getting segmentation fault with memcpy
我在使用 malloc
、指针数组和 memcpy
时遇到了一些问题。我有一个名为 hex_string 的字符串,它的长度总是可以被 8 整除。我试图将这个字符串拆分成子字符串,每个子字符串有 8 个字符。当我在字符串中有 16 个字符时,这工作正常,但如果我将其增加到 24 个字符或更多,我会遇到分段错误。任何人都可以帮我解决这个问题吗?我知道我使用了很多本质上 运行 相同循环的 for 循环,我将压缩这些但我想分别执行程序的每个部分以开始。
int main (int argc, char * argv[]) {
const char * hex_string = "1c0111001f010100abcdef12";
/* Using this value for hex_string fails - but if the
string is replaced with "1c0111001f010100" the program runs fine. */
int string_length = strlen(hex_string);
/* get the number of 8 character substrings needed
(using ceil to allow me to expand this to arbitrary length strings in the future) */
int num_of_blocks = (int)ceil((double)string_length / 8);
/* allocate memory for pointers to each substring */
char ** hex_array = (char **) malloc(num_of_blocks * sizeof(char));
/* allocate 9 bytes for each substring
to allow for the 8 characters and the null-terminator. */
for (int i = 0; i < num_of_blocks; i++)
hex_array[i] = (char *) malloc(9);
/* split the strings into 8-character substrings and add a null-terminator */
for (int i = 0; i < num_of_blocks; i++) {
memcpy(hex_array[i], hex_string+(i*8), 8);
(hex_array[i])[8] = '[=10=]';
}
/* print each substring */
for (int i = 0; i < num_of_blocks; i++)
printf("substring %d = %s\n",i,hex_array[i]);
/* free the memory allocated for each substring */
for (int i = 0; i < num_of_blocks; i++)
free(hex_array[i]);
/* free the memory allocated for the pointers to the substrings */
free(hex_array);
return 0;
}
主要问题是在内存分配中使用了错误的类型 - 这是一个常见错误。
// char ** hex_array = (char **) malloc(num_of_blocks * sizeof(char));
char ** hex_array = (char **) malloc(num_of_blocks * sizeof(char *));
非常同意@Matt McNabb,推荐使用以下内容。不需要演员表。更容易编码。更容易维护。在这种情况下,不太可能得到错误的类型。
pointer_variable = malloc(n * sizeof *pointer_variable);
char ** hex_array = malloc(num_of_blocks * sizeof *hex_array);
我在使用 malloc
、指针数组和 memcpy
时遇到了一些问题。我有一个名为 hex_string 的字符串,它的长度总是可以被 8 整除。我试图将这个字符串拆分成子字符串,每个子字符串有 8 个字符。当我在字符串中有 16 个字符时,这工作正常,但如果我将其增加到 24 个字符或更多,我会遇到分段错误。任何人都可以帮我解决这个问题吗?我知道我使用了很多本质上 运行 相同循环的 for 循环,我将压缩这些但我想分别执行程序的每个部分以开始。
int main (int argc, char * argv[]) {
const char * hex_string = "1c0111001f010100abcdef12";
/* Using this value for hex_string fails - but if the
string is replaced with "1c0111001f010100" the program runs fine. */
int string_length = strlen(hex_string);
/* get the number of 8 character substrings needed
(using ceil to allow me to expand this to arbitrary length strings in the future) */
int num_of_blocks = (int)ceil((double)string_length / 8);
/* allocate memory for pointers to each substring */
char ** hex_array = (char **) malloc(num_of_blocks * sizeof(char));
/* allocate 9 bytes for each substring
to allow for the 8 characters and the null-terminator. */
for (int i = 0; i < num_of_blocks; i++)
hex_array[i] = (char *) malloc(9);
/* split the strings into 8-character substrings and add a null-terminator */
for (int i = 0; i < num_of_blocks; i++) {
memcpy(hex_array[i], hex_string+(i*8), 8);
(hex_array[i])[8] = '[=10=]';
}
/* print each substring */
for (int i = 0; i < num_of_blocks; i++)
printf("substring %d = %s\n",i,hex_array[i]);
/* free the memory allocated for each substring */
for (int i = 0; i < num_of_blocks; i++)
free(hex_array[i]);
/* free the memory allocated for the pointers to the substrings */
free(hex_array);
return 0;
}
主要问题是在内存分配中使用了错误的类型 - 这是一个常见错误。
// char ** hex_array = (char **) malloc(num_of_blocks * sizeof(char));
char ** hex_array = (char **) malloc(num_of_blocks * sizeof(char *));
非常同意@Matt McNabb,推荐使用以下内容。不需要演员表。更容易编码。更容易维护。在这种情况下,不太可能得到错误的类型。
pointer_variable = malloc(n * sizeof *pointer_variable);
char ** hex_array = malloc(num_of_blocks * sizeof *hex_array);