简体   繁体   English

Malloc 内部结构失效 function

[英]Malloc of a structure failure inside a function

I have a struct:我有一个结构:

typedef struct cellNode {
        int cell;
        struct cellNode* next;
}   Node;

typedef struct  {
        int numRows;
        int numColumns;
        Node** rows;
}   Matrix;

and a function to add nodes in my code, but I get the 0xC0000005 error when i try to allocate memory for the node inside the function:和 function 在我的代码中添加节点,但是当我尝试为 function 中的节点分配 memory 时出现 0xC0000005 错误:

Node* createNode()
{
    int n;
    Node* newNode = malloc(sizeof(Node));
    if (newNode==NULL)
        exit(1);
    scanf("%d",&n);
    printf("n is %d\n",n);
    newNode->cell=n;
    newNode->next = NULL;
    return newNode;
}

The function allocates memory for a node and returns it to another function that appends it to an array of nodes. function 为一个节点分配 memory 并将其返回给另一个 function,后者将其附加到节点数组。 so the call for the createNode function looks like this:所以对 createNode function 的调用如下所示:

Matrix* MatrixAdder(int row, int col, char mat)
{
    int i,j;
    int value=0;
    Node* newNode=NULL;
    Matrix* temp = malloc(sizeof(Matrix));
    if (temp==NULL)
        exit (1);
    temp->numRows=row;
    temp->numColumns=col;
    temp->rows = malloc(row * sizeof(Node*));
    if (temp->rows==NULL)
        exit(1);
    for (i=0;i<row;i++)
    {
        printf("Enter row %d data\n",i);
        scanf("%d",&value);
        temp->rows[i]->cell=value;
        for (j=0;j<col-1;j++)
        {

            newNode=createNode();
            temp->rows[i]->next=newNode;
            
        }
    }


}

I get this:我明白了:

Enter row 0 data
2

Process returned -1073741819 (0xC0000005)   execution time : 6.525 s
Press any key to continue.

the function is able to receive the int but immediately fails at the malloc line. function 能够接收 int 但在 malloc 行立即失败。 the main just calles for the functions:主要只是调用功能:

int main()

{
    int i;
    Matrix *A, *B, *C;
    int n,m,k;
    MatSize(&n,&m,&k); /*works fine*/
    A=MatrixAdder(n, m, 'A');
    B=MatrixAdder(m, k, 'B');

    return 1;
}

This line这条线

temp->rows[i]->cell=value;

is wrong.是错的。 temp->rows[i] is not pointing to any valid object. temp->rows[i]没有指向任何有效的 object。

You did allocate memory for rows , ie您确实为rows分配了 memory,即

temp->rows = malloc(row * sizeof(Node*));

but you never assigned any values to row[0], row[1], ... so you are dereferencing uninitialized pointers.但您从未为row[0], row[1], ...分配任何值,因此您正在取消引用未初始化的指针。

Works here:在这里工作:


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

typedef struct cellNode {
        int cell;
        struct cellNode *next;
}   Node;

// and a function to add nodes in my code, but I get the 0xC0000005 error when i try to allocate memory for the node inside the function:

Node *createNode()
{
    int n;
    Node *newNode = malloc(sizeof(Node));
    if (newNode==NULL)
        exit(1);
    scanf("%d",&n);
    printf("n is %d\n",n);
    newNode->cell=n;
    newNode->next = NULL;
    return newNode;
}

// The function allocates memory for a node and returns it to another function that appends it to an array of nodes. so the call for the createNode function looks like this :

int main(void)
{
Node *someNode=NULL;

printf("Enter row 0 data\n");
someNode = createNode();

printf("Some node: %d\n", someNode->cell);

return 0;
}

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

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