Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy can I write and read memory when I haven't allocated space?
    primarykey
    data
    text
    <p>I'm trying to build my own Hash Table in C from scratch as an exercise and I'm doing one little step at a time. But I'm having a little issue...</p> <p>I'm declaring the Hash Table structure as pointer so I can initialize it with the size I want and increase it's size whenever the load factor is high.</p> <p>The problem is that I'm creating a table with only 2 elements (it's just for testing purposes), I'm allocating memory for just those 2 elements but I'm still able to write to memory locations that I shouldn't. And I also can read memory locations that I haven't written to.</p> <p>Here's my current code:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #define HASHSIZE 2 typedef char *HashKey; typedef int HashValue; typedef struct sHashTable { HashKey key; HashValue value; } HashEntry; typedef HashEntry *HashTable; void hashInsert(HashTable table, HashKey key, HashValue value) { } void hashInitialize(HashTable *table, int tabSize) { *table = malloc(sizeof(HashEntry) * tabSize); if(!*table) { perror("malloc"); exit(1); } (*table)[0].key = "ABC"; (*table)[0].value = 45; (*table)[1].key = "XYZ"; (*table)[1].value = 82; (*table)[2].key = "JKL"; (*table)[2].value = 13; } int main(void) { HashTable t1 = NULL; hashInitialize(&amp;t1, HASHSIZE); printf("PAIR(%d): %s, %d\n", 0, t1[0].key, t1[0].value); printf("PAIR(%d): %s, %d\n", 1, t1[1].key, t1[1].value); printf("PAIR(%d): %s, %d\n", 3, t1[2].key, t1[2].value); printf("PAIR(%d): %s, %d\n", 3, t1[3].key, t1[3].value); return 0; } </code></pre> <p>You can easily see that I haven't allocated space for <code>(*table)[2].key = "JKL";</code> nor <code>(*table)[2].value = 13;</code>. I also shouldn't be able read the memory locations in the last 2 <code>printfs</code> in <code>main()</code>.</p> <p>Can someone please explain this to me and if I can/should do anything about it?</p> <p><strong>EDIT:</strong><br> Ok, I've realized a few things about my code above, which is a mess... But I have a class right now and can't update my question. I'll update this when I have the time. Sorry about that.</p> <p><strong>EDIT 2:</strong><br> I'm sorry, but I shouldn't have posted this question because I don't want my code like I posted above. I want to do things slightly different which makes this question a bit irrelevant. So, I'm just going to assume this was question that I needed an answer for and accept one of the correct answers below. I'll then post my proper questions...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload