简体   繁体   English

通过取消引用将字符串与 uint32_t 进行比较

[英]Compare string with uint32_t by dereference

Say for instance I wanted to parse input from the user/stdin, this input was a string ( char* / char [] ).比如说我想解析来自用户/标准输入的输入,这个输入是一个字符串( char* / char [] )。 I know I can typecast a string's memory address pointer to a uint32_t pointer and dereference said uint32_t pointer to extract a unsigned int value which is the same across most architectures (that I've tested).我知道我可以将字符串的内存地址指针类型转换为uint32_t指针并取消引用所述uint32_t指针以提取一个unsigned int值,该值在大多数体系结构中都是相同的(我已经测试过)。 Is this safe?这安全吗? Can it lead to bugs/undefined behavior?它会导致错误/未定义的行为吗? ie just in general a bad idea?即一般来说是个坏主意?

Example:例子:


#include <stdio.h>
#include <stdint.h>

int main(void) {
    char data[] = "status";
    printf("%u\n", *(uint32_t*)data);

    return 0;
}

Will result in 1952543859 on 64bit and 32bit (windows & linux, not tested on MAC/unix system)将导致1952543859在 64 位和 32 位(windows 和 linux,未在 MAC/unix 系统上测试)

Is it safe to do the below operation?:进行以下操作是否安全?:

char str_from_input[] = "status";
if (*(uint32_t*)str_from_input == 1952543859)
   //... execute some code oriented around "status".

My obvious aim is to reduce computations by not repeatedly calling string parsing functions like strcmp and strncmp by a direct uint32_t check/if statement.我的明显目标是通过不通过直接uint32_t check/if 语句重复调用strcmpstrncmp等字符串解析函数来减少计算。

… typecast a string's memory address pointer to a uint32_t pointer… Is this safe? ……将字符串的内存地址指针类型转换为 uint32_t 指针……这样安全吗?

No. If the string is not properly aligned for a uint32_t , the behavior of the cast is not defined, per C 2018 6.3.2.3 7: “… If the resulting pointer is not correctly aligned for the referenced type, the behavior is undefined…”否。如果uint32_t的字符串未正确对齐,则未定义uint32_t的行为,根据 C 2018 6.3.2.3 7:“......如果结果指针未正确对齐引用类型,则行为未定义...... ”

Dereferencing the resulting pointer accesses the string using a type that is inappropriate for it, violating the aliasing rule in 6.5 7, and the behavior is not defined by the C standard.取消引用结果指针会使用不适合它的类型访问字符串,违反了 6.5 7 中的别名规则,并且 C 标准没有定义该行为。

Can it lead to bugs/undefined behavior?它会导致错误/未定义的行为吗?

Yes.是的。

ie just in general a bad idea?即一般来说是个坏主意?

Yes.是的。

Is it safe to do the below operation?:进行以下操作是否安全?:

No.不。

To reinterpret the bytes in a string as a uint32_t , use:要将字符串中的字节重新解释为uint32_t ,请使用:

uint32_t t;
memcpy(&t, string, sizeof t);
printf("%" PRIu32 "\n", t);

( memcpy is declared in <string.h> , and PRIu32 is declared in <inttypes.h> .) memcpy<string.h>中声明, PRIu32<inttypes.h>声明。)

To copy the bytes of a uint32_t into a string, use memcpy(string, &t, sizeof t);要将uint32_t的字节复制到字符串中,请使用memcpy(string, &t, sizeof t); . .

A good C compiler is likely to replace the memcpy calls with direct data move instructions, if feasible.如果可行,一个好的 C 编译器很可能会用直接数据移动指令替换memcpy调用。

My obvious aim is to reduce computations by not repeatedly calling string parsing functions like strcmp and strncmp by a direct uint32_t check/if statement.我的明显目标是通过不通过直接uint32_t check/if 语句重复调用strcmpstrncmp等字符串解析函数来减少计算。

Avoid using kludges.避免使用杂物。 Write clear code and let the compiler optimize.编写清晰的代码并让编译器进行优化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM