关于指针和malloc错误
我正在为一种玩具语言的编译器编写代码。这是一段代码。在关于指针和malloc错误,c,C,我正在为一种玩具语言的编译器编写代码。这是一段代码。在main中有两个函数:createlist和node。当其中一个被注释掉时,另一个可以正常工作,但是它一起显示了一个我不理解的错误。感谢您的帮助 #include <stdio.h> #include <stdlib.h> struct node { //declaration for node int tokenno; struct node * next ; }; struct
main
中有两个函数:createlist
和node
。当其中一个被注释掉时,另一个可以正常工作,但是它一起显示了一个我不理解的错误。感谢您的帮助
#include <stdio.h>
#include <stdlib.h>
struct node { //declaration for node
int tokenno;
struct node * next ;
};
struct node * newnode(int a) // to create a new node
{
struct node * new1 = (struct node *)malloc(sizeof(struct node));
printf("\n malloc sucees");
(*new1).tokenno = a ;
(*new1).next = NULL ;
printf("\new node sucess\n");
return (new1);
};
struct firstlist{
int size ;
struct node * list[10];
};
typedef struct firstlist * plist;
plist createlist(){ //fun to create a first list
int i ;
plist p = (plist) malloc(sizeof(struct firstlist));
(*p).size = 10 ;
for(i = 0 ; i <=10;i++){ //initializing list[i] to NULL
(*p).list[i] = NULL ;
}
printf("\n created sucessfully");
return p;
}
int main(){
plist p ;
//p = createlist(); // If you comment createlist the new node works fine
//getfirstset(p);
//insert(1,5,p);
newnode(2);
}
它崩溃的原因是您在
createlist
函数中写入的内容超出了数组的边界:
for(i = 0 ; i <= 10; i++)
for(i=0;i它崩溃的原因是您在createlist
函数中的数组边界之外写入:
for(i = 0 ; i <= 10; i++)
for(i=0;i它崩溃的原因是您在createlist
函数中的数组边界之外写入:
for(i = 0 ; i <= 10; i++)
for(i=0;i它崩溃的原因是您在createlist
函数中的数组边界之外写入:
for(i = 0 ; i <= 10; i++)
for(i=0;i显示了哪些错误?未定义的行为:for(i=0;i显示了哪些错误?未定义的行为:for(i=0;i显示了哪些错误?未定义的行为:for(i=0;i