Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 09 Dec 1999
Offline Last Active Private

#5235981 Do you comment Above or Below your code?

Posted by on 21 June 2015 - 12:45 AM

I think the only time I've ever seen comments below code is in Python where there's a comment for what indented block was just exited or in assembly where you also see comments for end of loops, functions, data blocks and so on. Ex:
for loop_variable in range(x):
    # way too many lines of code
# end loop_variable for
Usually curly brace languages put similar comments on the same line as the curly brace, so it's not really below the code.

Actually, I think there was one time I saw an ASCII art comment with arrows pointing to individual characters in a string literal that had the odd characters commented above and the even characters commented below (or maybe vice versa). Something like:
//                          +--- cow is an integer
//                          | +- moose is a float
//                          v v
const char * type_string = "ZZff";
//                           ^ ^
//                           | +- cat is a float
//                           +--- goat is an integer
Hopefully the actual code was less silly than this.

#5235730 Interpreting unordered_map key as blob of bytes for hashing?

Posted by on 19 June 2015 - 12:00 PM

If you were summing the hashes, then that would explain getting a lot of collisions. Simple sums have a tendency to introduce non uniform distributions in the bit patterns. You could try using boost's hash_combine algorithm which basically boils down to:
template <class T>
inline void hash_combine(std::size_t& seed, const T& v)
    std::hash<T> hasher;
    seed ^= hasher(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);

#5235044 deprecated conversion from string constant to ‘char

Posted by on 16 June 2015 - 02:02 AM

To just get rid of the warning, you could change the type of outre to const char *. Note that your function as written is returning a pointer to a stack allocated buffer, which can cause bad things to happen. Since you're using C++, you may want to consider returning a std::string by value instead. 

#5234805 Simple template doesnt compile

Posted by on 14 June 2015 - 06:37 PM

You can't cast a pointer to a member function to a regular function pointer. A pointer to a member function often isn't even the same size as a regular function pointer. A regular function pointer is often just the address of a piece of code, but a member function could be that kind of address, or an index into the vtable or an index into the vtable plus weird bookkeeping information to find the right subobject from a leaf node of a virtual inheritance hierarchy. 


Note that what you're trying to do can already be done in C++ with std::bind().

#5234781 hash

Posted by on 14 June 2015 - 02:17 PM

Hash values try to ensure that small changes in the source data produce large changes in the hash

That's true for cryptographic hashes, but not all hashing applications require it. Hash tables, for example, are largely perfectly happy if a single bit of difference in the input produces only a single bit of difference in the hash.

#5234756 hash

Posted by on 14 June 2015 - 11:09 AM

In general, string hashes are irreversible since multiple strings can hash to the same value. Note that this particular hash function is worse than usual in that respect. "abc" and "cba" look like they would hash to the same string as would switching the first and third letters of any string. 

#5234617 Acceptance of a BCompSc degree

Posted by on 13 June 2015 - 11:09 AM

As far as I can tell, even ignoring the online only nature of the degree, University of the People has two minor strikes against it. One, while it is accredited, it's only nationally accredited by the DEAC. Due to a quirk of how the US accreditation system was developed, national accreditation is less useful than regional accreditation. If your institution is only nationally accredited then you're less likely to be able to do things like transfer credits to another university, qualify for a graduate degree at another university and so on. In some places, a degree from a US nationally accredited school will not count for employment purposes. The other problem is that it's only been accredited since 2014, which is a warning sign for some people considering a degree from a place they've never heard about before. 

#5233377 Serialize Object to specific directory

Posted by on 07 June 2015 - 11:00 AM

What have you tried? You should just be able to specify the full path to the file you want to write when you create a FileOutputStream. 

#5232242 C++ regex replace only once

Posted by on 01 June 2015 - 03:52 PM

Did you try using format_first_only for the flags argument?

#5232108 signal() : any performance impact ?

Posted by on 01 June 2015 - 01:19 AM

Basically what happens when a signal is raised is the executing environment suspends execution of the process, looks up if the process has registered a signal handler and then executes either the registered handler or a default handler. Your program doesn't do any polling for raised signals. 

#5230262 When you realize how dumb a bug is...

Posted by on 21 May 2015 - 09:44 AM

I'm more curious to know why one would type the lowercase L instead of the 1 in the first place, given that at least on a QWERTY keyboard they're practically on opposite sides of the keyboard.

To disguise an exploit deliberately inserted into the code, of course.

I may have spent too much time reading entries at the underhanded C contest.

#5230171 Too clever...?

Posted by on 20 May 2015 - 08:12 PM

std::is_sorted() works pretty much how you imagine. It loops through the sequence and checks if the first element is less than or equal to the second element and the second element is less than or equal to the third element and so on. In principle, if you have a fixed sized array, the compiler can unroll the loop and just do the checks on the individual elements. In practice, many compilers don't that kind of aggressive data flow analysis. Additionally, many standard library implementations don't implement is_sorted() in what you might consider the most straightforward manner, and instead implement in terms of comparing std::is_sorted_until() against last. This requires the addresses of the individual array elements, which compilers have a harder time optimizing out than the raw value comparisons. 
In Python a < b < c has the exact same performance as a < b and b < c during execution, and is very slightly faster during compilation to byte code. With sufficiently aggressive inlining C++ expression templates would have the same run time as the raw operators, but would be massively slower during compilation. Ex: gcc 4.9.2 with -03 generates the same assembly code for comp1() and comp2() in the following code:
struct Lte {
  static bool process(int a, int  b) {
    return a <= b;

template <typename T>
struct Operator {};

Operator<Lte> lte;

template <typename Op, typename Lhs>
struct Holder {
  Lhs lhs;
  int rhs;

  Holder(Lhs l, int r) : lhs(l), rhs(r) {}

  operator bool() {
    return Op::process(lhs, rhs);

template <typename Op, typename Val>
struct Holder<Lte, Holder<Op, Val> > {
  Holder<Op, Val> lhs;
  int rhs;

  Holder(Holder<Op, Val> l, int r) : lhs(l), rhs(r) {}

  operator bool() {
    return lhs.operator bool() && (lhs.rhs <= rhs);

template <typename Op, typename Val>
struct LeftHandProxy {
  Val lhs;

  LeftHandProxy(Val val) : lhs(val) {}

template <typename Op, typename Val>
LeftHandProxy<Op, Val> operator<(Val lhs, Operator<Op> rhs) {
  return LeftHandProxy<Op, Val>(lhs);

template <typename Op, typename Val>
Holder<Op, Val> operator>(LeftHandProxy<Op, Val> lhs, int rhs) {
  return Holder<Op, Val>(lhs.lhs, rhs);

bool comp1(int a, int b, int c, int d) {
  return a <= b && b <= c && c <= d;

bool comp2(int a, int b, int c, int d) {
  return a <lte> b <lte> c <lte> d;
This is slightly more complicated than necessary, but it compiles, produces the desired assembly code and I don't feel like spending more time on it just for a forum post. Adding other operators in addition to <= is left as an exercise for the reader, but should be fairly straightforward.

#5230150 Too clever...?

Posted by on 20 May 2015 - 06:04 PM

This isn't too bad. Overly clever would be putting 0, first, last and length in an array, calling is_sorted() on it, and relying on the compiler to unroll the loop to get the same result. Or abusing operator overloading and expression templates to get 0 <lte> first <lte> last <lte> length to compile so that you can python style chained comparisons.

#5230065 A whole lot of constants.... good or terrible? :/ (c++)

Posted by on 20 May 2015 - 11:22 AM

Note that for the case of IDLE_LEFT and WALK_LEFTDOWN you may not want a single constant that combines both IDLE vs WALK and direction. You might instead want a state enum that contains IDLE, WALK, RUN, etc. and a direction enum that has UP, DOWN, LEFT, RIGHT, etc. You may also want to choose your direction enum values so that things like DOWNLEFT can be expressed by a bitwise or or other arithmetic combination of an UP/DOWN constant and a LEFT/RIGHT constant. Even if you go for a single enum, you may want to be careful picking your values so that they can be decomposed in this manner. 

#5229676 Should I use an installer or .zip for my game?

Posted by on 18 May 2015 - 02:33 PM

When I still did Windows application development, I had a blank Windows XP virtual machine that had nothing but service pack 3 installed. My rule of thumb was that if I couldn't zip my application and run it on that VM, then it needed an installer. This basically meant that every single program I ever wrote needed an installer.