简体   繁体   English

在C中将成本寻址指针从无符号char *转换为const char *?

[英]Casting cost addressing pointer in c from unsigned char * to const char *?

I am wondering how many there will be calculating at compile for casting of addressing. 我想知道在编译中将有多少计算用于地址转换。

eg 例如

in strlen API defined : strlen API中定义:

size_t strlen ( const char * str );

they get arr only const char * . 他们只得到arr const char *

but sometime I use like 但是有时候我用

unsigned char arr[] = "something"
strlen((const char *)arr);

just focus on unsigned char * to const char * 只关注unsigned char *const char *

I wonder what cost there will be in code? 我想知道代码会花多少钱?

There is no runtime overhead associated with a cast in this way. 通过这种方式,没有与转换相关的运行时开销。 It merely tells the compiler to access (or interpret) the variable differently from how it was declared. 它仅告诉编译器以不同于声明方式的方式访问(或解释)变量。

In your specific case, the compiler is smart enough to generate code that treats a char array like a char* pointer with no overhead. 在您的特定情况下,编译器足够聪明,可以生成将char数组视为char*指针的代码,而不会产生开销。

In general, casts in C never incur any runtime overhead. 通常,C语言中的强制转换永远不会产生任何运行时开销。

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

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