23 votes
Accepted

Avoiding repetition when using the "snprintf idiom" to write text

@vnp's code is solid and helpful, but his stringify_state_helper is a single-purpose function, and still leaves a degree of repetition and memory management in <...
Jerry Coffin's user avatar
  • 33.5k
12 votes
Accepted

Decoding assembly instructions in a Game Boy disassembler

I have some ideas about how you might be able to improve your program. Avoid problems Rather than trying to deal with the problem for every instruction, one approach is avoiding it entirely. One ...
Edward's user avatar
  • 66.4k
11 votes
Accepted

Save and load the state of a role-playing game using Pickle

Comments You have #Player Class and #Enemy Class as comments. Comments should be used to clarify something that may otherwise ...
zondo's user avatar
  • 3,787
11 votes

Avoiding repetition when using the "snprintf idiom" to write text

Consider factoring it out into a function: ...
vnp's user avatar
  • 56.4k
10 votes
Accepted

Serializing objects to delimited files

readonly will not make members of your static serializers readonly. While you cannot reassign another serializer to replace it, its members can still be modified. ...
Xiaoy312's user avatar
  • 3,136
10 votes
Accepted

Writing a bitmap image from C++

Not bad! Let's go through it line-by-line. typedef unsigned char BYTE; This is fine, but just so you know, starting from C++17, there is a ...
indi's user avatar
  • 11.1k
10 votes
Accepted

Serializing (nested) data structures in a human-readable format

A few things I noticed: Since supported is a superset of singles I would recommend the following assignment: ...
riskypenguin's user avatar
  • 3,453
9 votes

Structure to ByteArray Extension

I haven't used it, however all of the documentation seems to suggest that you need explicitly free the memory allocated through: ...
forsvarir's user avatar
  • 11.6k
9 votes
Accepted

reinterpret_cast vs bit shifts for extracting 32-bit integers from bytes, with either endianness

You are correct to be concerned about the first method. It generates UB if buf[offset] doesn't happen to be at the right alignment boundary for a 32-bit value. The ...
indi's user avatar
  • 11.1k
9 votes

Avoiding repetition when using the "snprintf idiom" to write text

The GNU libc already provides asprintf, which avoids exactly the repetition you are concerned about. I haven't looked at the solutions of other operating systems (...
Roland Illig's user avatar
  • 21.3k
9 votes
Accepted

Serialization and deserialization a doubly-linked list with a pointer to a random node in C++

Thanks for posting your code and not being afraid of feedback. The indentation of your code is nice and you're already using a lot of stuff present in the C++ standard library. That's great. Unclear ...
Thomas Weller's user avatar
8 votes
Accepted

Packing and unpacking bits

Bug The swap<T>() method isn't doing what you think it does, because the passed in T obj1, T obj2 aren't passed with the <...
Heslacher's user avatar
  • 50.4k
8 votes

Parsing shorts from binary file and finding the closest and furthest points

Given that this was for an interview I'll refrain from saying that Javadoc is missing. private static final Point closest = new Point(-200, 300); That's a ...
Bobby's user avatar
  • 7,722
8 votes

Converting std::chrono::time_point to/from std::string

Headers and typenames We're missing ...
Toby Speight's user avatar
  • 74.9k
8 votes
Accepted

De-/Serialization of structs representing TCP messages

Your code produces no memory leaks since you're not manually allocating and deleting memory. Include all headers Always include all the headers. In this case, you need ...
Rish's user avatar
  • 2,101
7 votes
Accepted

Reading binary files in XTF format

An alternative solution could be a binary reader (see also: this code project article) Implementation: (I just replaced the 2 try catch blocks with one using): ...
JanDotNet's user avatar
  • 8,538
7 votes
Accepted

Hex String to Bytes (NSData)

First note that your code does not detect invalid input data. For example, the string "XX" is just converted to a zero byte. Detecting invalid input with ...
Martin R's user avatar
  • 23.2k
7 votes
Accepted

Converting a bytearray into an integer

To decode binary data, use struct.unpack(). ...
200_success's user avatar
7 votes
Accepted

Converting HEX values to Unicode characters

Only use try-except when you need to. Your outer try should be useless. You should also perform ...
Peilonrayz's user avatar
  • 43.3k
7 votes
Accepted

Sending data to a database in size-limited chunks

I waited a few days to get other answers to your questions. now I feel I can add my 2 cents. First off, the obvious: This is one single method. It does: Polling a queue Serialising a message into a ...
MrBrushy's user avatar
  • 1,198
7 votes

Workaround for unchecked cast of a deserialized Object to ArrayList<Vehicle>

In general, the Java compiler knows the type of each variable, at every point during the execution. And when you operate on incompatible types, the program won't compile. When you cast an object to <...
Roland Illig's user avatar
  • 21.3k
7 votes

C++ string Formatter Again Part-1

First of all, this is remarkably good and clean code! Nevertheless, I do have some things to nitpick on: Code Formatting Let's talk about line length. As of right now, you have a (physical) line in ...
Ben Steffan's user avatar
  • 5,228
7 votes

Decoding assembly instructions in a Game Boy disassembler

I think the big switch is a problem. Consider a more data-driven approach where each opcode is described by a struct: ...
Toby Speight's user avatar
  • 74.9k
7 votes
Accepted

IEnumerable<T> async serialization

You have both IEnumerableStreamSerializer and EnumerableStreamSerializer, and in fact neither of them are used in the code you ...
canton7's user avatar
  • 216
7 votes

Avoiding repetition when using the "snprintf idiom" to write text

Write the ifs such that the error paths are inside them, and the error-free path isn't indented: ...
alx - recommends codidact's user avatar
7 votes
Accepted

Convenience function to save and load pickle objects

A few general tips: Have a function name that indicates what the function does. If it's hard to come up with a name that describes the function's purpose (e.g. because it does totally different ...
Samwise's user avatar
  • 4,000
7 votes

De-/Serialization of structs representing TCP messages

Here are some things that may help you improve your program. Use all required #includes Part of the interface is the #include ...
Edward's user avatar
  • 66.4k
7 votes

Packing and unpacking values in a buffer

Auto-review by compiler: ...
Toby Speight's user avatar
  • 74.9k
6 votes
Accepted

Creating enchantment objects based on configuration

Assuming you already have a class called Enchant (should be a superclass of your enchantments), you could use an enum to organize all of your enchantments: ...
Salem's user avatar
  • 174
6 votes
Accepted

Three methods to serialize 7-, 6-, and 3-dimensional int arrays

An array in Java is an object, not a primitive, so you can accept the parameter as Object, without specializing it as an array (you do not do that in your current ...
Tamoghna Chowdhury's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible