简体   繁体   English

指针算法在C ++中不起作用

[英]pointers arithmetics not working in c++

I had worked with pointer arithmetics in C but, I just started to learn new and delete in c++ & I could not understand why I get runtime errors when incrementing the pointer in C++ 我曾经在C语言中使用指针算术,但是,我刚开始学习c语言中的newdelete ,我不明白为什么在C ++中增加指针时为什么会出现运行时错误

I get the following error when i use p++ or ++p... 使用p ++或++ p时出现以下错误...
free(): invalid pointer: 0x0000000002324c24 ***0x2324c240x2324c28Aborted (core dumped)

#include<iostream>
using namespace std;
int main()
{
    int *p;
    p=new int[4];
    *p=34;
    *(p+1)=36;
    cout<<++p;//doesnot work(I just wanted to print the address)
    cout<<p+1;//works 
    delete[] p;
    return 0;
}

You need to delete[] the original pointer, the one you got as a result of new[] . 您需要delete[] 原始指针,这是由于new[] And you loose the original pointer because you do ++p . 然后您松开了原始指针,因为您执行++p

That leads to undefined behavior when you do delete[] p . 当您执行delete[] p时,这导致未定义的行为

You need to use delete on the value returned by new . 您需要对new返回的值使用delete In your code this is not the case as you do ++p . 在您的代码中,情况并非如++p

This is because new/delete uses p to look up housekeeping information that you are not privy to. 这是因为new / delete使用p查找您不了解的客房信息。 As you pass in an invalid value for p it gets confused. 当您为p传递无效值时,会感到困惑。

This has nothing to do with a difference between C and C++. 这与C和C ++之间的差异无关。 It's solely about giving delete[] a pointer value that was not produced by new[] , and you'd get the same problem in C by giving free a pointer value not produced by malloc or calloc . 这仅仅是给delete[]一个不是new[]产生的指针值,而在C语言中,通过free一个不是malloccalloc产生的指针值,也会遇到同样的问题。 The pointer value that this code uses is one produced by incrementing the original; 该代码使用的指针值是通过递增原始值而产生的; use the original instead. 改用原版。


There is a difference between C and C++ in this area, though. 这方面的C和C ++之间的差异,虽然。 Namely that in C++ an application of the ++ operator is always an lvalue expression, while in C the result of the prefix ++ is “the new value of the operand after incrementation”. 即,在C ++中, ++运算符的应用程序始终是一个左值表达式,而在C中,前缀++的结果是“操作数后的新值”。 This difference of semantics is not relevant to your code, however. 但是,语义上的这种差异与您的代码无关。

C99 §6.5.3.1/2 C99§6.5.3.1/ 2

The value of the operand of the prefix ++ operator is incremented. 前缀++运算符的操作数的值增加。 The result is the new value of the operand after incrementation. 结果是递增后操作数的新值。 The expression ++E is equivalent to (E+=1) . 表达式++E等效于(E+=1)

C++11 §5.3.2/1 C ++ 11§5.3.2/ 1

The operand of prefix ++ is modified by adding 1, or set to true if it is bool (this use is deprecated). 前缀++的操作数通过加1进行修改,如果为bool则将其设置为true (不建议使用此用法)。 The operand shall be a modifiable lvalue. 操作数应为可修改的左值。 The type of the operand shall be an arithmetic type or a pointer to a completely-defined object type. 操作数的类型应为算术类型或指向完全定义的对象类型的指针。 The result is the updated operand; 结果是更新的操作数; it is an lvalue, and it is a bit-field if the operand is a bit-field. 它是一个左值,如果操作数是一个位字段,则它是一个位字段。 If x is not of type bool , the expression ++x is equivalent to x+=1 如果x不是bool类型,则表达式++x等效于x+=1


Instead of creating dynamic arrays via expressions such as new int[4] , consider using std::vector . 可以考虑使用std::vector ,而不是通过诸如new int[4]表达式创建动态数组。 It automates the memory management, which means that you avoid many problems, including the one in this question. 它使内存管理自动化,这意味着您可以避免很多问题,包括此问题中的一个。 And it provides much convenient functionality, and is extremely well tested and reliable, which means less work and higher productivity. 它提供了许多方便的功能,并且经过了充分的测试和可靠的测试,这意味着更少的工作量和更高的生产率。

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

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