简体   繁体   English

reinterpret_cast <int *> (字符*)

[英]reinterpret_cast<int *>(char *)

When dynamically allocating a buffer of type char * and when you want to cast it to a particular type, should you use: 动态分配char *类型的缓冲区时,以及要将其强制转换为特定类型时,应使用:

reinterpret_cast<int *>(char *)

or: 要么:

static_cast<int *>(static_cast<void *>(char *))

and why? 为什么呢?

This *pk = new int(2); *pk = new int(2); should be pk instead of *pk 应该是pk而不是*pk

After you've declared a pointer int * pointer , all references to point refer to an address and all references to *pointer refer to what's contained at the address. 在将指针声明为int * pointer ,所有point引用都point一个地址,而所有*pointer引用都*pointer该地址中包含的内容。 By default, pointers don't point to valid memory locations, you have to assign a valid address or make a call to new . 默认情况下,指针不会指向有效的内存位置,您必须分配一个有效的地址或调用new This is why when you write pk = &k , there is no segmentation faults - because pk now contains the address of k , which points to a valid memory location. 这就是为什么当您写pk = &k ,不会出现分段错误-因为pk现在包含k的地址,该地址指向有效的内存位置。

The syntax for pointers can be a little confusing, mainly because * can have multiple meanings, depending on how you use it. 指针的语法可能会有些混乱,主要是因为*根据您的使用方式可能具有多种含义。 Here's a quick explanation: 这里是一个简短的解释:

  • In declarations, * means you are declaring a pointer (ie int * pk ). 在声明中, *表示您正在声明一个指针(即int * pk )。
  • In between to numerical data types, * is the symbol for multiplication (ie 4 * num ). 在数字数据类型之间, *是要乘的符号(即4 * num )。
  • Written next to a variable, it returns the value at the address that the pointer contains (ie *myPointer or (*mypointer) ). 写在变量旁边,它返回指针包含的地址处的值(即*myPointer(*mypointer) )。

Since new returns an address, you are assigning an address to pk instead of a value pointed by pk . 由于new返回一个地址,因此您要为pk分配一个地址,而不是pk指向的值。

If you wish to initialize a pointer, declare it with a new statement. 如果要初始化指针,请使用new语句声明它。 Like this: 像这样:

int * pk = new int;

But there is no compile time feature that declares a self-contained pointer and value. 但是没有编译时功能声明一个自包含的指针和值。 The closest you'll come to that is by declaring separate variable, as you have done, and initialize the pointer with the address of that variable. 与之最接近的是,通过声明一个单独的变量,就像已经做的那样,并使用该变量的地址初始化指针。 Like this: 像这样:

int num = 43;
int * p = &num; 
  • Can you explain me what's going on here? 你能解释一下这是怎么回事吗?

You use unitialized variable: http://en.wikipedia.org/wiki/Uninitialized_variable 您使用单元化变量: http : //en.wikipedia.org/wiki/Uninitialized_variable

int* pk;  // declare variable `pk` of type `int*` but do not initialize it
*pk = 2;  // use unitialized variable. `*` is dereference operator. Seg fault
int k;    // declare variable `k` of type `int`, not initialized
pk = &k;  // initialize variable `pk` with address of variable `k`
*pk = 3;  // use initialized variable. Ok now

Dereference operator: http://en.wikipedia.org/wiki/Dereference_operator 取消引用运算符: http : //en.wikipedia.org/wiki/Dereference_operator

int* pk = nullptr;

Here you are declaring that pk is a pointer to an int . 在这里,您声明pk是一个指向int的指针。

*pk = new int(2);

The type of *pk is int and the return type of new int(2) is int* . *pk的类型为intnew int(2)的返回类型为int*

pk is the pointer while *pk is the value to which it points. pk是指针,而*pk是它指向的值。

So change your code to, 因此,将您的代码更改为

int* pk = new int;

pk = reinterpret_cast<int*>(2);

When assigning to pk the right-hand side must be a pointer, for *pk it must be an integer. 分配给pk ,右侧必须是指针,而*pk则必须是整数。

How can I initialize a pointer with a value? 如何使用值初始化指针?

You don't. 你不知道 That's the point of having a pointer. 这是具有指针的 You don't initialize it with value. 您不使用值对其进行初始化。 You initialize it with variable address . 您可以使用变量address对其进行初始化。

Let's analyze it step by step: 让我们逐步分析它:

int* pk;

You have a pointer, and it's uninitialized, so now it's worse than useless. 您有一个指针,并且它尚未初始化,因此,它比没用的要糟。

int* pk = nullptr;

Much better, at least you can check with an if statement if it points to anything. 更好的是,至少您可以使用if语句检查它是否指向任何内容。

if(pk)
{
    //do operations on pointed value *pk
}
else
{
    //Houston, we have a problem
}

For now, these operations make no sense 目前,这些操作没有任何意义

*pk = 2; //you haven't pointed with pk to anything
*pk = reinterpret_cast<int*>(2); //????? *pk is int, and right-hand side is int*. What do you expect to do with it?

This code works 此代码有效

int k;
pk = &k;
*pk = 3;

because you actually pointed pk to something meaningful. 因为您实际上将pk指向了有意义的东西。

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

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