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?
>>30
Who the fuuuuuck cares, man. I need to get stuff done. I don't have time to waste micro-managing my processes, I want them working as soon as I think of them, for which I need productive languages.
>>32
Yes, C is necessary for OSes, drivers, commonly used system utilities, core libraries, toolkits, and interpreters. I'm sure you can come up with more low-level, speed-critical examples. For everything else, time can't be wasted. In any case, these require C. C++ is just an overcomplicated C that screams of "I want to be a high-level language, why can't I be a high-level language?".
Name:
Anonymous2007-06-29 22:55 ID:2Dq2ukvu
>>24
yeah, a lot of the code is actually copied from a C hash table I wrote.
You say that my program is vulnerable to memory leaks, but where? I used free() to clean up the malloced memory, and delete do get rid of the classes =D
I try to avoid the vector class, I don't really understand it. I'm not defending my noob C++ methods, I'm just new to it. any good tutorials explaining it?
I feel safer with C techniques as I'm migrating to C++. but hey, it works!
but 99% of people who program C++ hate that cin<<"text here" crap, it just looks ugly. like fprintf()
// Array-style access
for (int i = 0; i < vec.size(); ++i) {
std::cout << vec[i] << std::endl;
}
// Bounds-checked access
for (int i = 0; i < vec.size(); ++i) {
std::cout << vec.at(i) << std::endl;
}
// Iterator access (recommended)
for (std::vector<int>::const_iterator ci = vec.begin();
ci != vec.end(); ++ci) {
std::cout << *ci << std::endl;
}
This doesn't cover everything. I'm using the vector mostly as read-only values in the loops, but all three ways can also be used to assign stuff to the relevant position in the vector. Except maybe the last: you'd need to change that to a regular std::vector<int>::iterator.
Also, your staunch manual memory management stance resulted in this when I tested it: *** glibc detected *** ./a.out: free(): invalid pointer: 0xbfd1354c ***
Name:
Anonymous2007-06-30 6:42 ID:T5dKd027
>>35
If you can do C, which is the real thing, then why are you bothering with C++?
(Map_step(position) % capacity, true) <- WHAT THE FUCK IS THIS YOU IDIOT
int Map::insert(std::string key, std::string value)
{
// Rehash if we are too full
if(percent_full() >= REHASH_PERCENT)
rehash();
unsigned int position = hash(key)%capacity;
if(insert_key(key, position))
return frequencies2[position];
// There is another key at this position
for(unsigned int scanpos=Map_step(position) % capacity; scanpos!=(position); (Map_step(position) % capacity, true))
if(insert_key(key, position))
return frequencies2[position];
return 0;
}
// Returns false if there is another key at that position.
bool Map::insert_key(std::string key, unsigned int position)
{
if(frequencies[position]==0)
{
itemcount++;
frequencies2[position]=1;
keys2[position]=key;
return true;
}
if(keys2[position]==key)
{
frequencies2[position]++;
return true;
}
return false;
}
Name:
Anonymous2007-06-30 18:22 ID:p8vbW2se
(Map_step(position) % capacity, true)
I'm guessing it's magic.
No, probably the guy wrote it as a while, then changed it to a for, and something mixed up.
Anyway that code is shit.
Name:
Anonymous2007-06-30 18:36 ID:hvxu1n6E
ITT, C/C++ and STL amateurs.
Fun fact: most implementations of std::map are actually self-balanching binary search trees, instead of true hash tables.
>>42 That syntax is valid, what is your problem - never seen it before newbie?
>>43 Yes, in the ops it was a while loop - the purpose of the code was to demonstrate what real c++ could look like, I wasn't concerned about if it actually worked, if it doesn't any changes would be pretty minor.
Overall it's about 20 less lines than the ops, more readable and less error-prone. CODE IS NOT SHIT LOSERS.
If you think it is, rewrite the OPs insert function (in C++) and show me better.
Name:
Anonymous2007-07-01 9:36 ID:VR5aYA7A
>>44
interesting, that would result in a slow down if it got big.
If i remember correctly if you wanted to compile the program in g++ you needed some compiler options:
gcc -W -Wall -ansi -pedantic *.c adt\*.c -o adt
Don't have any idea what they are. They worked on my C program, haven't tested it with my c++ code
>>46
Thanks for your code, but you could have stated it was pseudo =P
I will try to recode it using C++ vectors, but not the namespace std. It's just not my coding style