Closed SimonCarlson closed 2 years ago
I added a prints to reallocate:
...
printf("pointer %p newSize %ld\n", pointer, newSize);
void *result = realloc(pointer, newSize);
prtinf("realloc successfully called\n");
...
and get the following output.
Successfully allocating string "aa"
:
> "aa"
pointer (nil) newSize 3
realloc successfully called
pointer (nil) newSize 40
realloc successfully called
pointer (nil) newSize 192
realloc successfully called
pointer (nil) newSize 128
realloc successfully called
pointer (nil) newSize 8
realloc successfully called
pointer (nil) newSize 32
realloc successfully called
== code ==
0000 1 OP_CONSTANT 0 'aa'
0002 2 OP_RETURN
0000 1 OP_CONSTANT 0 'aa'
[ aa ]
0002 2 OP_RETURN
aa
Crashing on string "a":
> "a"
pointer (nil) newSize 2
realloc successfully called
pointer (nil) newSize 40
realloc successfully called
pointer (nil) newSize 192
realloc successfully called
pointer (nil) newSize 128
a.out: malloc.c:2379: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.
Aborted
The function is called with the same arguments both times, except the very first invocation where newSize is 3 and 2 respectively, yet only fails in one case
There was a bug in table.c
: I was calculating the index as uint32_t index = key->hash & capacity;
instead of uint32_t index = key->hash % capacity;
.
Hi. I'm at chapter 20.5 in the book and my interpreter crashes when provided a string of length < 2 or > 3. For instance:
works but
does not. These problems did not occur in the previous part, 20.4. I notice this starts to happen when I include the line
in the
allocateString
function in object.c. I tried using valgrind and get these errors:where I feel like the chain
tableSet -> adjustCapacity -> reallocate -> realloc
is the culprit, but I've not been able to confirm this using gdb as stepping through the code is finicky (for instance stepping over the linevoid* result = realloc(pointer, newSize);
inreallocate
in memory.c sometimes causes a crash, but usingnext
instead never causes a crash).I've compared my source code to the one in the repository and cannot find any errors., find my current versions of table.c and memory.c below. I'm out of ideas on what to do here, any help is appreciated.
table.c:
memory.c: