指针递减会产生错误的结果 [英] Pointer decrementation delivers wrong result

查看:64
本文介绍了指针递减会产生错误的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。


我有以下代码:


unsigned char temporary [160];

unsigned __int64 * reader =(unsigned __int64 *)& temporary + 20;


std :: cout<< * reader--<< std :: endl;

std :: cout<< * reader--<< std :: endl;

std :: cout<< * reader--<< std :: endl;


第一个输出和第三个输出都可以。但第二个输出

给我的数据与char数组中的任何值都不匹配。

如果我从头开始做同样的操作递增

指针所有值都会被读取。


我真的很困惑。


g ,Hipo

解决方案

Hipo写道:

嗨。

我有以下代码:

unsigned char temporary [160];
unsigned __int64 * reader =(unsigned __int64 *)& temporary + 20;


& temporary是阵列的地址。 & temporary + 20将是160个字符数组数组的第20个元素的地址

。你可能意味着

临时+20(没有&)。

std :: cout<< * reader--<< std :: endl;
std :: cout<< * reader--<< std :: endl;
std :: cout<< * reader--<< std :: endl;

第一个输出和第三个输出都可以。但第二个输出
给了我与char数组中的任何值都不匹配的数据。
如果我从头开始做同样的操作并递增
指针全部值就像它们应该的那样读。




Rolf Magnus schrieb:

Hipo写道:
< blockquote class =post_quotes>嗨。

我有以下代码:

unsigned char temporary [160];
unsigned __int64 * reader =(unsigned __int64 *)& temporary + 20;



& temporary是数组的地址。 & temporary + 20将是160 char数组数组的第20个元素的地址
。你可能意味着
临时+20(没有&)。




抱歉,输入错误

< blockquote class =post_quotes> std :: cout<< * reader--<< std :: endl;
std :: cout<< * reader--<< std :: endl;
std :: cout<< * reader--<< std :: endl;

第一个输出和第三个输出都可以。但第二个输出
给了我与char数组中的任何值都不匹配的数据。
如果我从头开始做同样的操作并递增
指针全部值读取就像它们应该的那样。




g,Hipo


Rolf Magnus写道:

Hipo写道:

嗨。

我有以下代码:

unsigned char temporary [160 ];
unsigned __int64 * reader =(unsigned __int64 *)& temporary + 20;



& temporary是数组的地址。 & temporary + 20将是160 char数组数组的第20个元素的地址
。你可能意味着临时+20(没有&)。




消除&在临时数组前面没有区别 -

阅读器变量的地址是相同的。并且该地址对8字节整数数组的第21个元素起作用,然后是b
$ b等于160字符的第161个字符的地址
/>
数组。换句话说,reader变量首先指向超出为临时数组分配的内存的第一个字节。


这段代码当然是一个很好的例子为什么指针和C数组最好避免使用



Greg


Hi.

I have the following code:

unsigned char temporary[160];
unsigned __int64 *reader = (unsigned __int64*)&temporary+20;

std::cout << *reader-- << std::endl;
std::cout << *reader-- << std::endl;
std::cout << *reader-- << std::endl;

The first output and the third output are OK. But the second output
gives me data that doesn''t match to any values in the char array.
If I''m doing the same operation from the beginning and incrementing the
pointer all values are read like they should.

I''m really puzzled.

g, Hipo

解决方案

Hipo wrote:

Hi.

I have the following code:

unsigned char temporary[160];
unsigned __int64 *reader = (unsigned __int64*)&temporary+20;
&temporary is the address of the array. &temporary+20 would be the address
of the 20th element of an array of arrays of 160 char. You probably meant
temporary+20 (without the &).
std::cout << *reader-- << std::endl;
std::cout << *reader-- << std::endl;
std::cout << *reader-- << std::endl;

The first output and the third output are OK. But the second output
gives me data that doesn''t match to any values in the char array.
If I''m doing the same operation from the beginning and incrementing the
pointer all values are read like they should.




Rolf Magnus schrieb:

Hipo wrote:

Hi.

I have the following code:

unsigned char temporary[160];
unsigned __int64 *reader = (unsigned __int64*)&temporary+20;



&temporary is the address of the array. &temporary+20 would be the address
of the 20th element of an array of arrays of 160 char. You probably meant
temporary+20 (without the &).



Sorry, typing error

std::cout << *reader-- << std::endl;
std::cout << *reader-- << std::endl;
std::cout << *reader-- << std::endl;

The first output and the third output are OK. But the second output
gives me data that doesn''t match to any values in the char array.
If I''m doing the same operation from the beginning and incrementing the
pointer all values are read like they should.



g, Hipo


Rolf Magnus wrote:

Hipo wrote:

Hi.

I have the following code:

unsigned char temporary[160];
unsigned __int64 *reader = (unsigned __int64*)&temporary+20;



&temporary is the address of the array. &temporary+20 would be the address
of the 20th element of an array of arrays of 160 char. You probably meant
temporary+20 (without the &).



Eliminating the & in front of the temporary array makes no difference -
the address of the reader variable is the same. And that address works
out to the 21st element of an array of 8 byte integers which in turn is
equivalent to the address of the 161st character of the 160 character
array. In other words, the reader variable starts by pointing to the
first byte beyond the memory allocated for the temporary array.

This code certainly makes a good example why pointers and C arrays are
best avoided.

Greg


这篇关于指针递减会产生错误的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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