Я пишу код для компилятора для языка игрушек. Это фрагмент кода. В main
имеются две функции: createlist
и node
. Когда кто-то прокомментировал, другой работает нормально, но вместе он показывает ошибку, которую я не понимаю. Любая помощь приветствуется.Что касается указателей и ошибок malloc
#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);
}
Если закомментировать newnode
то createlist
работает отлично, но вместе это показывает следующее сообщение об ошибке:
a.out: malloc.c:2372: sysmalloc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd))))
&& old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 *(sizeof(size_t))) - 1))
& ~((2 *(sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long) old_end & pagemask) == 0)' failed.
created sucessfullyAborted
Какие ошибки отображаются? –
Неопределенное поведение: 'for (i = 0; i <= 10; i ++)' как выходящее за пределы массива 'list'. – hmjd