address of pointer is
0xad02b8
value stored in pointer is
0xad02bc
value pointed by pointer is
0
value stored in pointer is
0xad02c0
value pointed by pointer is
1
value stored in pointer is
0xad02c4
value pointed by pointer is
2
value stored in pointer is
0xad02c8
value pointed by pointer is
3
value stored in pointer is
0xad02cc //was this reserved by new?
value pointed by pointer is
4
Aborted
program works fine . The only thing that is bugging me is I am not able to assign any value to first address(since I have first incremented the pointer address itself and then assigned the value,it's obvious). Following are my doubts
1.Am I getting this aborted because I have reserved address only until 5 positions but I am accessing 6th memory location?
2. When I try to assign value to the pointer first and then increment the pointer, I get random value stored in the address. Why so?
For eg. If I do *p=0 and then p++ (which is more logical because in this way, I first address wont go unused)
3. When I try to put delete[]p at the end, I get a coredump. Ain't I am freeing memory at the right place? (before return)
1. Yes, you are correct.
2. Because the cout statement is placed after the increment. Place it after applying the value to p but before incrementing the pointer variable.
3. If you do things the right way that shouldn't happen.
When you increment the pointer and then access it with the cout obviously you get random garbage because you did not assign any value to the new address. If you want, just add a *(p-1) to the cout, then it will work. But use the suggestion I gave on point 2. not this.
Thank you sir ! oops. Din't strike me earlier that I was doing both at the same time. It now solves the increment and assignment problem. However, delete[] p still remains!
Now ,since I have placed p++ at the end, the last iteration still gives me an extra address(6th one). It prints this extra address but program works fine as long as I don't increment loop condition to (i<6) and try to assign any value to this address.
But putting delete[] p gives a coredump. I guess this is again related to this extra memory location?
Use delete p-5. You should pass the original p (the first address) to the delete operator. Since you increment p in the loop it loses the original address that was allocated to it and the delete doesn't work.
Use some other pointer variable for the loop or save the initial address of p before changing it's value. The best way would be introduce a new variable exquisitely for the loop.
Edit:
Why not use *(p+i) instead of incrementing p. That way, the value of p won't be disturbed.
well, I provided original address to delete and it worked fine. But as I have allotted memory to more than one elements, am I not supposed to free all of them instead of passing only original address?
int*q=p; delete q does the job. But then arnt we supposed to use delete [] p instead of delete?
or it's just we remove the original address pointer pointing to, freeing it and in turn, making all consecutive allocated addresses free?
I apologise, you should indeed use delete[] p for arrays. Doing it once should do the job, no need to delete every element one by one.
The operator knows how much space it had allocated and how much it has to free.
oh ! Sorry to irritate you but that is the problem in the first place. If you look at my code above, you will find delete [] p before return statement. But it is giving me a core-dump so I commented it. I am not sure why is it not working :)
sun@lap:~/projects$ cat test.cc
#include<iostream>
usingnamespace std;
int main()
{
int *p;
p=newint[5];
cout << "address of pointer is\n"<<p<<endl;
cout << endl;
for(int i=0;i<5;i++)
{
*(p+i) = i;
cout << "value stored in pointer is\n"<< (p+i) <<endl;
cout << "value pointed by pointer is\n"<< *(p+i) <<endl;
cout<<endl;
}
delete[] p;
return 0;
}
sun@lap:~/projects$ g++ test.cc -o test
sun@lap:~/projects$ ./test
address of pointer is
0x9c45008
value stored in pointer is
0x9c45008
value pointed by pointer is
0
value stored in pointer is
0x9c4500c
value pointed by pointer is
1
value stored in pointer is
0x9c45010
value pointed by pointer is
2
value stored in pointer is
0x9c45014
value pointed by pointer is
3
value stored in pointer is
0x9c45018
value pointed by pointer is
4
sun@lap:~/projects$ echo $? # check return value of the last programme that was run
0
sun@lap:~/projects$
As you can see it works fine here. The programme returns the code 0 which means everything went fine.