函数内部结构失败的 Malloc
Malloc of a structure failure inside a function
我有一个结构:
typedef struct cellNode {
int cell;
struct cellNode* next;
} Node;
typedef struct {
int numRows;
int numColumns;
Node** rows;
} Matrix;
和一个在我的代码中添加节点的函数,但是当我尝试为函数内的节点分配内存时出现 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;
}
该函数为一个节点分配内存,然后 returns 将其分配给另一个函数,该函数将其附加到节点数组。所以对 createNode 函数的调用如下所示:
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;
}
}
}
我明白了:
Enter row 0 data
2
Process returned -1073741819 (0xC0000005) execution time : 6.525 s
Press any key to continue.
该函数能够接收 int 但在 malloc 行立即失败。
主要只是调用函数:
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;
}
在这里工作:
#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;
}
这一行
temp->rows[i]->cell=value;
错了。 temp->rows[i]
未指向任何有效对象。
您确实为 rows
分配了内存,即
temp->rows = malloc(row * sizeof(Node*));
但您从未为 row[0], row[1], ...
分配任何值,因此您正在取消引用未初始化的指针。
我有一个结构:
typedef struct cellNode {
int cell;
struct cellNode* next;
} Node;
typedef struct {
int numRows;
int numColumns;
Node** rows;
} Matrix;
和一个在我的代码中添加节点的函数,但是当我尝试为函数内的节点分配内存时出现 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;
}
该函数为一个节点分配内存,然后 returns 将其分配给另一个函数,该函数将其附加到节点数组。所以对 createNode 函数的调用如下所示:
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;
}
}
}
我明白了:
Enter row 0 data
2
Process returned -1073741819 (0xC0000005) execution time : 6.525 s
Press any key to continue.
该函数能够接收 int 但在 malloc 行立即失败。 主要只是调用函数:
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;
}
在这里工作:
#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;
}
这一行
temp->rows[i]->cell=value;
错了。 temp->rows[i]
未指向任何有效对象。
您确实为 rows
分配了内存,即
temp->rows = malloc(row * sizeof(Node*));
但您从未为 row[0], row[1], ...
分配任何值,因此您正在取消引用未初始化的指针。