Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

c++ Simple Class Question

Name: Anonymous 2007-06-27 0:40 ID:CjAL5fOI

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: Anonymous 2007-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: Anonymous 2007-06-27 10:20 ID:UYX9rel9

"delete old table" is supposed to be "delete this" ?

Name: Anonymous 2007-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: Anonymous 2007-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?

Name: Anonymous 2007-06-28 9:05 ID:bn+R3ATt

>>4
Ah, thanks. that makes a lot of sense.

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: Anonymous 2007-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.

Name: Anonymous 2007-06-28 9:30 ID:bn+R3ATt

>>7
*this = htable_rehash();

I've done it!

i think. Just got to hammer out some problems with collision and hopefully i have made a hash table in c++ =D

Name: Anonymous 2007-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: Anonymous 2007-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: Anonymous 2007-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: Anonymous 2007-06-28 9:57 ID:k6abc8SN

yes, I would like to see it.
btw are you sure there is no leak?

Name: Anonymous 2007-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: Anonymous 2007-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.

Name: Anonymous 2007-06-28 12:11 ID:PGIf/UOw

>>11
No, I prefer my std::tr1::unordered_map.

Name: Anonymous 2007-06-28 12:34 ID:3yq3bgyf

>>15
what isn't in c++'s standard library?

Name: Anonymous 2007-06-28 12:46 ID:5x2u26RN

>>16
The answer to the Ultimate Question of Life, the Universe, and Everything.

Name: Anonymous 2007-06-28 13:06 ID:q13pVQnd

>>16
defmacro

Name: Anonymous 2007-06-28 13:08 ID:3yq3bgyf

>>17,18
they are, however, in boost.

Name: Anonymous 2007-06-28 21:39 ID:bn+R3ATt

>>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?)

Heres the code:
http://rapidshare.com/files/39959324/htable6.rar.html

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...

i hope =D

Name: Anonymous 2007-06-28 22:07 ID://kvDip4

ITT, amateur hour.

Name: Anonymous 2007-06-29 0:27 ID:2Dq2ukvu

>>21
I know c, I'm learning c++

Name: Anonymous 2007-06-29 0:38 ID:7HFcgrtT

>>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.

http://www.sgi.com/tech/stl/table_of_contents.html

Name: Anonymous 2007-06-29 9:44 ID:bwlMfvJV

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!

Name: Anonymous 2007-06-29 9:53 ID:7Kwp6kJ2

>>24
have less memory leaks
Lol C/C++

Name: Anonymous 2007-06-29 10:31 ID:IjrxzVir

>>25
Any language can have memory leaks.  C/C++ just let you make them yourself rather than the compiler/interpreter.

Name: Anonymous 2007-06-29 10:47 ID:7Kwp6kJ2

>>26
C/C++ just let you make them yourself
Lol C/C++

Name: Anonymous 2007-06-29 10:56 ID:FDwrQD3/

>>25, 27

I can't code for shit and can't handle C/C++ because it is too hard for me

fixed

Name: Anonymous 2007-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: Anonymous 2007-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: Anonymous 2007-06-29 14:29 ID:iAqBqrtH

>>30
For most tasks, machine compiler > human compiler

Name: Anonymous 2007-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?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List