I have a class called htable, when it reaches a certain size it will rehash itself. Heres the pseudo code:
/* Creates new table, and deletes old one. */
Htable Htable::htable_rehash(){
make new Htable;
add old values to new table;
delete old table;
return new Htable;
}
to call it I would say hashtable = hashtable .htable_rehash();
My question is this: do i need to free the memory in the old hash table in the class, or will it be deleted when i say table = table.rehash?
Name:
Anonymous2007-06-27 1:17 ID:VHb7QBGj
Since you call the 'delete', it should free itself. If you stay with object-oriented, you should only manually call free when making delete methods for new classes.
Name:
Anonymous2007-06-27 10:20 ID:UYX9rel9
"delete old table" is supposed to be "delete this" ?
Name:
Anonymous2007-06-27 11:01 ID:7TfJh0cM
It would probably be easier to just call the copy constructor and delete the original. That seems to be what you are doing, anyways.
Htable new_table = new Htable(old_table);
delete old_table;
Name:
Anonymous2007-06-27 11:13 ID:S1YCa8iZ
when it reaches a certain size it will rehash *itself*.
I don't get it. how it's going to rehash itself by creating a new instance?
However I'm still stuck:
this = this.htable_rehash();
[htable_rehash() returns a Htable class].
header: "Htable Htable::htable_rehash()"
Visual c++ does not recognize "this" sentence. Is there a way of referencing itself while in the class? I have tried having a pointer in the header like I did in C, but c++ does not like this.
error: "error C2228: left of '.htable_rehash' must have class/struct/union type"
Name:
Anonymous2007-06-28 9:11 ID:k6abc8SN
I'm pretty sure you can't asign to "this". It's not an "lvalue". In fact, I don't think you can actually do what you're trying to do, at least, the way you're trying to do it.
i think. Just got to hammer out some problems with collision and hopefully i have made a hash table in c++ =D
Name:
Anonymous2007-06-28 9:34 ID:SM4TQb0c
Don't delete a class from inside itself.
Solution: Don't delete the existing table - just modify it.
Name:
Anonymous2007-06-28 9:42 ID:SM4TQb0c
Do you realise the class you just newed has to be deleted somewhere >>8 ?
(and I still hope you aren't deleting this).
Name:
Anonymous2007-06-28 9:45 ID:bn+R3ATt
Its working perfectly now. It passes all my simple tests so far, anyone interested to read through and criticize my code?
You would also score some free hash table code =D
Name:
Anonymous2007-06-28 9:57 ID:k6abc8SN
yes, I would like to see it.
btw are you sure there is no leak?
Name:
Anonymous2007-06-28 11:44 ID:5x2u26RN
>>7 >>8
You can assign to "*this", not "this". It is a pointer like any other, just one we are not *supposed* to play with.
Name:
Anonymous2007-06-28 11:47 ID:5x2u26RN
>>11
By the way, why are you trying to rehash the table? If it is because of degradation from insertions/removals, you should probably use a more efficient tree structure.
>>15
Crap, if i had knew that I would have learnt to use that class. How cross compatible is the standard library btw? (I'm walking into a stupid question here aren't I?)
If theres any problems with it, its probably in the "Htable Htable::htable_rehash()" function. It compiles fine so hopefully it can be used with g++ too.
>>10
I see what you mean, i call "delete this" but later on I say *this = table_rehash(). i think the pointer can be deleted, and everything inside it, and then point to the new pointer that has been rehashed...
>>20
"How cross compatible is the standard library btw?"
It is the **standard** library. Most major compilers (microsoft c++,g++,intel c++, etc.) handle a majority of the library. What they lack you can usually mimick through other classes and templates.
You use like 0 objects - it's like you just dumped some C code into a class. If you wrote it using 'C++' it would be less than half the lines. You would use <string> instead of <string.h> and <iostream> instead of <stdio>. Your code would be more readabl, easier to maintain and have less memory leaks.
int *frequencies;
char **keys;
char **values;
UGLY AND UNSAFE.
Use standard containers - such as vector and list.
Other observations: Nice comments, I don't like your indenting but hey that's style - not important, no way do I trust that code to clean up its memory, no need to prefix functions of a class with the class name (e.g. Htable::htable_print() ).
welcome to the programming world
\o/
l
/ \
>>19 defmacro is in boost? I never knew that, amazing!
I can't code for shit and can't handle C/C++ because it is too hard for me
fixed
Name:
Anonymous2007-06-29 11:57 ID:7Kwp6kJ2
>>28
I've been managing my memory since I was fucking 15. Then I matured and understood it's more efficient if I do the hard tasks, such as algorithm design, and the computer does the easy, repetitive tasks, such as memory management, which is what computers exist for. That way I can do more in the same time, and if it runs slow I buy faster hardware, because hardware is less expensive than my time. Then I grew lazy (a very good programmer feature) and now I don't want to do C and C++.
Name:
Anonymous2007-06-29 12:39 ID:IjrxzVir
>>29
The problem is you are at the mercy of the compiler/interpreter, which may or may not be a good thing.
Name:
Anonymous2007-06-29 14:29 ID:iAqBqrtH
>>30
For most tasks, machine compiler > human compiler
Name:
Anonymous2007-06-29 16:46 ID:FbIpNpot
can we just come to a consensus that there are some circumstances where it would be prudent to handle memory management yourself and for that reason it's a good idea to understand the concept and it's implementation in your language, but generally it's okay to never touch the thing?