Linked List in C buffer overrun -
i make linked list in c ordered insert function. array lists array of individual linked lists , have generate 10000 random numbers , can generate 300 or 400 numbers , fails , gives me buffer overrun exception. reason me getting this?
i thought might because need free memory seems me need memory allocating, nothing left over.
when error occurs call stack shows line:
struct node *newnode = (struct node *)malloc(sizeof(*newnode));
is causing exception.
it works less numbers being generated, if 100 numbers output looks this: http://gyazo.com/18a9ba87611f5676d6fa7b6229fc41e0 that's not full output of course that's idea.
// program 6.cpp : defines entry point console application. // #include "stdafx.h" #include <time.h> #include <stdlib.h> #define max 200 void orderedinsert(struct node **, int); void printlist(struct node **, int); struct list{ int size; struct node *front; }; struct node{ int value; struct node *next; }; void main(){ struct list lists[max]; int i, random; for(i = 0; < max; i++){ lists[i].front = 0; lists[i].size = 0; } srand(time(null)); for(i = 0; < 100; i++){ random = rand() % 10000000; orderedinsert( &(lists[random%max].front), random); (lists[i].size)++; } for(i = 0; < max; i++){ printf("%d ", i); printlist( &(lists[i].front), lists[i].size); } scanf_s("%d", null); } void orderedinsert(struct node **front, int value){ struct node *newnode = (struct node *)malloc(sizeof(*newnode)); struct node *temp, *prev; newnode->value = value; if(*front == null){ *front = newnode; newnode->next = 0; return; } if((*front)->value > newnode->value){ newnode->next = *front; *front = newnode; return; } temp = (*front)->next; prev = *front; while(temp != null && temp->value < newnode->value){ prev = temp; temp = temp->next; } newnode->next = temp; prev->next = newnode; } void printlist(struct node **front, int value){ struct node *temp; temp = *front; if(temp){ printf("the list contains elements: %d", temp->value); temp = temp->next; while(temp != null){ printf(", %d", temp->value); temp = temp->next; } } printf("\n"); }
here full call stack if need it-
msvcr110d.dll!_crt_debugger_hook(int _reserved) line 57 c program 6.exe!__raise_securityfailure(_exception_pointers * exceptionpointers) line 67 c program 6.exe!__report_gsfailure() line 235 c msvcr110d.dll!validatelocalcookies(void (unsigned int) * cookiecheckfunction, _eh4_scopetable * scopetable, char * framepointer) line 198 c msvcr110d.dll!_except_handler4_common(unsigned int * cookiepointer, void (unsigned int) * cookiecheckfunction, _exception_record * exceptionrecord, _exception_registration_record * establisherframe, _context * contextrecord, void * dispatchercontext) line 329 c program 6.exe!_except_handler4(_exception_record * exceptionrecord, _exception_registration_record * establisherframe, _context * contextrecord, void * dispatchercontext) line 94 c ntdll.dll!77e2b499() unknown [frames below may incorrect and/or missing, no symbols loaded ntdll.dll] ntdll.dll!77e2b46b() unknown ntdll.dll!77e2b40e() unknown ntdll.dll!77de0133() unknown msvcr110d.dll!malloc(unsigned int nsize) line 56 c++ > program 6.exe!orderedinsert(node * * front, int value) line 59 c program 6.exe!main(...) line 42 c program 6.exe!__tmaincrtstartup() line 536 c cd001c1d() unknown
i got error: unhandled exception @ 0x100b26b6 (msvcr110d.dll) in program 6.exe: 0xc0000005: access violation reading location 0x0146f78f.
call stack one:
> msvcr110d.dll!_nh_malloc_dbg_impl(unsigned int nsize, int nhflag, int nblockuse, const char * szfilename, int nline, int * errno_tmp) line 239 c++ program 6.exe!orderedinsert(node * * front, int value) line 59 c program 6.exe!main(...) line 42 c program 6.exe!__tmaincrtstartup() line 536 c a500201d() unknown
this isn't full call stack. full call stack miles long.
you have possible corruption in printing function. note, you're incrementing i-th list size , not 1 insert. surely lead corruption later when printing. still it's bit weird failure occurs before getting print itself.
Comments
Post a Comment