简体   繁体   English

C ++内存测试返回奇怪的输出

[英]C++ Memory test returns weird output

Slackware here. Slackware在这里。 I was just messing around with memory stuff and pointers... I wanted to learn a bit more about those, so I made an array in c++, and looked up the memory address of the first item in it...: 我只是搞乱内存和指针...我想学习更多关于那些,所以我用c ++创建了一个数组,并查找了第一项的内存地址......:

string foo[3] = {"a", "b", "c"};
cout << *(&foo[0]-4) << endl;

It outputted this: http://pastebin.com/K0HAL5nJ The whole code: 它输出: http//pastebin.com/K0HAL5nJ整个代码:

#include <iostream>

using namespace std;

int main()
{
    string foo[3] = {"a", "b", "c"};
    cout << &foo[0] << " minus " << &foo[1] << " equals " << int(&foo[0])-int(&foo[1]) << endl;
    cout << *(&foo[0]-4) << endl;
    cout << "Hello world!" << endl;
    return 0;
}

I am a complete beginner in c++ and do not understand why this happens at all... I know that this sort of code is not supposed to... be, but still, could anyone please explain what happened there? 我是c ++的完全初学者,并且不明白为什么会发生这种情况...我知道这种代码不应该......但是,仍然可以请任何人解释那里发生的事情吗?

It's undefined behaviour. 这是未定义的行为。 &foo[0] gives you the address of the first std::string object, which you then subtract 4 from. &foo[0]为您提供第一个std::string对象的地址,然后从中减去4。 From §5.7 Additive operators: 来自§5.7添加剂运算符:

If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; 如果指针操作数和结果都指向同一个数组对象的元素,或者指向数组对象的最后一个元素,则评估不应产生溢出; otherwise, the behavior is undefined. 否则,行为未定义。

Undefined behaviour means you could experience anything. 未定义的行为意味着您可以体验任何事物。 What is probably happening is some area of memory, four positions before the beginning of the array, that is not a valid std::string object is being treated as a std::string . 可能发生的是一些内存区域,在数组开始之前的四个位置,这不是一个有效的std::string对象被视为std::string This is bound to lead to ugly things happening. 这必将导致丑陋的事情发生。

Pointer addition and element size 指针添加和元素大小


When you add an integer to a pointer, the integer is multiplied by the element size of the type that the pointer points to. 向指针添加整数时,整数乘以指针指向的类型的元素大小。

// Assume sizeof(int) is 4.
int b[100];  // b is an array of 100 ints.
int* p;      // p is a a pointer to an int.
p = b;       // Assigns address of first element of b. Ie, &b[0]
p = p + 1;   // Adds 4 to p (4 == 1 * sizeof(int)). Ie, &b[1]

http://www.fredosaurus.com/notes-cpp/arrayptr/26arraysaspointers.html http://www.fredosaurus.com/notes-cpp/arrayptr/26arraysaspointers.html

  cout << *(&foo[0]-4) << endl;

this code is to print foo[-4] 这段代码是打印foo [-4]

try this code. 试试这段代码。

 cout << *(&foo[4]-4) << endl;

this will print foo[0] 这将打印foo [0]

 T * p;
 int n;

p+n means that the address of p add sizeof(T *)*n p+n意味着的地址p添加的sizeof(T *)* n个

Pointer addition and element size 指针添加和元素大小
When you add an integer to a pointer, the integer is multiplied by the element size of the type that the pointer points to. 向指针添加整数时,整数乘以指针指向的类型的元素大小。

// Assume sizeof(int) is 4.
int b[100];  // b is an array of 100 ints.
int* p;      // p is a a pointer to an int.
p = b;       // Assigns address of first element of b. Ie, &b[0]
p = p + 1;   // Adds 4 to p (4 == 1 * sizeof(int)). Ie, &b[1]

You are accessing some memory that is out of the address space of the array you allocated, which results in undefined behavior. 您正在访问一些超出您分配的数组的地址空间的内存,这会导致未定义的行为。

 string foo[3] = {"a", "b", "c"};
 cout << &foo[0] << " minus " << &foo[1] << " equals " 
      << int(&foo[0])-int(&foo[1]);

 &foo[0] get the memory address of "a",
 &foo[1] get the memory address of "b";
 the output is OK since both address are in range of foo's address space

cout << *(&foo[0]-4) << endl;
 You tried to get the value at address of ("a" -4),
since this address is outside the address of foo, it is undefined behavior. 

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

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