简体   繁体   English

当指针太大时会发生什么?

[英]What happens when a pointer is too big?

What happens, in C++ or C, if a pointer is set to a value so high that it goes out of the bounds of memory? 在C ++或C中,如果将指针设置为如此高的值以使其超出内存范围,会发生什么? Here's some code that would do such a thing: 这里有一些代码会做这样的事情:

int* ptr = 0;
while (true) {
    ptr += 1; // eventually this will go out of bounds of the memory ... unless there is an overflow.
    *ptr = 10; // just give it a value, why not?
}

What happens? 怎么了? Does the pointer -- when it gets out of the bounds of, let's say a 16 byte memory -- just flip from 0xF to 0x0? 指针 - 当它超出界限时,假设一个16字节的存储器 - 只是从0xF翻转到0x0? Does it keep counting up and the *ptr = 10; 它是否继续计数并且*ptr = 10; line crashes the computer? 线路崩溃了电脑?

I'm not stupid enough to try, but I am very curious. 我并不愚蠢尝试,但我很好奇。

Undefined behavior. 未定义的行为。

In fact, incrementing a pointer so it points to a location more than one element past the end of the object to which it originally pointed causes undefined behavior. 实际上,递增指针使其指向一个位置,该位置超过了它最初指向的对象末尾的多个元素会导致未定义的行为。 (You're permitted to construct a pointer just past the end of an object, but not beyond that.) (The object in question is an array, with a non-array object being treated as an array with one element.) (你可以在一个对象的末尾构建一个指针,但不能超过它。)(有问题的对象是一个数组,非数组对象被视为一个带有一个元素的数组。)

Which means that the language standard says nothing about what can happen. 这意味着语言标准没有说明会发生什么。 It might "work", it might crash your program, it might result in any symptom that your program is capable of producing. 它可能“有效”,它可能会使程序崩溃,可能会导致程序能够产生任何症状。

In practice, the behavior is likely to vary from one system to another. 在实践中,行为可能因系统而异。 It's likely that incrementing a pointer will do the same thing as incrementing an unsigned integer: update the value without causing any visible problems. 递增指针可能会与递增无符号整数完全相同:更新值而不会导致任何明显的问题。 If you try to dereference the pointer, the results depend on how your system manages memory. 如果尝试取消引用指针,结果取决于系统管理内存的方式。

Bottom line: Undefined behavior is undefined. 底线:未定义未定义的行为。

Accessing memory outside of what is allocated to you (by new or malloc etc.) is undefined behavior in C++, so don't expect anything to go well. 在C ++中访问分配给你的内存之外的内存(通过newmalloc等)是未定义的行为 ,所以不要期望任何事情顺利进行。

In practice, incrementing a pointer outside of the physical limits is defined by the platform, for most platforms, it will eventually roll over; 实际上,在物理极限之外增加指针是由平台定义的,对于大多数平台来说,它最终会翻转; accessing it may result in a crash. 访问它可能会导致崩溃。

For most modern OSes, attempting to read or write memory outside of what the process is allocated with result in an access violation or segmentation fault. 对于大多数现代操作系统,尝试在分配进程之外读取或写入内存会导致访问冲突或分段错误。

Pointers are only valid to an object or "1 pass". 指针仅对对象或“1遍”有效。

int x;
int *p = &x;
p++;
p++; // invalid
int y[5];
p = &y[4];
p = &y[5];
p = &y[6]; // invalid

Pointers may be assigned a null pointer value; 可以为指针指定空指针值;

p = NULL;

Pointers can refer to a funciton. 指针可以指代功能。

That's it. 而已。 Assigning a pointer to some other value is undefined behavior (UB) such as: 将指针指向其他值是未定义的行为(UB),例如:

int* ptr = 0;
ptr += 1;  // UB  (NULL is not a pointer to a valid object, so "1 pass" is UB)

The C/C++ language is not defined for an existing system/machine, but an abstract black box (although it does not ignore reality). C / C ++语言没有为现有系统/机器定义,而是为抽象黑盒子定义(尽管它不会忽略现实)。 You are asking for undefined behavior (some existing systems/machines may do what you expect) 您要求未定义的行为(某些现有系统/机器可能会按预期执行)

On the common systems I'm aware of (which would be mainly/solely my system), pointers behave very much like unsigned long s: 在我所知道的常见系统上(主要是/我的系统),指针的行为与unsigned long s非常相似:

int main(){
    char* p = NULL;
    printf("%p\n", p);
    printf("%p\n", --p);
}

outputs: 输出:

(nil)
0xffffffffffffffff

But I guess all kinds of exotic stuff could happen, theoretically. 但我想从理论上讲,各种异国情调都可能发生。

IMO, your pointer won't have any problem, there's nothing to happened if you just save the pointer, but when you try to dereference/access it i'm sure your code would ended up in access violation and crash your program. IMO,你的指针没有任何问题,如果你只是保存指针就没有任何事情发生,但当你试图取消引用/访问它时,我确信你的代码最终会导致访问冲突并导致程序崩溃。 or if it's a valid pointer, it would modify something else data, possibly modified another variable content tho' 或者,如果它是一个有效的指针,它将修改其他数据,可能修改另一个变量内容tho'

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

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