C强制转换的真正作用是什么? [英] What does a C cast really do?

查看:69
本文介绍了C强制转换的真正作用是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了越来越多的C应用程序,现在我想知道有关强制转换的一些信息。在C ++中,动态转换是一项非常昂贵的操作(例如,向下转换),但我什至不知道静态转换。

I write more and more C applications, and now I wonder something about casts. In C++, a dynamic cast is a very costly operation (for instance a down-cast), but I don’t even know for static one.

在C中,我必须写这样的东西:

In C, I had to write something like that:

assert ( p ); /* p is void* */
int v = *(int*)p;

这是«C动态广播»吗?它与C ++的 static_cast< int *>(p)完全相同吗?多少钱?

Is it a « C dynamic-cast »? Is it quite the same as the static_cast<int*>(p) of C++? How much does it cost?

预先感谢。

推荐答案

C强制转换仅在编译时才有意义,因为它告诉编译器您要如何操作一段数据。它不会更改数据的实际值。例如,(int *)p 告诉编译器将 p 视为整数的内存地址。但是,这在运行时不会花费任何费用,处理器只会按照原始数字的原始方式处理原始数字。

A cast in C is only meaningful at compile time because it tells the compiler how you want to manipulate a piece of data. It does not change the actual value of the data. For example, (int*)p tells the compiler to treat p as a memory address to an integer. However this costs nothing at run time, the processor just deals with raw numbers the way they are given to it.

这篇关于C强制转换的真正作用是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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