What is more readable (C ++ =)

int valueToWrite = 0xFFFFFFFF; static char buffer2[256]; int* writePosition = (int* ) &buffer2[5]; *writePosition = valueToWrite; //OR * ((int*) &buffer2[10] ) = valueToWrite; 

Now I ask you guys which one you think is more readable. A two-stage method involving a temporary variable or a single-stage technique?

Don't worry about optimizations, they both optimize the same thing, as you can see here. Just tell me which one is more readable for you.

 or DWORD PTR ?buffer2@ ?1??main@ @ 9@4PADA +5, -1 or DWORD PTR ?buffer2@ ?1??main@ @ 9@4PADA +10, -1 
+1
source share
9 answers
 int* writePosition = (int* ) &buffer2[5] 

or

 *((int*) &buffer2[10] ) = valueToWrite; 

Both are incorrect, because on some platforms access to unaligned values โ€‹โ€‹(+5 +10) can cost hundreds of processor cycles, and on some (for example, the older ARM) this will lead to illegal operation.

The right way:

 memcpy( buffer+5, &valueToWrite, sizeof(valueToWrite)); 

And it is more readable.

+19
source

Once you encapsulate it inside a class, it doesn't matter which method you use. The method name will contain a description of what the code does. Thus, in most cases, you do not have to delve into the actual imp. to see what happens.

 class Buffer { char buffer2[256]; public: void write(int pos, int value) { int* writePosition = (int*) &buffer2[pos]; *writePosition = value; } } 
+9
source

If I was forced to choose, I would say 1. However, I will notice that the code presented is very similar to C as in any case; I shy away from this and review the problem. Here's a simple, more C ++ - y

 const char * begin = static_cast<char*>(static_cast<void*>(&valueToWrite)); std::copy(begin, begin+sizeof(int), &buffer2[5]); 
+4
source

The first example is more readable only on the basis that your brain does not need to decipher pointer operations combined together.

This will reduce the time when the developer looking at the code for the first time needs to understand what is really happening. In my experience, this weakly correlates with a reduced probability of introducing new errors.

+2
source

I find the second, shorter, easier to read.

I suspect, however, that it more likely depends on whether you are the kind of person who can easily โ€œgetโ€ pointers.

The casting type from char * to int * is a bit inconvenient. I guess there is a good reason why this should be done.

+1
source

Beware - this code probably won't work due to alignment problems! Why not just use memset ?

 #include <string.h> memset(buffer2+10, 0xFF, 4); 
+1
source

If you can afford to bind yourself to a single compiler (or do preprocessor hacks on compatibility issues), you can use the option of packed structures to get symbolic names for the values โ€‹โ€‹you write. For example, in GCC:

 struct __attribute__ ((__packed__)) packed_struct { char stuff_before[5] int some_value; } /* .... */ static char buffer2[256]; struct packed_struct *ps = buffer2; ps->some_value = valueToWrite; 

This has several advantages:

  • Your code more clearly reflects what you do if you name your fields well.
  • Because the compiler knows if a platform that supports efficient, unequal access supports it, it can automatically choose between its own unrelated access or the corresponding workarounds on platforms that do not support unattached access.

But again, the main drawback is the lack of a standardized syntax.

+1
source

The most readable will be either a commentary option added to what you are doing there.

Speaking of this, I despise variables introduced just for a single use of a few lines later. Performing most of my work in the service sector, getting dozens of variable names that are pushed onto my face that work poorly, without having to write an explanatory comment, it suits me.

0
source

Definitely:

 * ((int*) &buffer2[10] ) = valueToWrite; 

I analyze it not in one but in several steps, and therefore it is more readable: I have all the steps in one line.

0
source

All Articles