哪个更可读的(C ++ =) [英] Which is more readable (C++ = )

查看:86
本文介绍了哪个更可读的(C ++ =)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  INT valueToWrite = 0xFFFFFFFF的;
静态的char缓冲器2 [256];
为int * writePosition =为(int *)及缓冲器2 [5];
* writePosition = valueToWrite;//要么
*(为(int *)及缓冲器2 [10])= valueToWrite;

现在,我问你,你找到更可读的哪一个球员。涉及到一个临时变量或一步到位的技术的2步技术?

不要担心优化,他们都以最优化的同样的事情,因为你可以在这里看到。
只要告诉我这是给你更具可读性。

 或DWORD PTR?缓冲器2 @?1 ??主要@@ 9 @ 4PADA + 5,-1
或DWORD PTR?缓冲器2 @?1 ??主要@@ 9 @ 4PADA + 10,-1


解决方案

 为int * writePosition =(INT *)及缓冲器2 [5]

或者

  *((INT *)及缓冲器2 [10])= valueToWrite;

都是不正确,因为在某些平台上访问未对齐的值(+5 +10)可能会耗资数CPU周期和一些(如老式ARM)会引起非法操作。

正确的方法是:

 的memcpy(缓冲+ 5,&安培; valueToWrite,sizeof的(valueToWrite));

和它更具可读性。

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 do you find more readable. The 2 step technique involving a temporary variable or the one step technique?

Do not worry about optimization, they both optimize to 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

解决方案

int* writePosition = (int* ) &buffer2[5]

Or

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

Are both incorrect because on some platforms access to unaligned values (+5 +10) may cost hundreds of CPU cycles and on some (like older ARM) it would cause an illegal operation.

The correct way is:

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

And it is more readable.

这篇关于哪个更可读的(C ++ =)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆