#include <stdint.h>
#include <stdio.h>
typedef union {
uint64_t u[2];
struct {
uint64_t a:30;
uint64_t b:30;
uint64_t c:30;
uint64_t d:30;
uint64_t e:8;
}x;
} mystruct;
int main()
{
printf("Size %d\n", sizeof(mystruct));
}
Run Code Online (Sandbox Code Playgroud)
我想在64位机器上编译它.预期的输出是16但我得到24.我知道这里发生了一些对齐,但我不确定为什么结构x正好是16个字节.有人可以解释一下吗.谢谢
(机器是x86 64位运行SL6)
我试图看看我是否可以在我的64位机器上优化memset.根据我的理解,memset逐字节地设置并设置值.我假设如果我以64位为单位,它会更快.但不知何故需要更多时间.有人可以看看我的代码并提出原因吗?
/* Code */
#include <stdio.h>
#include <time.h>
#include <stdint.h>
#include <string.h>
void memset8(unsigned char *dest, unsigned char val, uint32_t count)
{
while (count--)
*dest++ = val;
}
void memset32(uint32_t *dest, uint32_t val, uint32_t count)
{
while (count--)
*dest++ = val;
}
void
memset64(uint64_t *dest, uint64_t val, uint32_t count)
{
while (count--)
*dest++ = val;
}
#define CYCLES 1000000000
int main()
{
clock_t start, end;
double total;
uint64_t loop;
uint64_t val;
/* memset 32 */
start = clock(); …
Run Code Online (Sandbox Code Playgroud)