使用memcpy获取分段错误

使用memcpy获取分段错误,c,segmentation-fault,malloc,memcpy,C,Segmentation Fault,Malloc,Memcpy,我在malloc、指针数组和memcpy方面遇到了一些问题。我有一个叫做hex_string的字符串,它的长度总是可以被8整除。我试图将此字符串拆分为子字符串,每个子字符串包含8个字符。当字符串中有16个字符时,这很好,但如果我将其增加到24个字符及以上,则会出现分段错误。有人能帮我解释一下原因吗?我知道我使用了很多for循环,它们基本上运行相同的循环,我将对它们进行压缩,但我想先分别完成程序的每个部分 int main (int argc, char * argv[]) { cons

我在
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] = '\0';
    }

    /* 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;
}
intmain(intargc,char*argv[]){
常量字符*hex_string=“1C01111001F010100ABCDEF12”;
/*将此值用于十六进制字符串失败-但是如果
字符串替换为“1C01111001F010100”,程序运行正常*/
int string_length=strlen(十六进制字符串);
/*获取所需的8个字符子字符串的数目
(使用ceil允许我将来将其扩展到任意长度的字符串)*/
int num_of_blocks=(int)ceil((double)string_length/8);
/*为指向每个子字符串的指针分配内存*/
字符**十六进制数组=(字符**)malloc(块的数量*大小(字符));
/*为每个子字符串分配9个字节
允许8个字符和空终止符*/
对于(int i=0;i
主要问题是在内存分配中使用了错误的类型-这是一个常见错误

// char ** hex_array = (char **) malloc(num_of_blocks * sizeof(char));
char ** hex_array = (char **) malloc(num_of_blocks * sizeof(char *));
强烈同意,建议使用以下方法。不需要演员阵容。更容易编码。易于维护。在这种情况下,不太可能得到错误的类型

pointer_variable = malloc(n * sizeof *pointer_variable);

char ** hex_array = malloc(num_of_blocks * sizeof *hex_array);

主要问题是在内存分配中使用了错误的类型—这是一个常见错误

// char ** hex_array = (char **) malloc(num_of_blocks * sizeof(char));
char ** hex_array = (char **) malloc(num_of_blocks * sizeof(char *));
强烈同意,建议使用以下方法。不需要演员阵容。更容易编码。易于维护。在这种情况下,不太可能得到错误的类型

pointer_variable = malloc(n * sizeof *pointer_variable);

char ** hex_array = malloc(num_of_blocks * sizeof *hex_array);

sizeof(char)
-->
sizeof(char*)
对不起@Martin,没有啤酒给你。哈哈,这不公平:)@user3386109-谢谢,真不敢相信我没发现。现在一切正常。:)使用模式
p=malloc(N*sizeof*p)
有助于避免此问题,从视觉上看,您只需在sizeof之后直接检查
*
,而不必将其匹配回向左转换几个字符,而不是
int num\u of_blocks=(int)ceil((double)string\u length/8)
,您可以使用
int num\u of_blocks=(string\u length+7)/8避免浮点。
sizeof(char)
-->
sizeof(char*)
对不起@Martin,没有啤酒给你。哈哈,这不公平:)@user3386109-谢谢,真不敢相信我没发现。现在一切正常。:)使用模式
p=malloc(N*sizeof*p)
有助于避免此问题,从视觉上看,您只需在sizeof之后直接检查
*
,而不必将其匹配回向左转换几个字符,而不是
int num\u of_blocks=(int)ceil((double)string\u length/8)
,您可以使用
int num\u of_blocks=(string\u length+7)/8以避免浮点。。