Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C malloc(分段故障:11)_C_Arrays_Memory Management_Malloc - Fatal编程技术网

C malloc(分段故障:11)

C malloc(分段故障:11),c,arrays,memory-management,malloc,C,Arrays,Memory Management,Malloc,我试图理解malloc,但我一直在用这段代码得到“分段错误: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++)

我试图理解malloc,但我一直在用这段代码得到“分段错误: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;
}
#包括
#包括
int main()
{
int i=0,j=0;
char**ptr=(char**)malloc(sizeof(char*));
对于(i=0;i<5;i++)
{
对于(j=0;j<10;j++)
ptr[i][j]=“a”;
printf(“%s\n”,ptr[i]);
}
返回0;
}

我认为分配的字节不够,所以我选择了malloc(sizeof(char*)*100,但给出了相同的错误。我在这里不明白什么?

分配2D数组时,还需要分配单个子数组。此外,还需要说明希望拥有多少个元素。为此,将所需计数乘以元素数,如下所示:

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]);
}
char**ptr=(char**)malloc(5*sizeof(char*);
//尺寸=5---------------------^
对于(int i=0;i<5;i++){
ptr[i]=malloc(11*sizeof(char));
//sizeof(char)始终为1,因此上面的乘法是多余的。
//十个字符需要11个元素
对于(int j=0;j<10;j++){
ptr[i][j]=“a”;
}
//不要忘记使用null终止字符串:
ptr[i][10]='\0';
printf(“%s\n”,ptr[i]);
}

分配2D数组时,还需要分配各个子数组。此外,还需要说明希望有多少个元素。为此,将所需计数乘以元素数,如下所示:

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]);
}
char**ptr=(char**)malloc(5*sizeof(char*);
//尺寸=5---------------------^
对于(int i=0;i<5;i++){
ptr[i]=malloc(11*sizeof(char));
//sizeof(char)始终为1,因此上面的乘法是多余的。
//十个字符需要11个元素
对于(int j=0;j<10;j++){
ptr[i][j]=“a”;
}
//不要忘记使用null终止字符串:
ptr[i][10]='\0';
printf(“%s\n”,ptr[i]);
}

您的代码在各个方面都乱七八糟

1) 您只为指向它的1个指针分配了内存。这意味着您可以访问ptr[0],但不能访问ptr[1]。。。ptr[4]正如您所尝试的那样

2) 您从不为ptr[i]中的元素分配任何内容

3) 您尝试打印一个ptr[i]字符串,该字符串(即使您的分配正确)从未终止

4) 虽然这显然只是一个初学者的测试,但千万别忘了释放你的记忆

要接近sampel代码所描述的内容,可以执行以下操作:

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;
intmain()
{
int i,j;
char**ptr=malloc(5*sizeof(char*);/*由5个char*类型的元素组成的数组*/
对于(i=0;i<5;i++)
{
ptr[i]=malloc(11*sizeof(char));/*数组的元素i是一个11个字符的数组(10个字符表示“a”字符,一个字符表示空终止符)*/
对于(j=0;j<10;j++)
ptr[i][j]=“a”;
ptr[i][10]='\0';/*字符串需要以null结尾*/
printf(“%s\n”,ptr[i]);
}
//释放你的记忆!

对于(i=0;i您的代码在各个方面都是一团糟

1) 您为指向它的1个指针分配了内存。这意味着您可以访问ptr[0],但不能像您尝试的那样访问ptr[1]…ptr[4]

2) 您从不为ptr[i]中的元素分配任何内容

3) 您尝试打印一个ptr[i]字符串,该字符串(即使您的分配正确)从未终止

4) 虽然这显然只是一个初学者的测试,但千万别忘了释放你的记忆

要接近sampel代码所描述的内容,可以执行以下操作:

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;
intmain()
{
int i,j;
char**ptr=malloc(5*sizeof(char*);/*由5个char*类型的元素组成的数组*/
对于(i=0;i<5;i++)
{
ptr[i]=malloc(11*sizeof(char));/*数组的元素i是一个11个字符的数组(10个字符表示“a”字符,一个字符表示空终止符)*/
对于(j=0;j<10;j++)
ptr[i][j]=“a”;
ptr[i][10]='\0';/*字符串需要以null结尾*/
printf(“%s\n”,ptr[i]);
}
//释放你的记忆!

对于(i=0;i另一种分配内存的方法是:

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);
char(*ptr)[11]=malloc(sizeof(char[5][11]);
对于(i=0;i<5;i++)
{
对于(j=0;j<10;j++)
ptr[i][j]=“a”;
ptr[i][10]=0;
printf(“%s\n”,ptr[i]);
}
免费(ptr);

使用单个分配似乎比使用大量分配要简单,除非您有迫切的理由执行后者。

另一种分配内存的方法是:

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);
char(*ptr)[11]=malloc(sizeof(char[5][11]);
对于(i=0;i<5;i++)
{
对于(j=0;j<10;j++)
ptr[i][j]=“a”;
ptr[i][10]=0;
printf(“%s\n”,ptr[i]);
}
免费(ptr);

使用单个分配似乎比使用大量分配要简单,除非你有迫切的理由去做后者。

ptr[i]
是一个指针,但它指向什么?它不指向分配的内存的开始吗?如果我只做
char*ptr=(char*)malloc(sizeof(char))
去掉第二个循环语句,它可以正常工作。
ptr
可以,但是
ptr[0]
不能。不要在C!@Olaf中使用
malloc
&friends的结果。你什么意思?不要使用?我不明白。
ptr[I]
是一个指针,但它指向什么?它不是指向分配的内存的开始吗?如果我只做
char*ptr=(char*)malloc(sizeof(char))
并去掉第二个循环语句,它工作正常。
ptr
指向,但
ptr[0]
没有。不要在C中使用
malloc
&friends!Olaf!你什么意思?不要使用?我不明白。谢谢,我从来都不知道
free()
。我应该在使用malloc?malloc“reserves”初始化的任何指针后使用它内存。如果你不释放它,你的程序会使用越来越多的内存,永远不会把它还给操作系统。每个“malloc()”应该在代码中的某个地方有一个匹配的“free()”。而且这在你的程序中不是一个真正的问题