Skip to main content
8 events
when toggle format what by license comment
Jul 9, 2018 at 2:27 comment added faelesterio thanks cliff, much appreciated ~
Jul 9, 2018 at 2:26 vote accept faelesterio
Jul 8, 2018 at 20:19 comment added Cliff B hashtable is an array of pointers. Set them to NULL. In C, only some types of vars are automatically initialized. Pointers are never initialized automatically. Because of this, best practices are to manually initialize all vars every time to assure that it has been done. Also remember that it IS an array of pointers, not an array of structs. The address of the first struct will need to be assigned to this pointer. It's also why the test doesn't work before this first assignment.
Jul 8, 2018 at 18:26 comment added faelesterio by convention, what should i initialize my array of nodes (hashtable) to? Also, I thought by coding in: node *hashtable[26], that is already initializing my array; I just havent assigned specific values to it yet.
Jul 8, 2018 at 4:50 comment added Cliff B .... The problem is actually worse. If you're lucky, the garbage data will contain the equivalent of an address that isn't allocated to the program. But if you're really, really unlucky, the garbage will contain the equivalent of a random address that actually has been allocated and if the code writes to there, it'll corrupt some memory somewhere in your program without giving you the slightest clue that something is wrong. (unless you're lucky and the OS notices something is amiss and errors it out anyways.) ALWAYS INITIALIZE ARRAYS!
Jul 8, 2018 at 4:48 comment added Cliff B Let's extend the metaphor. The hashtable[] is the table that the glass sits on. For hashtable[x] that hasn't been initialized is the broken glass shrads that are sitting on the table. There's no glass! ;-) Same effect. Here's some more detail. If hashtable[x] has NOT been initialized, it indeed contains garbage data.
Jul 8, 2018 at 1:45 comment added faelesterio sorry, I didn't copy this in with the code I posted above, but I did initialize a hashtable and node structure: typedef struct node { char word[LENGTH +1]; struct node *next; } node; //hash table node *hashtable[26]; So, the glass exists, its just filled with garbage values at the beginning of the program :) so it can't be the hashtable....
Jul 7, 2018 at 20:51 history answered Cliff B CC BY-SA 4.0