简体   繁体   English

C malloc(分段错误:11)

[英]C malloc (Segmentation fault: 11)

I'm trying to understand malloc but I keep getting "Segmentation fault: 11" with this piece of code: 我试图理解malloc,但是通过这段代码我不断收到“ Segmentation fault:11”:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i = 0, j = 0;
    char ** ptr = (char **) malloc(sizeof(char*));

    for(i = 0; i < 5; i++)
    {
        for(j = 0; j < 10; j++)
            ptr[i][j] = 'a';

        printf("%s\n", ptr[i]);
    }

    return 0;
}

I thought there wasn't enough bytes being allocated so I did malloc(sizeof(char*) * 100 , but gives me the same error. What am I not understanding here? 我以为没有分配足够的字节,所以我做了malloc(sizeof(char*) * 100 ,但是给了我同样的错误,我在这里不明白什么?

When you allocate a 2D array, you need to allocate the individual sub-arrays as well. 分配2D数组时,还需要分配各个子数组。 In addition, you need to say how many elements you wish to have. 另外,您需要说出您希望拥有多少个元素。 For that you multiply the desired count by the number of elements, like this: 为此,将所需的计数乘以元素数,如下所示:

char ** ptr = (char **) malloc(5*sizeof(char*));
// Size=5 ---------------------^
for(int i = 0; i < 5; i++) {
    ptr[i] = malloc(11*sizeof(char));
    // sizeof(char) is always 1, so the multiplication above is redundant.
    // You need 11 elements for ten characters
    for(int j = 0; j < 10; j++) {
        ptr[i][j] = 'a';
    }
    // don't forget to null-terminate the string:
    ptr[i][10] = '\0';
    printf("%s\n", ptr[i]);
}

Your code is totally messed up in every aspect! 您的代码在各个方面都一团糟!

1) you allocated memory for exactly 1 Pointer to it. 1)您为它分配了恰好1个指针的内存。 This means you can access ptr[0], but not ptr[1] ... ptr[4] as you are trying to do. 这意味着您可以尝试访问ptr [0],但不能访问ptr [1] ... ptr [4]。

2) you never allocate anything for the elements in ptr[i]. 2)您永远不会为ptr [i]中的元素分配任何内容。

3) you try to print a string a ptr[i] which is (even if your allocation would be right) never terminated. 3)您尝试打印一个ptr [i]字符串(即使您的分配正确)也不会终止。

4) although this is obviously only a beginners test, never forget to free your memory!!!! 4)虽然这显然只是初学者的测试,但请不要忘记释放您的记忆!

To reach something CLOSE to what your sampel code is describing you could do: 要达到与示例代码描述的内容接近的方式,您可以执行以下操作:

int main() 
{
int i,j;
char ** ptr = malloc( 5 * sizeof(char*) ); /* an array of 5 elements of type char* */
for(i = 0; i < 5; i++)
{
    ptr[i] =  malloc( 11*sizeof(char) ); /* The element i of the array is an array of 11 chars (10 for the 'a' character, one for the null-termination */
    for(j = 0; j < 10; j++)
        ptr[i][j] = 'a';
    ptr[i][10] = '\0'; /* strings need to be null terminated */

    printf("%s\n", ptr[i]);
}
// free your memory!
for (i=0; i<5; i++ )
{
    free(ptr[i]);
}
free(ptr);

return 0;

Another way to allocate the memory is: 分配内存的另一种方法是:

char (*ptr)[11] = malloc( sizeof(char[5][11]) );

for(i = 0; i < 5; i++)
{
    for(j = 0; j < 10; j++)
        ptr[i][j] = 'a';

    ptr[i][10] = 0;
    printf("%s\n", ptr[i]);
}

free(ptr);

It seems less hassle to use a single allocation than to use a lot of allocations, unless you have a pressing reason to do the latter. 除非您有紧迫的理由进行单一分配,否则使用单一分配似乎比使用大量分配的麻烦要少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM