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

Pages: 1-4041-

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?

Name: Anonymous 2007-06-29 20:13 ID:vwrJF1pd

>>1-32
Lol C/C++

Name: Anonymous 2007-06-29 21:00 ID:o+aPCd2E

>>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: Anonymous 2007-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()

Name: Anonymous 2007-06-29 23:12 ID:rzXEyjEa

>>35
lol Bjarne's book

Name: Anonymous 2007-06-30 0:31 ID:D/X58+RQ

>>35
vectors are a lot like arrays, except you can resize them, or even just let them grow.

#include <iostream>
#include <vector>

// ...

std::vector<int> vec;
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);

// 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: Anonymous 2007-06-30 6:42 ID:T5dKd027

>>35
If you can do C, which is the real thing, then why are you bothering with C++?

Name: Anonymous 2007-06-30 14:16 ID:4ZOB4mgS

>>38 Because C++ is better obviously.

Here is how your insert function could look like if you used more C++ features:
http://rs56.rapidshare.com/files/40268230/Map.cpp

That's if the data structures in your class were:

std::vector< int > frequencies;
std::vector< std::string > keys;
std::vector< std::string > values;

Name: Anonymous 2007-06-30 14:18 ID:4ZOB4mgS

Beautiful code >>39 !

>>39 , >>40 same person.

Name: Anonymous 2007-06-30 15:15 ID:4ZOB4mgS

IGNORE THE 2 S.

Name: Anonymous 2007-06-30 15:18 ID:gDMKl3mZ

>>39

SHIT CODE YOU FAGGOT

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

Name: Anonymous 2007-06-30 19:05 ID:gDMKl3mZ

>>43
I AGREE THE CODE IS SHIT

Name: Anonymous 2007-07-01 7:41 ID:7UC38q+h

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

Name: Anonymous 2011-02-03 6:12

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