pointers arithmetics not working in c++ -


i had worked pointer arithmetics in c but, started learn new , delete in c++ & not understand why runtime errors when incrementing pointer in c++

following error when use p++ or ++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 wanted print address)     cout<<p+1;//works      delete[] p;     return 0; } 

you need delete[] original pointer, 1 got result of new[]. , loose original pointer because ++p.

that leads undefined behavior when delete[] p.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -