Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
floWenoL
Oct 23, 2002

JoeNotCharles posted:

Since we're iterating over actorsToDelete, removing things from actorList doesn't affect the iterator, and then actorList goes out of scope and gets cleaned up.

Too bad it's unnecessarily O(n2). :psyduck:

Adbot
ADBOT LOVES YOU

floWenoL
Oct 23, 2002

TheSleeper posted:

Uh, you mean O(2n), no? The absolute max here is that it iterates the entire list twice I'm not seeing where it can loop n2 times.

Well okay if you have magical linked list where .remove() is O(1).

floWenoL
Oct 23, 2002

prolecat posted:

In that case, you're stuck setting the members of the struct in the body of the class constructor, barring some language feature I'm unaware of.

Since structs have a default copy constructor, you can define a function that returns said struct (something like MyStruct MakeDefaultMyStruct()) and initialize the member variable with that.

floWenoL
Oct 23, 2002

Vanadium posted:

Doing about anything with that union is an aliasing violation. :colbert:

Edit: Well, okay, only in one direction.

No it's not; the whole point of unions is to avoid strict-aliasing (not aliasing) violations. You probably mean it's undefined to write to a union member and to read from another one (mostly).

floWenoL
Oct 23, 2002

FearIt posted:

I've tried casting windowClassName as an (LPWSTR) but then the code compiles but does nothing.

This is the worst way to solve compile errors in C/C++.

Your solution isn't that great either. Why not pass in L"MyString" or _T("MyString")? (I forgot the right one to use.)

floWenoL fucked around with this message at 04:35 on Sep 13, 2008

floWenoL
Oct 23, 2002

ultra-inquisitor posted:

If you're using the STL you may as well use string.

std::string doesn't make the same guarantees about memory locations as vector<char> does, I believe.

floWenoL
Oct 23, 2002

Risita posted:

If you want your code as short as possible, make an array of size 3 and bubble sort it! Or are you required to use 3 separate variables?

quote:

It has to be using if statements, as it is in a beginning programming class.

floWenoL
Oct 23, 2002

Avenging Dentist posted:

Yeah but right-shifting in C is an arithmetic shift. i.e. 0b1??????? >> 7 = 0b11111111 == -1

Implementation-defined. :colbert:

floWenoL
Oct 23, 2002

Pooball posted:

!!~(-1<<a-b)

What the gently caress is this?

floWenoL
Oct 23, 2002

JoeNotCharles posted:

If you make this function a pure virtual instead, it will 100% guarantee that the base version will never be called, because it doesn't even exist:

That's not true.

floWenoL
Oct 23, 2002

Whilst farting I posted:

I need to alphabetize a two dimensional string vector by the first column. An excerpt looks like this

Why aren't you using std::set or at least std::sort?

Edit:
Why does your inner loop do the same thing (readAllItems) as your outer loop?

floWenoL fucked around with this message at 23:43 on Oct 10, 2008

floWenoL
Oct 23, 2002

Whilst farting I posted:

The dataset is indeed truly immense - it's several thousand lines long.

Since when was several thousand lines an "immense" dataset? :confused:

floWenoL
Oct 23, 2002

Cross_ posted:

%i, as the letter implies, is expecting an int. You are feeding it a char, that leaves printf with taking "random" values from nearby memory to fill in the missing 3 bytes.

This is completely wrong.

floWenoL
Oct 23, 2002

Janin posted:

All memory allocated by malloc() is free'd when the application exits. If you're using a memory leak checker, explicitly freeing your allocations will prevent false positives.

How is it a false positive if a memory leak checker finds your memory leaks? :)

It's also worth pointing out that not all operating systems clean up after programs (but most modern ones do). But the practical reason is that you usually want to be able to use a program continuously without it eating up your memory. Short-lived programs like compilers don't have to worry so much and in fact compilers are notorious for being leaky.

floWenoL fucked around with this message at 11:07 on Oct 23, 2008

floWenoL
Oct 23, 2002

chocojosh posted:

Thanks AD!

Has anyone encountered this before? I think I'll go back to just returning the base class pointer because I can't seem to find a way to do what I want to do (search publicly available returns Base**, searchClass public available returns Derived** but just calls search; searchClass exists so that my client can have proper type-checking).

I don't understand why you're doing things the way you are, but I'm already certain it's completely wrong. It's also worth pointing out that even if you could cast ClassRecord** to SymbolRecord** the code would still be wrong (you'd be dereferencing an uninitialized pointer).

floWenoL
Oct 23, 2002

Anunnaki posted:

cin stops after the first whitespace. I'm entering a whole string of morse code, each character separated by a space.

I'm more curious about the "corrupt the I/O buffer" part.

floWenoL
Oct 23, 2002

hexadecimal posted:

well what if you don't want to use boost, or can't? its not an awful #define its very useful, at least for me :)

See what happens when either _it or _l is a computed expression (e.g., FOREACH(i++, my_map)).

It's a pretty awful macro.

floWenoL
Oct 23, 2002

User0015 posted:

If anyone was up for it, I had some questions I'd like to ask via e-mail. That, or start my own thread. I don't think a thread really warrants my questions, since they'd mostly be single question/comment types.

If only there were a thread specifically dedicated to (C++) questions not worth their own thread...

floWenoL
Oct 23, 2002

Painless posted:

Does anyone know what's going on with std::sort? Is it just that GCC 3.4.4 and visual c++ express suck at optimizing, or am I doing something wrong here?

You're comparing apples and oranges; your comparison function (which is wrong, by the way) is not the same as std::greater<int>.

I don't know if that's the only factor, but it's the most glaring one.

floWenoL
Oct 23, 2002

Nahrix posted:

But I'm using FILE** as a parameter already.

the expression "file" is of type FILE** (or decays to it) so adding &file is of type FILE***, and you're trying to pass it to a function which takes FILE**.

floWenoL
Oct 23, 2002

Adhemar posted:

What's wrong with a post-increment on an iterator?

It creates a temporary needlessly, which may or may not be optimized out.

floWenoL
Oct 23, 2002

hexadecimal posted:

i think you can override copy constructor. In your abstract class, there are probably some protected fields, so copy them in AbstractClass copy constructor then in the derived classes call the super constructor and copy the fields that are specific to derived class.

This 'solution' is totally broken.

floWenoL
Oct 23, 2002

Avenging Dentist posted:

Off the top of my head, the easiest solution would be to use CRTP:

I'm pretty sure that won't work, given the earlier discussion about virtual functions from one superclass 'fulfilling' an abstract virtual function from another superclass.

I can't think of a way that avoids having to write clone() for each subclass. :eng99:

Edit:

Wait, what about

code:
class Base {
  virtual Base *clone() const = 0;
};

template <class T>
class BaseCloner<T> : public Base {
  virtual Base *clone() const { return new T(dynamic_cast<const T &>(*this)); }
};

class Sub1 : public BaseCloner<Sub1> {};

class Sub2 : public BaseCloner<Sub2> {};
You can even do:

code:
#define BASE_SUBCLASS(T) class T : public BaseCloner<T>

BASE_SUBCLASS(Sub1) {};

BASE_SUBCLASS(Sub2) {};
:nyoron:

floWenoL fucked around with this message at 03:15 on Nov 25, 2008

floWenoL
Oct 23, 2002

hexadecimal posted:

How can something like that be possible?

hexadecimal posted:

check out this #define I have found somewhere and have been using for a while. It makes iterating through STL containers a lot easier.

code:
#define FOREACH(_it,_l) for(__typeof((_l).begin()) _it=((_l).begin());(_it)!=(_l).end();(_it)++) 

Hmm...

floWenoL
Oct 23, 2002

TSDK posted:

I can think of a few options, but none of them are going to get you what you want with zero changes. The simplest and clearest change I can think of doing is to put the handler member into an anonymous union, e.g.:

Anonymous unions are C++-only I believe.

In C, an empty param list means (or used to mean) the function can take any number of arguments, not that it takes no arguments. There might be a GCC switch to turn that behavior back on.

floWenoL
Oct 23, 2002

Ari posted:

Shouldn't he get rid of the thing2 = NULL and replace delete with delete[]?

No.

floWenoL
Oct 23, 2002

rawstorm posted:

Now I want to free up the memory I just allocated. What do I need to type on the next line that would free up that memory?

Nothing because apparently you can't post code that makes sense or compiles.

floWenoL
Oct 23, 2002

ultra-inquisitor posted:

edit: it's worth pointing out of course that doing quicksort this way is really an inefficient version of mergesort, one of the strengths of quicksort is that it can be done in-place.

I don't understand why you think it's like mergesort. There's no merging that needs to be done as you know one of the lists is sorted and less than the pivot, and the other is sorted and greater than the pivot, and concatenating two lists together is a simple constant-time operation.

Edit:
In fact, if you're careful to work only with node pointers you can quicksort "in-place" in a different sense, and if T is a large object it might actually be faster than quicksorting an array.

floWenoL fucked around with this message at 22:35 on Dec 12, 2008

floWenoL
Oct 23, 2002

Whilst farting I posted:

It always just returns every time a list has one element or 0 elements,

Isn't that what you want? It's what the top of your code does, anyway.

Also, your for loop simply infinite-loops.

floWenoL
Oct 23, 2002

Avenging Dentist posted:

I'm not sure what you are attempting to say here, but it is almost certainly wrong.

Hexadecimal giving a stupidly wrong answer to a question which someone has already answered? No way!

floWenoL
Oct 23, 2002

Painless posted:

I had a weird crash once on Visual C++ calling a function that was something like this:

code:
void foo( int& bar, int baz )
{
  if ( baz ) bar += baz;
}
with baz as 0 and bar as a null reference (dereferred from a null pointer). This taught me that it is pessimism, not realism, that really gives results.

Well that's explained as simply that the "real" dereference in the code Visual C++ generates happens on the "bar += baz" expression, although the behavior is undefined as soon as you dereferenced the null pointer to get the "null reference".

floWenoL
Oct 23, 2002

hexadecimal posted:

also most of the time you want to abstain from C++ vector and just use static arrays. Make them as big as the boundary cases, and you'll be good.

Are you giving bad advice on purpose, or are you just a horrible programmer? I'm inclined to believe the latter because that is some truly horrible code.

floWenoL
Oct 23, 2002

hexadecimal posted:

It's a good advise for UVa problems because you are not trying to use good design here, just what works the fastest to solve particular problem. You allocate arrays with maximum size for some task because it will work for all test cases and you only make it once. You are shooting for speed and easy to program over memory usage.

Speed is a bullshit reason, especially if you are talking about vectors vs. static arrays. vectors are definitely easier to work with than static arrays for all but the most trivial tasks. At worst, you can just call .reserve() or .resize() if you want.

quote:

Using vector is slightly more expensive, and usually you can do all of the same things just with large arrays that you can reuse between problems (don't have to resize vector, or create new one). memset works faster than vectorz.

Speed is not a factor if you use vector right. I don't know what vectorz is, but I assume it's some vector-zeroing function you wrote. In any case, you can call memset on &v[0].

quote:

Also what is so horrible about it. It was quick to write and passes the test cases.

It's horrible enough that you had to clean it up a bit. ;)

floWenoL
Oct 23, 2002

hexadecimal posted:

It wasn't bad code and there are no mistakes as well as bad advise. You and floWenoL just nit picked one of my points in order to troll.

It was bad, or at least poorly-worded, advice. Believe it or not, I'm more interested in talking about C++ programming, which includes pointing out bad code/practices/advice, than in trolling anyone. Perhaps rather than crying 'troll', you should reflect on why it seems like every one of your programming-related posts has someone criticizing it.

floWenoL
Oct 23, 2002

shodanjr_gr posted:

code:
Foo& myFunction()
{
return Foo::A_STATIC_MEMBER_OF_FOO; //this gives a compile error
}

Hey guys, time for another round of "Guess the problem without the error message"!

(My guess: you don't actually define Foo::A_STATIC_MEMBER_OF_FOO anywhere (the code you have just declares it).)

floWenoL
Oct 23, 2002

Avenging Dentist posted:

Is your mind blown???

:O

floWenoL
Oct 23, 2002

hexadecimal posted:

And all thats needed to print out solution is: cout<<pos[0].first<<" "<<pos[0].second<<endl;

Why sort if all you need is the max element?

floWenoL
Oct 23, 2002

hexadecimal posted:

Well considering that I got AC on this problem, yes.

For each configuration of bins, the minimum number of movements to achieve that configuration is always the same. It is the method I explained earlier, and it is very simple. You just move whatever is not supposed to be in the bin, to the other bins, and you just sum up the number of foreign kinds of glass in each bin in respect to final configuration.

Also I am not quite sure what those pictures are supposed to represent (I am pretty sure that all those movements achieve a different bin configuration in end), but there is definitely only 6 possible permutation of bins (3! = 6) so if you use above method to compute number of movements per each permutation, its easy to find the minimum, and a C++ way to code it I explained already.

code:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

bool cmp( const pair<string,unsigned long long> & a, const pair<string,unsigned long long> & b )
{
     if( a.second != b.second ) return a.second < b.second;
     else return a.first < b.first;
}

struct comporator
{
       bool operator()( const pair<string,unsigned long long> & a, const pair<string,unsigned long long> & b  )
       {
             if( a.second != b.second ) return a.second < b.second;
             else return a.first < b.first;
       }
};

int main()
{
    unsigned long long b1b, b1g, b1c, b2b, b2g, b2c, b3b, b3g, b3c;
    while( cin>>b1b>>b1g>>b1c>>b2b>>b2g>>b2c>>b3b>>b3g>>b3c )
    {         
           vector<pair<string,unsigned long long> > pos;
           pos.push_back( pair<string,unsigned long long>("BGC", b1g + b1c + b2b + b2c + b3b + b3g ));
           pos.push_back( pair<string,unsigned long long>("GBC", b1b + b1c + b2c + b2g + b3b + b3g ));
           pos.push_back( pair<string,unsigned long long>("GCB", b1b + b1c + b2b + b2g + b3c + b3g ));
           pos.push_back( pair<string,unsigned long long>("BCG", b1g + b1c + b2b + b2g + b3b + b3c ));
           pos.push_back( pair<string,unsigned long long>("CBG", b1b + b1g + b2c + b2g + b3b + b3c ));
           pos.push_back( pair<string,unsigned long long>("CGB", b1b + b1g + b2b + b2c + b3c + b3g ));
           //sort( pos.begin(), pos.end(), cmp );
           vector<pair<string,unsigned long long> >::iterator i = min_element( pos.begin(), pos.end(), comporator() );
           cout<<i->first<<" "<<i->second<<endl;
           //cout<<pos[0].first<<" "<<pos[0].second<<endl;
    }
    
    return 0;
}

Arrays? Loops? make_pair? heaps/maps? What are those?? :confused:

Edit:
unsigned long long? wtf?

floWenoL fucked around with this message at 05:57 on Jan 4, 2009

floWenoL
Oct 23, 2002

shodanjr_gr posted:

Say I have a function:

code:
myFunction(Myclass* pointerOut)
{
 pointerOut = new Myclass();
}
and then

code:
int main()
{
Myclass* myPointer = NULL;
myFunction(myPointer);
myPointer.doSomething(); // <- the pointer is still null here
}
What am I doing wrong? I want to be able to pass the pointer as an argument to a function, create an object on that pointer, then keep it around after the function call.

Why don't you just make myFunction() return the pointer? :confused:

Adbot
ADBOT LOVES YOU

floWenoL
Oct 23, 2002

hexadecimal posted:

It doesn't change the outcome of the problem, or impact total runtime significantly does it? Also we wouldn't be having this discussion if floWenoL wasn't nitpicking my posts again. :(

God forbid I try to nudge you into becoming a better programmer!

Edit:

hexadecimal posted:

The reason it works without including, I guess, is because compiler knows to look there by default if it can't resolve some things, since it is standard library for the language.

This is completely false (and going in hexadecimal.txt).

floWenoL fucked around with this message at 08:39 on Jan 4, 2009

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply