使用动态内存C ++移动指针 [英] Moving pointer with Dynamic Memory C++

查看:85
本文介绍了使用动态内存C ++移动指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在这个论坛的第一个问题,对不起我的英语。
我有一个关于c ++中的指针和动态内存的问题。

this is my first question in this forum, sorry my bad english. I have a question about pointers and dynamic memory in c++.

示例,此代码:

#include <iostream>

using namespace std;

int main(int argc, char const *argv[])
{
  int *a = new int;

  for (int i = 0; i < 5; i++)
    cout << a++ << endl;

  return 0;
}

输出:

0x11d4c20
0x11d4c24
0x11d4c28
0x11d4c2c
0x11d4c30

我的问题是,为什么我可以移动多于我用 new 创建的单个内存块。

My question, is why can I move more than that 'single' block of memory that I created with new.


  • a 指向什么?

  • What is a pointing to?

使用 new int [] 时也会发生同样的情况, :

Same occurs with new int[], even if I specific the size:

#include <iostream>

using namespace std;

int main(int argc, char const *argv[])
{
  int *a = new int[2];

  for (int i = 0; i < 5; i++)
    cout << a++ << endl;

  return 0;
}

输出:

0x2518c20
0x2518c24
0x2518c28
0x2518c2c
0x2518c30

同样,发生了什么?


  • a 指向?

  • What is a pointing to?

这是否意味着我违反了记忆?

Does all of this mean I'm violating memory?

推荐答案

指向对象或对象后面的点是合法的。 new int [2] 创建两个相邻对象(在数组中),并返回指向第一个对象的指针。

It is legal to point at an object or the spot right after an object. A new int[2] creates two adjacent objects (in an array) and returns a pointer to the first one.

是的,你所做的是不允许的。将5加到指向单个对象的指针的行为不是由C ++标准定义的;编译器可以生成任何东西的程序集。

So yes, what you did above is not permitted. The behaviour of adding 5 to a pointer to a single object is not defined by the C++ standard; the compiler can generate assembly that does anything at all.

在平面内存架构上,指针基本上是无符号整数。并且将指针递增1只是将指针递增指针对象的大小。

As it happens, on a flat memory architecture, pointers are basically unsigned integers. And incrementing a pointer by 1 is just incrementing it by the size of the pointed-to object.

所以经常发生的是你只是得到指针, 。

So what often happens is you just get pointers to whatever happens to be there.

现在这不是保证,不应依赖。 C ++的许多规则使动作未定义允许某些优化发生在天真映射你可能认为编译器。例如,指向 short 的指针不能指向 int ,并以定义的方式更改其值,这意味着如果一个函数具有 int short 指针,它可以假设写入 short 不会修改 int

Now this is not guaranteed and should not be relied upon. Many of the rules of C++ that make actions undefined permit certain optimizations to occur over the "naive" mapping you might think the compiler does. For example, pointers to short can never point to an int and change its value in a defined way, which means if a function has both an int and short pointer it can assume that a write to the short does not modify the int.

一个int方法使用shorts可以工作,然后不工作似乎没有理由,因为行为未定义,编译器可以自由优化假设它不能发生

The naive "write to the two words in an int" method of using shorts can work, then not work for seeming no reason, because the behaviour was undefined and the compiler was free to optimize assuming it could not happen.

简而言之,你的行为不合法,你所获得的行为并不奇怪,但你永远不会依赖它。

In short, your actions are not legal, the behaviour you got is not surprising, but you can never rely on it.

指针不只是无符号整数,evennif这是你的xompiler实现它们。他们是一个抽象。他们的行为不是由它们的实现决定的,而是由标准允许你做什么。当您以标准行事时,标准不允许,您获得的行为未被标准定义。编译器可以,并已知,利用这一事实假设未定义的行为不能和不会发生。由于编译器根据您的代码具有明确定义的行为的假设进行重新排序和优化,因此程序可能会在未定义的行为之前在代码行上出现意外行为。

Pointers are not just unsigned integers, evennif that is what your xompiler implements them with. They are an abstraction. Their behaviour is determined not by their implementation, but rather what the standard permits you do with them. When you act in ways the standard does not permit, the behaviour you get is undefined by the standard. Compilers can, and have been known to, exploit that fact to assume undefined behaviour cannot and does not occur. The program could behave unexpectedly on lines of code prior to the undefined behaviour as the compiler reorders and optimizes based on the assumption your code has well defined behaviour.

这篇关于使用动态内存C ++移动指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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