指针算术问题 [英] Pointer arithmetic question

查看:93
本文介绍了指针算术问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于以下内容:


char * ptr1,* ptr2;

size_t n;


ptr2 = ptr1 + n;


假设ptr1是一个有效指针,以下是否保证为真?


(ptr2 - ptr1)= = n


如果n大于ptr1指向的缓冲区大小,该怎么办?


例如:


char buf [10];

char * pt = buf + 100;

size_t n =(pt - buf);


n保证是100吗?或者,在缓冲区末尾计算一个

地址的简单行为(超出紧跟缓冲区的地址后的地址)会调用UB吗?
< br $> b $ b -

+ ------------------------- + ------ -------------- + ----------------------------- +

| Kenneth J. Brody | www.hvcomputer.com | |

| kenbrody / at\spamcop.net | www.fptech.com | #include< std_disclaimer.h> |

+ ------------------------- + -------------- ------ + ----------------------------- +

不要给我发电子邮件:< mailto:Th ************* @ gmail.com>

解决方案

Kenneth Brody写道:

给出以下内容:

char * ptr1,* ptr2;
size_t n;

ptr2 = ptr1 + n ;


如果n>未定义的行为1.如果n

== 1,则不得取消引用ptr2,但不会产生溢出。好的,如果n == 0.

假设ptr1是一个有效的指针,以下是否保证是真的?

(ptr2 - ptr1)== n

如果n大于ptr1指向的缓冲区的大小怎么办?

例如:

char buf [10];
char * pt = buf + 100;


未定义的行为(与上面相同的C& V)。

你只能超过数组的末尾,然后

你不能试图取消引用结果指针。

size_t n =(pt - buf);


n的正确类型是stddef.h中的diffptr_t

n是否保证为100?或者,在缓冲区末尾计算一个
地址的简单行为(超出紧跟缓冲区的地址之外)是否会调用UB?




是的。


干杯


弗拉基米尔


PS

C& V:6.5.6.x,尤其是6.5.6.7-11。

-

我的电子邮件地址是真的,我看了。


< blockquote> Vladimir S. Oka写道:

Kenneth Brody写道:

size_t n =(pt - buf);



正确n的类型是来自stddef.h的diffptr_t




很容易赢得最快的手指优先...... :-(


当然是来自< stddef.h>的ptrdiff_t。


对不起


弗拉基米尔


-

我的电子邮件地址是真的,我看了。


你好


添加指针不仅仅是1 + 1 = 2,而且指针的类型是

importand。


example


double * a,b [100];

a = b;

printf(" ba =%d",a - (a + 1));


给4,因为sizeod double是4字节

减去是直接的

plu s是指针类型的大小


所以你也可以这样做


加倍[100];


现在是(& a [10] == a + 10)给出TRUE

类型a不是重要的。


问候


Given the following:

char *ptr1, *ptr2;
size_t n;

ptr2 = ptr1 + n;

Assuming ptr1 is a valid pointer, is the following guaranteed to be true?

(ptr2 - ptr1) == n

What if n is greater than the size of the buffer to which ptr1 points?

For example:

char buf[10];
char *pt = buf + 100;
size_t n = (pt - buf);

Is n guaranteed to be 100? Or, does the simple act of calculating an
address off the end of the buffer (beyond the address that immediately
follows the buffer) invoke UB?

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don''t e-mail me at: <mailto:Th*************@gmail.com>

解决方案

Kenneth Brody wrote:

Given the following:

char *ptr1, *ptr2;
size_t n;

ptr2 = ptr1 + n;
Undefined behaviour if n > 1. You must not dereference ptr2 if n
== 1, but no overflow is generated. OK if n == 0.

Assuming ptr1 is a valid pointer, is the following guaranteed to be true?

(ptr2 - ptr1) == n

What if n is greater than the size of the buffer to which ptr1 points?

For example:

char buf[10];
char *pt = buf + 100;
Undefined behaviour (same C&V as above).
You can only go one past the end of the array, and then
you must not try to dereference the resultant pointer.
size_t n = (pt - buf);
Correct type for n is diffptr_t from stddef.h

Is n guaranteed to be 100? Or, does the simple act of calculating an
address off the end of the buffer (beyond the address that immediately
follows the buffer) invoke UB?



Yes it does.

Cheers

Vladimir

PS
C&V: 6.5.6.x, esp. 6.5.6.7-11.
--
My e-mail address is real, and I read it.


Vladimir S. Oka wrote:

Kenneth Brody wrote:

size_t n = (pt - buf);



Correct type for n is diffptr_t from stddef.h



Would''ve easily won fastest-fingers-first... :-(

It''s ptrdiff_t from <stddef.h>, of course.

Sorry

Vladimir

--
My e-mail address is real, and I read it.


Hello

Adding pointers is not just 1+1 = 2 but the type of the pointer is
importand.

example

double *a, b[100];
a= b;
printf("b-a=%d",a-(a+1));

give 4 because the sizeod double is 4 byte

subtract is straight forward
plus is size of pointer type

so you can do also

double a[100];

now is (&a[10] == a+10) gives TRUE
wich type a is is not importand.

Greetings


这篇关于指针算术问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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