c - How does toggling a call to printf within a function determine whether my code doesn't crash? -
i writing simple function creates new node @ beginning of linked list. when try execute file windows command error window comes saying "a.exe has stopped working."
what mystery me when add printf
call within body of insertnewnodeatfront
, code executes without above error box. ideas?
here source file:
#include <stdio.h> typedef struct nodetag{ const char* airport; struct nodetag * link; } nodetype; void insertnewnodeatfront(nodetype *, const char*); void insertnewnodeatfront(nodetype * l, const char* str){ nodetype * n; n = l; nodetype * newfirst; newfirst->airport = str; newfirst->link = n; l = newfirst; //printf("l->airport: %s\n",l->airport); <---this line magically makes work. } int main(){ nodetype * myitinerary; insertnewnodeatfront(myitinerary,"ont"); return 0; }
as hmjd has said, crash caused using uninitialised memory. reason doesn't crash @ point when add more code causes compiler move things around in memory, uninitialised variable points somewhere different.
it still bad, however: code has still blatted random location in memory cause crash time.
Comments
Post a Comment