marekweb / datastructs-c Goto Github PK
View Code? Open in Web Editor NEWArraylist and Hashtable implementation in C
Arraylist and Hashtable implementation in C
As arraylist_remove is written, it reads memory from one position where it might not be readable.
The code is:
void* value = l->body[index];
arraylist_memshift(l->body + index + 1, -1, l->size - index);
l->size--;
return value;
Let say I have this body: {0, 1, 2, 3, 4}, with size = capacity = 5.
if I do arraylist_remove(l, 2)
, it will try to do:
arraylist_memshift(l->body + 3, -1, 3)
That will try to copy body[3] into body[2], body[4] into body[3], and body[5] into body[4].
But we can't access body[5], because capacity < 6.
This might not throw an error, but it's not correct.
Also, it could just be me misunderstanding the code
If you remove an entry using hashtable_remove, you set its key = NULL. But if you then call hashtable_find_slot, and it linearly probes to the site of the deleted entry, seeing key = NULL will make the routine think this is the end of the linear probing chain, and the routine will falsely return the claim that the key was not found, instead of continuing to search past the removed entry.
A quick fix is to set a removed entry's key to some arbitrary value, say "XYZZY" or "\0", and to ban both NULL and "\0" from being permissible keys in the hashtable.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.