Heap error

I am learning c++. I am learning about pointers, the free store and how to use it. At the end of this code i am trying to release memory used by the pointer pname. what am I doing wrong?



#include <iostream>
#include <cstring>
#include <iomanip>


using namespace std;

int main()
{

char* pname;
char* panswers[5];
int* page;

pname = new char;
page = new int;


for(int i = 0 ; i<=4 ; i++)
panswers[i] = new char[20];

cout << "Please input your name: "<<endl;cin.get(pname,15,'\n');
cout << "\nPlease input your age: ";cin>>*page;

cout<<"your name is "<<pname<<" and your age is "<<*page<<endl;


cout<<"\nPlease input your favorite 5 actors\n";
for(int j=0;j<=4;j++)
{
cout<<j+1<<". :";cin>>panswers[j];
cout<<endl;

}

cout<<"Your favorite actors are\n";
for(int i = 0;i<=4;i++)
{
cout<<endl<<i+1<<": "<<*(panswers+i)<<endl;

}



delete [] *panswers;

delete page;

delete pname;

return 0;
}
Please use the code format tag to format the code you post. You can edit the post rather than posting again.
char* pname; // This declares your pointer -- good

pname = new char; // This allocates 1 byte of the heap and pnane points to it

cout << "Please input your name: "<<endl;cin.get(pname,15,'\n'); // This reads upto 15 bytes into the allocated block, but you only allocated 1 byte, not 15
Last edited on
You have to loop through the elements of panswers to do the delete.
1
2
for (int i; i<5;i++)
   delete [] panswers[i];

And pname has to larger than just one char, like
1
2
3
4
5
pname = new char [20];

...

delete [] pname;

Last edited on
I am confused, how can I increase the block I allocate to hold a name(I do not want to use a pointer array or a array)?
The light just went off, Thank you
closed account (zb0S216C)
A dynamically allocated region of memory must be re-allocated in order for its size to change. The standard vector[1] is an array which can grow and shrink to your specifications.

References:
[1]http://www.cplusplus.com/reference/stl/vector/


Wazzak
Ok now Im confused, Im trying to create a dynamic pointer and using cin.get() mainly to allow flexibility and end the string at the '\n' character. I am a beginner so throwing in the vector terminology is un-needed its only confused. is there a better way?
closed account (zb0S216C)
The only way I can think of is a linked-list. If you don't already know, a linked-list is a chain of pointers. More specifically, this:

1
2
3
4
5
6
7
8
9
struct SOListNode
{
    SOListNode *pNextNode;
};

SOListNode *pSIRootNode = new SOListNode;
pSIRootNode->pNextNode = new SOListNode;
pSIRootNode->pNextNode->pNextNode = new SOListNode;
// ...and so on. 


In you case, your structure would appear like this:

1
2
3
4
5
struct SOListNode
{
    char cCharacter;
    SOListNode *pNextNode;
};

When used ineffectively, lists can get cumbersome and confusing.

Wazzak
Last edited on
If you want to read a line of indeterminate length, there's already an stl solution.
1
2
std::string line;
std::getline(std::cin, line);
Topic archived. No new replies allowed.